Api cleanup
This commit is contained in:
parent
25adc330bd
commit
f03e94cf5e
@ -46,3 +46,7 @@ interface StatelessHash : Hash {
|
||||
): UByteArray
|
||||
}
|
||||
|
||||
fun String.encodeToUByteArray() : UByteArray{
|
||||
return encodeToByteArray().toUByteArray()
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ import org.khronos.webgl.Uint8Array
|
||||
*/
|
||||
interface JsSodiumInterface {
|
||||
|
||||
fun crypto_generichash(hashLength: Int, inputMessage: String) : Uint8Array
|
||||
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
|
||||
|
||||
fun randombytes_buf(numberOfBytes: Int) : Uint8Array
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import org.khronos.webgl.Uint8Array
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
@ -33,36 +34,17 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
|
||||
actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
override val MAX_HASH_BYTES: Int = 64
|
||||
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
val hashed = getSodium().crypto_generichash(64, inputString)
|
||||
val hash = UByteArray(MAX_HASH_BYTES)
|
||||
for (i in 0 until MAX_HASH_BYTES) {
|
||||
js(
|
||||
"""
|
||||
hash[i] = hashed[i]
|
||||
"""
|
||||
)
|
||||
}
|
||||
println("Hash ${hash.toHexString()}")
|
||||
return hash
|
||||
}
|
||||
|
||||
fun digestBlocking(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
val hashed = getSodium().crypto_generichash(hashLength, inputString)
|
||||
val hash = UByteArray(MAX_HASH_BYTES)
|
||||
for (i in 0 until MAX_HASH_BYTES) {
|
||||
js(
|
||||
"""
|
||||
hash[i] = hashed[i]
|
||||
"""
|
||||
)
|
||||
}
|
||||
println("Hash ${hash.toHexString()}")
|
||||
return hash
|
||||
}
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
val hashed = getSodium().crypto_generichash(64, Uint8Array(inputMessage.toByteArray().toTypedArray()))
|
||||
val hash = UByteArray(MAX_HASH_BYTES)
|
||||
for (i in 0 until MAX_HASH_BYTES) {
|
||||
js(
|
||||
"""
|
||||
hash[i] = hashed[i]
|
||||
"""
|
||||
)
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,9 +23,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
|
||||
}
|
||||
|
||||
actual object Sha256StatelessDelegated : StatelessSha256 {
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
}
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
|
@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
|
||||
}
|
||||
|
||||
actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
}
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.Crypto
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlin.test.Test
|
||||
@ -18,18 +19,9 @@ class Blake2bJsTest {
|
||||
@Test
|
||||
fun testBlake2BSodiumInterop() = testBlocking {
|
||||
Crypto.initialize()
|
||||
val hash = Blake2bDelegatedStateless.digest("test")
|
||||
val hash = Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
|
||||
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
|
||||
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBlake2BSodiumBlockingInterop() = testBlocking {
|
||||
Crypto.initialize()
|
||||
val hash = Blake2bDelegatedStateless.digestBlocking("test", null, 64)
|
||||
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
|
||||
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -50,20 +50,20 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
|
||||
|
||||
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
|
||||
actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
println("Input $inputString, ${key ?: "null"}, $hashLength")
|
||||
val hashResult = UByteArray(MAX_HASH_BYTES)
|
||||
val hashResultPinned = hashResult.pin()
|
||||
crypto_generichash(
|
||||
hashResultPinned.addressOf(0),
|
||||
hashLength.convert(),
|
||||
inputString.encodeToByteArray().toUByteArray().toCValues(),
|
||||
inputString.length.convert(),
|
||||
key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
|
||||
key?.length?.convert() ?: 0UL
|
||||
)
|
||||
return hashResult
|
||||
}
|
||||
// override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
// println("Input $inputString, ${key ?: "null"}, $hashLength")
|
||||
// val hashResult = UByteArray(MAX_HASH_BYTES)
|
||||
// val hashResultPinned = hashResult.pin()
|
||||
// crypto_generichash(
|
||||
// hashResultPinned.addressOf(0),
|
||||
// hashLength.convert(),
|
||||
// inputString.encodeToByteArray().toUByteArray().toCValues(),
|
||||
// inputString.length.convert(),
|
||||
// key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
|
||||
// key?.length?.convert() ?: 0UL
|
||||
// )
|
||||
// return hashResult
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@ -79,8 +79,6 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
key.toCValues(),
|
||||
key.size.convert() ?: 0UL
|
||||
)
|
||||
println("HashPointer: ${hashResult.toHexString()}")
|
||||
println(hashResult.toHexString())
|
||||
return hashResult
|
||||
|
||||
}
|
||||
|
@ -26,9 +26,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
|
||||
|
||||
}
|
||||
actual object Sha256StatelessDelegated : StatelessSha256 {
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
}
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
|
@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
|
||||
}
|
||||
|
||||
actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
}
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
|
@ -7,6 +7,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
*/
|
||||
|
||||
import com.ionspin.kotlin.crypto.Crypto
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@ -34,6 +35,6 @@ class Blake2bLinuxTest {
|
||||
|
||||
@Test
|
||||
fun testBlake2BStateless() = testBlocking {
|
||||
Blake2bDelegatedStateless.digest("test")
|
||||
Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 24-May-2020
|
||||
*/
|
||||
|
||||
import com.ionspin.kotlin.crypto.Crypto
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class Sha512DelegatedLinuxTest {
|
||||
|
||||
@Test
|
||||
fun testCinterop() {
|
||||
runBlocking {
|
||||
Crypto.initialize()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBlake2bUpdateable() = testBlocking {
|
||||
val blake2b = Crypto.Blake2b.updateable()
|
||||
blake2b.update("test")
|
||||
val result = blake2b.digest().toHexString()
|
||||
println(result)
|
||||
assertTrue { result.length > 2 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBlake2BStateless() = testBlocking {
|
||||
Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
|
||||
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
|
||||
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure
|
||||
@ -41,7 +42,7 @@ class ReadmeTest {
|
||||
@Test
|
||||
fun blake2bObjectExample() {
|
||||
val input = "abc"
|
||||
val result = Blake2bPure.digest(input)
|
||||
val result = Blake2bPure.digest(input.encodeToUByteArray())
|
||||
//@formatter:off
|
||||
val expectedResult = ubyteArrayOf(
|
||||
0xBAU,0x80U,0xA5U,0x3FU,0x98U,0x1CU,0x4DU,0x0DU,0x6AU,0x27U,0x97U,0xB6U,0x9FU,0x12U,0xF6U,0xE9U,
|
||||
@ -80,7 +81,7 @@ class ReadmeTest {
|
||||
@Test
|
||||
fun sha256Example() {
|
||||
val input = "abc"
|
||||
val result = Sha256Pure.digest(inputString = input)
|
||||
val result = Sha256Pure.digest(input.encodeToUByteArray())
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
@ -158,7 +159,7 @@ class ReadmeTest {
|
||||
|
||||
@Test
|
||||
fun debugTest() {
|
||||
val result = Blake2bStateless.digest("test")
|
||||
val result = Blake2bStateless.digest("test".encodeToUByteArray())
|
||||
println(result.toHexString())
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
@ -46,7 +47,7 @@ class Blake2BTest {
|
||||
"1234567890" +
|
||||
"1234567890"
|
||||
|
||||
val result = Blake2bPure.digest(test)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray())
|
||||
//Generated with b2sum 8.31
|
||||
val expectedResult = ubyteArrayOf(
|
||||
//@formatter:off
|
||||
@ -77,7 +78,7 @@ class Blake2BTest {
|
||||
"1234567890"
|
||||
|
||||
|
||||
val result = Blake2bPure.digest(test)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray())
|
||||
val expectedResultString = "800bb78cd4da18995c8074713bb674" +
|
||||
"3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" +
|
||||
"4ce1935a76154d7ea7c410493557741d18ec3a08da75"
|
||||
@ -93,7 +94,7 @@ class Blake2BTest {
|
||||
fun testDigest() {
|
||||
val test = "111111111122222222223333333333333"
|
||||
|
||||
val result = Blake2bPure.digest(test)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray())
|
||||
//Generated with b2sum 8.31
|
||||
val expectedResult = ubyteArrayOf(
|
||||
//@formatter:off
|
||||
@ -115,7 +116,7 @@ class Blake2BTest {
|
||||
val test = "abc"
|
||||
val key = "key"
|
||||
|
||||
val result = Blake2bPure.digest(test, key)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray(), key.encodeToUByteArray())
|
||||
|
||||
assertTrue {
|
||||
result.isNotEmpty()
|
||||
@ -134,7 +135,7 @@ class Blake2BTest {
|
||||
fun testDigestFromRfc() {
|
||||
val test = "abc"
|
||||
|
||||
val result = Blake2bPure.digest(test)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray())
|
||||
//@formatter:off
|
||||
val expectedResult = ubyteArrayOf(
|
||||
|
||||
@ -286,7 +287,7 @@ class Blake2BTest {
|
||||
fun testInvalidHashLength() {
|
||||
val test = "1234567890"
|
||||
assertFailsWith(RuntimeException::class) {
|
||||
val result = Blake2bPure.digest(inputString = test, hashLength = 65)
|
||||
val result = Blake2bPure.digest(test.encodeToUByteArray(), hashLength = 65)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,8 +58,8 @@ class Blake2bInstanceTest {
|
||||
fun testDigestToString() {
|
||||
val updates = 14
|
||||
val input = "1234567890"
|
||||
val expectedResult = "2F49AEB613E34E924E175A6AF2FAAD7BC78235F9C5E461C68FD5B47E".toLowerCase() +
|
||||
"E8E2FD2FB4C07D7E4A72404612D92899AF8A328F3B614ED77244B481151D40B11E32A4".toLowerCase()
|
||||
val expectedResult = "2F49AEB613E34E924E175A6AF2FAAD7BC78235F9C5E461C68FD5B407E".toLowerCase() +
|
||||
"E8E2F0D2FB4C07D7E4A72404612D92899AF8A328F3B614ED77244B481151D40B11E32A4".toLowerCase()
|
||||
|
||||
val blake2b = Blake2bPure()
|
||||
for (i in 0 until updates) {
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -31,7 +32,7 @@ class Sha256Test {
|
||||
@Test
|
||||
fun testWellKnownValue() {
|
||||
|
||||
val result = Sha256Pure.digest(inputString = "abc")
|
||||
val result = Sha256Pure.digest("abc".encodeToUByteArray())
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
@ -44,7 +45,7 @@ class Sha256Test {
|
||||
@Test
|
||||
fun testWellKnownDoubleBlock() {
|
||||
|
||||
val resultDoubleBlock = Sha256Pure.digest(inputString = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
|
||||
val resultDoubleBlock = Sha256Pure.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToUByteArray())
|
||||
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
@ -56,7 +57,7 @@ class Sha256Test {
|
||||
fun testWellKnown3() { //It's good that I'm consistent with names.
|
||||
|
||||
|
||||
val resultDoubleBlock = Sha256Pure.digest(inputString = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")
|
||||
val resultDoubleBlock = Sha256Pure.digest("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".encodeToUByteArray())
|
||||
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
|
||||
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
||||
assertTrue {
|
||||
|
Loading…
x
Reference in New Issue
Block a user