Short hash implementation and test, updated cbc tests as they ignored padding

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-28 22:35:33 +02:00
1 parent 86611453ea
commit 91cd41c8c1
12 files changed
+570 -27

No files matched your search

@@ -0,0 +1,15 @@
package com.ionspin.kotlin.crypto.shortinputhash
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
const val crypto_shorthash_KEYBYTES = 16
const val crypto_shorthash_BYTES = 8
expect object ShortHash {
fun shortHash(data : UByteArray, key: UByteArray) : UByteArray
fun shortHashKeygen() : UByteArray
}
@@ -1,10 +0,0 @@
package com.ionspin.kotlin.crypto.shortinputhash
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
object ShortInputHashing {
}
@@ -0,0 +1,24 @@
package com.ionspin.kotlin.crypto.util
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 28-Aug-2020
*/
fun String.hexStringToUByteArray() : UByteArray {
return this.chunked(2).map { it.toUByte(16) }.toUByteArray()
}
fun String.encodeToUByteArray() : UByteArray{
return encodeToByteArray().asUByteArray()
}
fun UByteArray.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}
@@ -0,0 +1,41 @@
package com.ionspin.kotlin.crypto.shorthash
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.shortinputhash.ShortHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 28-Aug-2020
*/
class ShortHashTest {
@Test
fun testShortHash() {
LibsodiumInitializer.initializeWithCallback {
val expected = "00e5d509c14e81bb".hexStringToUByteArray()
val input = "Libsodium test"
val key = "key1key1key1key1"
val hash = ShortHash.shortHash(input.encodeToUByteArray(), key.encodeToUByteArray())
println(hash.toHexString())
assertTrue {
expected.contentEquals(hash)
}
}
}
@Test
fun testKeygen() {
LibsodiumInitializer.initializeWithCallback {
assertTrue {
val key = ShortHash.shortHashKeygen()
key.size == 16
}
}
}
}
@@ -35,16 +35,4 @@ fun testBlocking(block : suspend () -> Unit) {
block.startCoroutine(continuation)
}
fun String.encodeToUByteArray() : UByteArray{
return encodeToByteArray().asUByteArray()
}
fun UByteArray.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}
@@ -26,6 +26,11 @@ interface JsSodiumInterface {
fun crypto_generichash_final(state: dynamic, hashLength: Int) : Uint8Array
//Short hash
fun crypto_shorthash(data : Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_shorthash_keygen() : Uint8Array
fun crypto_hash_sha256_init() : dynamic
@@ -0,0 +1,21 @@
package com.ionspin.kotlin.crypto.shortinputhash
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual object ShortHash {
actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray {
return getSodium().crypto_shorthash(data.toUInt8Array(), key.toUInt8Array()).toUByteArray()
}
actual fun shortHashKeygen(): UByteArray {
return getSodium().crypto_shorthash_keygen().toUByteArray()
}
}
@@ -0,0 +1,25 @@
package com.ionspin.kotlin.crypto.shortinputhash
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual object ShortHash {
actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray {
val hashResult = UByteArray(crypto_shorthash_BYTES)
sodium.crypto_shorthash(hashResult.asByteArray(), data.asByteArray(), data.size.toLong(), key.asByteArray())
return hashResult
}
actual fun shortHashKeygen(): UByteArray {
val key = UByteArray(crypto_shorthash_KEYBYTES)
sodium.crypto_shorthash_keygen(key.asByteArray())
return key
}
}
@@ -0,0 +1,35 @@
package com.ionspin.kotlin.crypto.shortinputhash
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
import libsodium.crypto_shorthash
import libsodium.crypto_shorthash_keygen
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual object ShortHash {
actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray {
val hashResult = UByteArray(crypto_shorthash_BYTES)
val hashResultPinned = hashResult.pin()
val dataPinned = data.pin()
val keyPinned = key.pin()
crypto_shorthash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert(), keyPinned.toPtr())
hashResultPinned.unpin()
dataPinned.unpin()
keyPinned.unpin()
return hashResult
}
actual fun shortHashKeygen(): UByteArray {
val keyResult = UByteArray(crypto_shorthash_KEYBYTES)
val keyResultPinned = keyResult.pin()
crypto_shorthash_keygen(keyResultPinned.toPtr())
keyResultPinned.unpin()
return keyResult
}
}