Skip to content

Commit 905ed0d

Browse files
committed
Fix potential issue with finalising of null source and cipher returning null final bytes (using certian cryto provider such as on Android).
1 parent 7c17e54 commit 905ed0d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/src/main/java/nl/nl2312/okio/cipher/CipherSource.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class CipherSource(
4343
}
4444
if (streamEnd) {
4545
// Finalize (with padding) if we are at the end
46-
decipheredBuffer.write(cipher.doFinal())
46+
val remainder = cipher.doFinal()
47+
if (remainder != null) {
48+
decipheredBuffer.write(remainder)
49+
}
4750
}
4851

4952
// Sink the requested number of bytes (or all that were still in the source)

lib/src/test/java/nl/nl2312/okio/cipher/CipherSourceTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class CipherSourceTest {
3535
decodeCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
3636
}
3737

38+
@Test
39+
fun read_emptySource() {
40+
val cipheredSource = Buffer()//.write(byteArrayOf())
41+
42+
val decoded = CipherSource(cipheredSource, decodeCipher)
43+
44+
val output = Buffer().also { it.writeAll(decoded) }
45+
assertThat(output.readUtf8()).isEqualTo("")
46+
}
47+
3848
@Test
3949
fun read_cipheredArray_oneBlock() {
4050
// NOTE Input is shorter than 1 cipher block of 16 bytes

0 commit comments

Comments
 (0)