Added jvm and js implementation for sha256/512
This commit is contained in:
parent
15be707114
commit
1894a5d995
@ -15,7 +15,8 @@ expect class Sha512State
|
|||||||
|
|
||||||
expect object Hash {
|
expect object Hash {
|
||||||
|
|
||||||
fun hash(data: UByteArray) : UByteArray
|
//Not present in LazySodium
|
||||||
|
//fun hash(data: UByteArray) : UByteArray
|
||||||
|
|
||||||
fun sha256(data: UByteArray) : UByteArray
|
fun sha256(data: UByteArray) : UByteArray
|
||||||
fun sha256Init() : Sha256State
|
fun sha256Init() : Sha256State
|
||||||
|
@ -13,20 +13,21 @@ import kotlin.test.assertTrue
|
|||||||
* on 31-Aug-2020
|
* on 31-Aug-2020
|
||||||
*/
|
*/
|
||||||
class HashTest {
|
class HashTest {
|
||||||
@Test
|
//Not present in Lazy sodium
|
||||||
fun hashTest() {
|
// @Test
|
||||||
LibsodiumInitializer.initializeWithCallback {
|
// fun hashTest() {
|
||||||
val input = ("Input for various hash functions").encodeToUByteArray()
|
// LibsodiumInitializer.initializeWithCallback {
|
||||||
val expected = ("34fcbcdcfe9e6aa3e6d5a64649afcfafb449c4b8435a65e5e7b7c2b6af3b04da350acee" +
|
// val input = ("Input for various hash functions").encodeToUByteArray()
|
||||||
"838246d7c2637021def0c844fcb79ac42d6a50279f1078e535997b6e6").hexStringToUByteArray()
|
// val expected = ("34fcbcdcfe9e6aa3e6d5a64649afcfafb449c4b8435a65e5e7b7c2b6af3b04da350acee" +
|
||||||
|
// "838246d7c2637021def0c844fcb79ac42d6a50279f1078e535997b6e6").hexStringToUByteArray()
|
||||||
val result = Hash.hash(input)
|
//
|
||||||
assertTrue {
|
// val result = Hash.hash(input)
|
||||||
result.contentEquals(expected)
|
// assertTrue {
|
||||||
}
|
// result.contentEquals(expected)
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
}
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.hash
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.getSodium
|
||||||
|
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||||
|
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||||
|
|
||||||
|
actual typealias Sha256State = Any
|
||||||
|
actual typealias Sha512State = Any
|
||||||
|
|
||||||
|
actual object Hash {
|
||||||
|
|
||||||
|
//Not present in LazySodium
|
||||||
|
//fun hash(data: UByteArray) : UByteArray
|
||||||
|
actual fun sha256(data: UByteArray): UByteArray {
|
||||||
|
return getSodium().crypto_hash_sha256(data.toUInt8Array()).toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Init(): Sha256State {
|
||||||
|
return getSodium().crypto_hash_sha256_init()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Update(state: Sha256State, data: UByteArray) {
|
||||||
|
getSodium().crypto_hash_sha256_update(state, data.toUInt8Array())
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Final(state: Sha256State): UByteArray {
|
||||||
|
return getSodium().crypto_hash_sha256_final(state).toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512(data: UByteArray): UByteArray {
|
||||||
|
return getSodium().crypto_hash_sha512(data.toUInt8Array()).toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Init(): Sha512State {
|
||||||
|
return getSodium().crypto_hash_sha512_init()
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Update(state: Sha512State, data: UByteArray) {
|
||||||
|
getSodium().crypto_hash_sha512_update(state, data.toUInt8Array())
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Final(state: Sha512State): UByteArray {
|
||||||
|
return getSodium().crypto_hash_sha512_final(state).toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,39 +0,0 @@
|
|||||||
package com.ionspin.kotlin.crypto.hash
|
|
||||||
|
|
||||||
actual object Hash {
|
|
||||||
actual fun hash(data: UByteArray): UByteArray {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha256(data: UByteArray): UByteArray {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha256Init(): Sha256State {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha256Update(state: Sha256State, data: UByteArray) {
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha256Final(state: Sha256State): UByteArray {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha512(data: UByteArray): UByteArray {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha512Init(): Sha512State {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha512Update(state: Sha512State, data: UByteArray) {
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun sha512Final(state: Sha512State): UByteArray {
|
|
||||||
TODO("not implemented yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.hash
|
||||||
|
|
||||||
|
import com.goterl.lazycode.lazysodium.interfaces.Hash
|
||||||
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
|
||||||
|
|
||||||
|
actual typealias Sha256State = Hash.State256
|
||||||
|
actual typealias Sha512State = Hash.State512
|
||||||
|
|
||||||
|
actual object Hash {
|
||||||
|
|
||||||
|
actual fun sha256(data: UByteArray): UByteArray {
|
||||||
|
val resultHash = UByteArray(crypto_hash_sha256_BYTES)
|
||||||
|
sodium.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong())
|
||||||
|
return resultHash
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Init(): Sha256State {
|
||||||
|
val state = Hash.State256()
|
||||||
|
sodium.crypto_hash_sha256_init(state)
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Update(state: Sha256State, data: UByteArray) {
|
||||||
|
sodium.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong())
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha256Final(state: Sha256State): UByteArray {
|
||||||
|
val resultHash = UByteArray(crypto_hash_sha256_BYTES)
|
||||||
|
sodium.crypto_hash_sha256_final(state, resultHash.asByteArray())
|
||||||
|
return resultHash
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512(data: UByteArray): UByteArray {
|
||||||
|
val resultHash = UByteArray(crypto_hash_sha512_BYTES)
|
||||||
|
sodium.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong())
|
||||||
|
return resultHash
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Init(): Sha512State {
|
||||||
|
val state = Hash.State512()
|
||||||
|
sodium.crypto_hash_sha512_init(state)
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Update(state: Sha512State, data: UByteArray) {
|
||||||
|
sodium.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong())
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun sha512Final(state: Sha512State): UByteArray {
|
||||||
|
val resultHash = UByteArray(crypto_hash_sha512_BYTES)
|
||||||
|
sodium.crypto_hash_sha512_final(state, resultHash.asByteArray())
|
||||||
|
return resultHash
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -23,16 +23,17 @@ actual typealias Sha256State = crypto_hash_sha256_state
|
|||||||
actual typealias Sha512State = crypto_hash_sha512_state
|
actual typealias Sha512State = crypto_hash_sha512_state
|
||||||
|
|
||||||
actual object Hash {
|
actual object Hash {
|
||||||
actual fun hash(data: UByteArray): UByteArray {
|
//Not present in Lazy Sodium
|
||||||
val hashResult = UByteArray(crypto_hash_BYTES)
|
// actual fun hash(data: UByteArray): UByteArray {
|
||||||
val hashResultPinned = hashResult.pin()
|
// val hashResult = UByteArray(crypto_hash_BYTES)
|
||||||
val dataPinned = data.pin()
|
// val hashResultPinned = hashResult.pin()
|
||||||
crypto_hash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert())
|
// val dataPinned = data.pin()
|
||||||
hashResultPinned.unpin()
|
// crypto_hash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert())
|
||||||
dataPinned.unpin()
|
// hashResultPinned.unpin()
|
||||||
|
// dataPinned.unpin()
|
||||||
return hashResult
|
//
|
||||||
}
|
// return hashResult
|
||||||
|
// }
|
||||||
|
|
||||||
actual fun sha256(data: UByteArray): UByteArray {
|
actual fun sha256(data: UByteArray): UByteArray {
|
||||||
val hashResult = UByteArray(crypto_hash_sha256_BYTES)
|
val hashResult = UByteArray(crypto_hash_sha256_BYTES)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user