Api cleanup

This commit is contained in:
Ugljesa Jovanovic 2020-06-07 00:37:09 +02:00 committed by Ugljesa Jovanovic
parent 25adc330bd
commit f03e94cf5e
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
15 changed files with 91 additions and 83 deletions

View File

@ -46,3 +46,7 @@ interface StatelessHash : Hash {
): UByteArray ): UByteArray
} }
fun String.encodeToUByteArray() : UByteArray{
return encodeToByteArray().toUByteArray()
}

View File

@ -9,7 +9,7 @@ import org.khronos.webgl.Uint8Array
*/ */
interface JsSodiumInterface { interface JsSodiumInterface {
fun crypto_generichash(hashLength: Int, inputMessage: String) : Uint8Array fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
fun randombytes_buf(numberOfBytes: Int) : Uint8Array fun randombytes_buf(numberOfBytes: Int) : Uint8Array

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import org.khronos.webgl.Uint8Array
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -33,36 +34,17 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
actual object Blake2bDelegatedStateless : Blake2bStateless { actual object Blake2bDelegatedStateless : Blake2bStateless {
override val MAX_HASH_BYTES: Int = 64 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 { 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
} }

View File

@ -23,9 +23,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
} }
actual object Sha256StatelessDelegated : StatelessSha256 { 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 { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")

View File

@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
} }
actual object Sha512StatelessDelegated : StatelessSha512 { 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 { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.Crypto 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.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test import kotlin.test.Test
@ -18,18 +19,9 @@ class Blake2bJsTest {
@Test @Test
fun testBlake2BSodiumInterop() = testBlocking { fun testBlake2BSodiumInterop() = testBlocking {
Crypto.initialize() Crypto.initialize()
val hash = Blake2bDelegatedStateless.digest("test") val hash = Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" + assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572") "83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
} }
@Test
fun testBlake2BSodiumBlockingInterop() = testBlocking {
Crypto.initialize()
val hash = Blake2bDelegatedStateless.digestBlocking("test", null, 64)
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
}
} }

View File

@ -50,20 +50,20 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") @Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
actual object Blake2bDelegatedStateless : Blake2bStateless { actual object Blake2bDelegatedStateless : Blake2bStateless {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { // override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
println("Input $inputString, ${key ?: "null"}, $hashLength") // println("Input $inputString, ${key ?: "null"}, $hashLength")
val hashResult = UByteArray(MAX_HASH_BYTES) // val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin() // val hashResultPinned = hashResult.pin()
crypto_generichash( // crypto_generichash(
hashResultPinned.addressOf(0), // hashResultPinned.addressOf(0),
hashLength.convert(), // hashLength.convert(),
inputString.encodeToByteArray().toUByteArray().toCValues(), // inputString.encodeToByteArray().toUByteArray().toCValues(),
inputString.length.convert(), // inputString.length.convert(),
key?.run { this.encodeToByteArray().toUByteArray().toCValues() }, // key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
key?.length?.convert() ?: 0UL // key?.length?.convert() ?: 0UL
) // )
return hashResult // return hashResult
} // }
@ -79,8 +79,6 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
key.toCValues(), key.toCValues(),
key.size.convert() ?: 0UL key.size.convert() ?: 0UL
) )
println("HashPointer: ${hashResult.toHexString()}")
println(hashResult.toHexString())
return hashResult return hashResult
} }

View File

@ -26,9 +26,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
} }
actual object Sha256StatelessDelegated : StatelessSha256 { 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 { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")

View File

@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
} }
actual object Sha512StatelessDelegated : StatelessSha512 { 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 { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")

View File

@ -7,6 +7,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
*/ */
import com.ionspin.kotlin.crypto.Crypto 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.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -34,6 +35,6 @@ class Blake2bLinuxTest {
@Test @Test
fun testBlake2BStateless() = testBlocking { fun testBlake2BStateless() = testBlocking {
Blake2bDelegatedStateless.digest("test") Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
} }
} }

View File

@ -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())
}
}

View File

@ -17,6 +17,7 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure 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.Sha256Pure
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure
@ -41,7 +42,7 @@ class ReadmeTest {
@Test @Test
fun blake2bObjectExample() { fun blake2bObjectExample() {
val input = "abc" val input = "abc"
val result = Blake2bPure.digest(input) val result = Blake2bPure.digest(input.encodeToUByteArray())
//@formatter:off //@formatter:off
val expectedResult = ubyteArrayOf( val expectedResult = ubyteArrayOf(
0xBAU,0x80U,0xA5U,0x3FU,0x98U,0x1CU,0x4DU,0x0DU,0x6AU,0x27U,0x97U,0xB6U,0x9FU,0x12U,0xF6U,0xE9U, 0xBAU,0x80U,0xA5U,0x3FU,0x98U,0x1CU,0x4DU,0x0DU,0x6AU,0x27U,0x97U,0xB6U,0x9FU,0x12U,0xF6U,0xE9U,
@ -80,7 +81,7 @@ class ReadmeTest {
@Test @Test
fun sha256Example() { fun sha256Example() {
val input = "abc" val input = "abc"
val result = Sha256Pure.digest(inputString = input) val result = Sha256Pure.digest(input.encodeToUByteArray())
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
@ -158,7 +159,7 @@ class ReadmeTest {
@Test @Test
fun debugTest() { fun debugTest() {
val result = Blake2bStateless.digest("test") val result = Blake2bStateless.digest("test".encodeToUByteArray())
println(result.toHexString()) println(result.toHexString())
} }

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -46,7 +47,7 @@ class Blake2BTest {
"1234567890" + "1234567890" +
"1234567890" "1234567890"
val result = Blake2bPure.digest(test) val result = Blake2bPure.digest(test.encodeToUByteArray())
//Generated with b2sum 8.31 //Generated with b2sum 8.31
val expectedResult = ubyteArrayOf( val expectedResult = ubyteArrayOf(
//@formatter:off //@formatter:off
@ -77,7 +78,7 @@ class Blake2BTest {
"1234567890" "1234567890"
val result = Blake2bPure.digest(test) val result = Blake2bPure.digest(test.encodeToUByteArray())
val expectedResultString = "800bb78cd4da18995c8074713bb674" + val expectedResultString = "800bb78cd4da18995c8074713bb674" +
"3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" + "3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" +
"4ce1935a76154d7ea7c410493557741d18ec3a08da75" "4ce1935a76154d7ea7c410493557741d18ec3a08da75"
@ -93,7 +94,7 @@ class Blake2BTest {
fun testDigest() { fun testDigest() {
val test = "111111111122222222223333333333333" val test = "111111111122222222223333333333333"
val result = Blake2bPure.digest(test) val result = Blake2bPure.digest(test.encodeToUByteArray())
//Generated with b2sum 8.31 //Generated with b2sum 8.31
val expectedResult = ubyteArrayOf( val expectedResult = ubyteArrayOf(
//@formatter:off //@formatter:off
@ -115,7 +116,7 @@ class Blake2BTest {
val test = "abc" val test = "abc"
val key = "key" val key = "key"
val result = Blake2bPure.digest(test, key) val result = Blake2bPure.digest(test.encodeToUByteArray(), key.encodeToUByteArray())
assertTrue { assertTrue {
result.isNotEmpty() result.isNotEmpty()
@ -134,7 +135,7 @@ class Blake2BTest {
fun testDigestFromRfc() { fun testDigestFromRfc() {
val test = "abc" val test = "abc"
val result = Blake2bPure.digest(test) val result = Blake2bPure.digest(test.encodeToUByteArray())
//@formatter:off //@formatter:off
val expectedResult = ubyteArrayOf( val expectedResult = ubyteArrayOf(
@ -286,7 +287,7 @@ class Blake2BTest {
fun testInvalidHashLength() { fun testInvalidHashLength() {
val test = "1234567890" val test = "1234567890"
assertFailsWith(RuntimeException::class) { assertFailsWith(RuntimeException::class) {
val result = Blake2bPure.digest(inputString = test, hashLength = 65) val result = Blake2bPure.digest(test.encodeToUByteArray(), hashLength = 65)
} }
} }

View File

@ -58,8 +58,8 @@ class Blake2bInstanceTest {
fun testDigestToString() { fun testDigestToString() {
val updates = 14 val updates = 14
val input = "1234567890" val input = "1234567890"
val expectedResult = "2F49AEB613E34E924E175A6AF2FAAD7BC78235F9C5E461C68FD5B47E".toLowerCase() + val expectedResult = "2F49AEB613E34E924E175A6AF2FAAD7BC78235F9C5E461C68FD5B407E".toLowerCase() +
"E8E2FD2FB4C07D7E4A72404612D92899AF8A328F3B614ED77244B481151D40B11E32A4".toLowerCase() "E8E2F0D2FB4C07D7E4A72404612D92899AF8A328F3B614ED77244B481151D40B11E32A4".toLowerCase()
val blake2b = Blake2bPure() val blake2b = Blake2bPure()
for (i in 0 until updates) { for (i in 0 until updates) {

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -31,7 +32,7 @@ class Sha256Test {
@Test @Test
fun testWellKnownValue() { fun testWellKnownValue() {
val result = Sha256Pure.digest(inputString = "abc") val result = Sha256Pure.digest("abc".encodeToUByteArray())
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
@ -44,7 +45,7 @@ class Sha256Test {
@Test @Test
fun testWellKnownDoubleBlock() { fun testWellKnownDoubleBlock() {
val resultDoubleBlock = Sha256Pure.digest(inputString = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") val resultDoubleBlock = Sha256Pure.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToUByteArray())
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) 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. 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 = "")) println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
assertTrue { assertTrue {