forked from readium/kotlin-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check hash of LCP acquisitions (readium#439)
- Loading branch information
Showing
4 changed files
with
123 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
readium/lcp/src/main/java/org/readium/r2/lcp/util/Digest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2023 Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by the BSD-style license | ||
* available in the top-level LICENSE file of the project. | ||
*/ | ||
|
||
package org.readium.r2.lcp.util | ||
|
||
import java.io.File | ||
import java.security.MessageDigest | ||
import org.readium.r2.shared.extensions.tryOrNull | ||
|
||
/** | ||
* Returns the SHA-256 sum of file content or null if computation failed. | ||
*/ | ||
internal fun File.sha256(): ByteArray? = | ||
tryOrNull<ByteArray> { | ||
val md = MessageDigest.getInstance("SHA-256") | ||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) | ||
inputStream().use { | ||
var bytes = it.read(buffer) | ||
while (bytes >= 0) { | ||
md.update(buffer, 0, bytes) | ||
bytes = it.read(buffer) | ||
} | ||
} | ||
return md.digest() | ||
} |
31 changes: 31 additions & 0 deletions
31
readium/lcp/src/test/java/org/readium/r2/lcp/util/DigestTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2023 Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by the BSD-style license | ||
* available in the top-level LICENSE file of the project. | ||
*/ | ||
|
||
package org.readium.r2.lcp.util | ||
|
||
import java.io.File | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import org.junit.Test | ||
|
||
class DigestTest { | ||
|
||
private val file: File = | ||
File(DigestTest::class.java.getResource("a-fc.jpg")!!.path) | ||
|
||
@OptIn(ExperimentalEncodingApi::class, ExperimentalStdlibApi::class) | ||
@Test | ||
fun `sha256 is correct`() { | ||
val digest = assertNotNull(file.sha256()) | ||
assertEquals("GI42TOamBYJ4q4KKBcmMzlkfvld8bTVRcbjjQ20OvLI=", Base64.encode(digest)) | ||
assertEquals( | ||
"188e364ce6a6058278ab828a05c98cce591fbe577c6d355171b8e3436d0ebcb2", | ||
digest.toHexString() | ||
) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.