Use unsafe cast instead of conversion that creates new array
This commit is contained in:
parent
9751f80347
commit
5d3c14de2f
@ -40,6 +40,6 @@ interface Hash : HashFunction {
|
||||
}
|
||||
|
||||
fun String.encodeToUByteArray() : UByteArray{
|
||||
return encodeToByteArray().toUByteArray()
|
||||
return encodeToByteArray().asUByteArray()
|
||||
}
|
||||
|
||||
|
@ -53,4 +53,4 @@ fun UByteArray.toHexString() : String {
|
||||
it.toString(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,6 @@ actual object SRNG {
|
||||
actual fun getRandomBytes(amount: Int): UByteArray {
|
||||
val byteArray = ByteArray(amount)
|
||||
secureRandom.nextBytes(byteArray)
|
||||
return byteArray.toUByteArray()
|
||||
return byteArray.asUByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
||||
key.toByteArray()
|
||||
|
||||
)
|
||||
return ciphertext.toUByteArray()
|
||||
return ciphertext.asUByteArray()
|
||||
}
|
||||
|
||||
actual fun decrypt(
|
||||
@ -52,7 +52,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
||||
key.toByteArray()
|
||||
|
||||
)
|
||||
return message.toUByteArray()
|
||||
return message.asUByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLengt
|
||||
override fun digest(): UByteArray {
|
||||
val hashed = ByteArray(hashLength)
|
||||
sodium.crypto_generichash_final(state, hashed, hashLength)
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
@ -34,7 +34,7 @@ actual object Blake2bDelegatedStateless : Blake2b {
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
val hashed = ByteArray(hashLength)
|
||||
sodium.crypto_generichash(hashed, hashed.size, inputMessage.toByteArray(), inputMessage.size.toLong(), key.toByteArray(), key.size)
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ actual class Sha256Delegated actual constructor() : Sha256 {
|
||||
override fun digest(): UByteArray {
|
||||
val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES)
|
||||
sodium.crypto_hash_sha256_final(state, hashed)
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
@ -36,6 +36,6 @@ actual object Sha256StatelessDelegated : StatelessSha256 {
|
||||
override fun digest(inputMessage: UByteArray): UByteArray {
|
||||
val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES)
|
||||
sodium.crypto_hash_sha256(hashed, inputMessage.toByteArray(), inputMessage.size.toLong())
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ actual class Sha512Delegated : Sha512Multipart {
|
||||
override fun digest(): UByteArray {
|
||||
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
|
||||
Initializer.sodium.crypto_hash_sha512_final(state, hashed)
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
@ -35,6 +35,6 @@ actual object Sha512StatelessDelegated : Sha512 {
|
||||
override fun digest(inputMessage: UByteArray): UByteArray {
|
||||
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
|
||||
Initializer.sodium.crypto_hash_sha512(hashed, inputMessage.toByteArray(), inputMessage.size.toLong())
|
||||
return hashed.toUByteArray()
|
||||
return hashed.asUByteArray()
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
||||
pointer[i] = testState[i]
|
||||
}
|
||||
println("state after setting-----------")
|
||||
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint()
|
||||
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).asUByteArray().hexColumsPrint()
|
||||
println("state after setting-----------")
|
||||
println("header after setting-----------")
|
||||
testHeader.copyInto(header)
|
||||
@ -92,7 +92,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
||||
val pinnedHeader = header.pin()
|
||||
crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.addressOf(0), key.toCValues())
|
||||
println("state-----------")
|
||||
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint()
|
||||
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).asUByteArray().hexColumsPrint()
|
||||
println("state-----------")
|
||||
println("--------header-----------")
|
||||
header.hexColumsPrint()
|
||||
|
@ -20,7 +20,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
|
||||
requestedHashLength = hashLength
|
||||
val allocated = malloc(crypto_generichash_state.size.convert())!!
|
||||
state = allocated.reinterpret<crypto_generichash_state>().pointed
|
||||
crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert())
|
||||
crypto_generichash_init(state.ptr, key?.run { this.toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert())
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
|
@ -19,6 +19,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import com.ionspin.kotlin.bignum.integer.toBigInteger
|
||||
import com.ionspin.kotlin.crypto.*
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.rotateRight
|
||||
|
||||
/**
|
||||
@ -146,9 +147,9 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
|
||||
|
||||
|
||||
fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
|
||||
val array = inputString.encodeToByteArray().toUByteArray()
|
||||
val array = inputString.encodeToUByteArray()
|
||||
val keyBytes = key?.run {
|
||||
encodeToByteArray().toUByteArray()
|
||||
encodeToUByteArray()
|
||||
} ?: ubyteArrayOf()
|
||||
return digest(inputMessage = array, key = keyBytes, hashLength = hashLength)
|
||||
|
||||
@ -248,7 +249,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
|
||||
key: String?,
|
||||
requestedHashLenght: Int = 64
|
||||
) : this(
|
||||
(key?.encodeToByteArray()?.toUByteArray() ?: ubyteArrayOf()),
|
||||
(key?.encodeToUByteArray() ?: ubyteArrayOf()),
|
||||
requestedHashLenght
|
||||
)
|
||||
|
||||
@ -310,7 +311,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
|
||||
}
|
||||
|
||||
fun update(data: String) {
|
||||
update(data.encodeToByteArray().toUByteArray())
|
||||
update(data.encodeToUByteArray())
|
||||
}
|
||||
|
||||
private fun appendToBuffer(array: UByteArray, start: Int) {
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.rotateRight
|
||||
|
||||
|
||||
@ -236,7 +237,7 @@ class Sha256Pure : Sha256 {
|
||||
|
||||
|
||||
fun update(data: String) {
|
||||
return update(data.encodeToByteArray().toUByteArray())
|
||||
return update(data.encodeToUByteArray())
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
@ -311,4 +312,4 @@ class Sha256Pure : Sha256 {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.rotateRight
|
||||
|
||||
/**
|
||||
@ -310,7 +311,7 @@ class Sha512Pure : Sha512Multipart {
|
||||
|
||||
|
||||
fun update(data: String) {
|
||||
return update(data.encodeToByteArray().toUByteArray())
|
||||
return update(data.encodeToUByteArray())
|
||||
}
|
||||
|
||||
override fun update(data: UByteArray) {
|
||||
|
@ -22,6 +22,7 @@ import com.ionspin.kotlin.bignum.integer.toBigInteger
|
||||
|
||||
import com.ionspin.kotlin.crypto.SRNG
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.keyderivation.ArgonResult
|
||||
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash
|
||||
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG
|
||||
@ -74,14 +75,14 @@ class Argon2Pure(
|
||||
): ArgonResult {
|
||||
val salt = SRNG.getRandomBytes(64)
|
||||
val argon = Argon2Pure(
|
||||
password.encodeToByteArray().toUByteArray(),
|
||||
password.encodeToUByteArray(),
|
||||
salt,
|
||||
parallelism,
|
||||
tagLength.toUInt(),
|
||||
memory.toUInt(),
|
||||
numberOfIterations,
|
||||
key.encodeToByteArray().toUByteArray(),
|
||||
associatedData.encodeToByteArray().toUByteArray(),
|
||||
key.encodeToUByteArray(),
|
||||
associatedData.encodeToUByteArray(),
|
||||
ArgonType.Argon2id
|
||||
)
|
||||
val resultArray = argon.derive()
|
||||
@ -100,14 +101,14 @@ class Argon2Pure(
|
||||
associatedData: String = "",
|
||||
argonType: ArgonType = ArgonType.Argon2id
|
||||
) : this(
|
||||
password.encodeToByteArray().toUByteArray(),
|
||||
salt.encodeToByteArray().toUByteArray(),
|
||||
password.encodeToUByteArray(),
|
||||
salt.encodeToUByteArray(),
|
||||
parallelism,
|
||||
tagLength,
|
||||
requestedMemorySize,
|
||||
numberOfIterations,
|
||||
key.encodeToByteArray().toUByteArray(),
|
||||
associatedData.encodeToByteArray().toUByteArray(),
|
||||
key.encodeToUByteArray(),
|
||||
associatedData.encodeToUByteArray(),
|
||||
argonType
|
||||
)
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 13-Jun-2020
|
||||
*/
|
||||
internal sealed class InternalAesKey(val key: String, val keyLength: Int) {
|
||||
val keyArray: UByteArray = key.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val keyArray: UByteArray = key.hexStringToUByteArray()
|
||||
val numberOf32BitWords = keyLength / 32
|
||||
|
||||
class Aes128Key(key: String) : InternalAesKey(key, 128)
|
||||
|
@ -57,7 +57,7 @@ internal class AesCtrPure internal constructor(val aesKey: InternalAesKey, val m
|
||||
return AesCtrPure(aesKey, Mode.DECRYPT)
|
||||
}
|
||||
/**
|
||||
* Bulk encryption, returns encrypted data and a random initial counter
|
||||
* Bulk encryption, returns encrypted data and a random initial counter
|
||||
*/
|
||||
fun encrypt(aesKey: InternalAesKey, data: UByteArray): EncryptedDataAndInitialCounter {
|
||||
val aesCtr = AesCtrPure(aesKey, Mode.ENCRYPT)
|
||||
@ -161,7 +161,7 @@ internal class AesCtrPure internal constructor(val aesKey: InternalAesKey, val m
|
||||
}
|
||||
|
||||
private fun consumeBlock(data: UByteArray, blockCount: ModularBigInteger): UByteArray {
|
||||
val blockCountAsByteArray = blockCount.toUByteArray(Endianness.BIG).toUByteArray().expandCounterTo16Bytes()
|
||||
val blockCountAsByteArray = blockCount.toUByteArray(Endianness.BIG).expandCounterTo16Bytes()
|
||||
return when (mode) {
|
||||
Mode.ENCRYPT -> {
|
||||
AesPure.encrypt(aesKey, blockCountAsByteArray) xor data
|
||||
|
@ -22,6 +22,7 @@ import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
|
||||
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
|
||||
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure
|
||||
import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlin.test.Test
|
||||
@ -70,7 +71,7 @@ class ReadmeTest {
|
||||
}
|
||||
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
|
||||
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
|
||||
.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
.hexStringToUByteArray()
|
||||
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult)
|
||||
@ -84,7 +85,7 @@ class ReadmeTest {
|
||||
val result = Sha256Pure.digest(input.encodeToUByteArray())
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
@ -94,12 +95,12 @@ class ReadmeTest {
|
||||
@Test
|
||||
fun sha512Example() {
|
||||
val input = "abc"
|
||||
val result = Sha512Pure.digest(inputMessage = input.encodeToByteArray().map { it.toUByte() }.toUByteArray())
|
||||
val result = Sha512Pure.digest(inputMessage = input.encodeToUByteArray())
|
||||
println(result.map { it.toString(16) })
|
||||
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
|
||||
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
@ -113,7 +114,7 @@ class ReadmeTest {
|
||||
val result = sha256.digest()
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,7 +127,7 @@ class ReadmeTest {
|
||||
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
|
||||
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
@ -82,7 +83,7 @@ class Blake2BTest {
|
||||
val expectedResultString = "800bb78cd4da18995c8074713bb674" +
|
||||
"3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" +
|
||||
"4ce1935a76154d7ea7c410493557741d18ec3a08da75"
|
||||
val expectedResult = expectedResultString.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val expectedResult = expectedResultString.hexStringToUByteArray()
|
||||
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult)
|
||||
@ -123,7 +124,7 @@ class Blake2BTest {
|
||||
}
|
||||
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
|
||||
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
|
||||
.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
.hexStringToUByteArray()
|
||||
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult)
|
||||
@ -291,4 +292,4 @@ class Blake2BTest {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
@ -84,10 +85,10 @@ class Blake2bInstanceTest {
|
||||
}
|
||||
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
|
||||
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
|
||||
.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
.hexStringToUByteArray()
|
||||
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -36,13 +37,13 @@ class Blake2bKnowAnswerTests {
|
||||
@Test
|
||||
fun knownAnswerTest() {
|
||||
kat.forEach {
|
||||
val parsedInput = it.input.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val parsedInput = it.input.hexStringToUByteArray()
|
||||
val result = Blake2bPure.digest(
|
||||
inputMessage = parsedInput,
|
||||
key = it.key.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
key = it.key.hexStringToUByteArray()
|
||||
)
|
||||
assertTrue {
|
||||
result.contentEquals(it.hash.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(it.hash.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -51,13 +52,13 @@ class Blake2bKnowAnswerTests {
|
||||
fun knownAnswerTestInstance() {
|
||||
|
||||
kat.forEach { kat ->
|
||||
val parsedInput = kat.input.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val parsedInput = kat.input.hexStringToUByteArray()
|
||||
val chunkedInput = parsedInput.toList().chunked(128).map { it.toUByteArray() }
|
||||
val blake2b = Blake2bPure(key = kat.key.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
val blake2b = Blake2bPure(key = kat.key.hexStringToUByteArray())
|
||||
chunkedInput.forEach { blake2b.update(it) }
|
||||
val result = blake2b.digest()
|
||||
assertTrue("KAT ${kat.input} \nkey: ${kat.key} \nexpected: {${kat.hash}") {
|
||||
result.contentEquals(kat.hash.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(kat.hash.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -35,7 +36,7 @@ class Sha256Test {
|
||||
val result = Sha256Pure.digest("abc".encodeToUByteArray())
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
@ -48,7 +49,7 @@ class Sha256Test {
|
||||
val resultDoubleBlock = Sha256Pure.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToUByteArray())
|
||||
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +62,7 @@ class Sha256Test {
|
||||
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
|
||||
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ class Sha256Test {
|
||||
val resultDoubleBlock = Sha256Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray())
|
||||
val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Ignore
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
@ -36,7 +37,7 @@ class Sha256UpdatableTest {
|
||||
val result = sha256.digest()
|
||||
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,7 +50,7 @@ class Sha256UpdatableTest {
|
||||
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
|
||||
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +63,7 @@ class Sha256UpdatableTest {
|
||||
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
|
||||
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,7 +77,7 @@ class Sha256UpdatableTest {
|
||||
val resultDoubleBlock = sha256.digest()
|
||||
val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +95,7 @@ class Sha256UpdatableTest {
|
||||
val resultDoubleBlock = sha256.digest()
|
||||
val expectedResultForDoubleBlock = "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -35,7 +36,7 @@ class Sha512Test {
|
||||
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
|
||||
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
@ -51,7 +52,7 @@ class Sha512Test {
|
||||
val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
|
||||
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,7 +66,7 @@ class Sha512Test {
|
||||
val resultDoubleBlock = Sha512Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray())
|
||||
val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Ignore
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
@ -35,7 +36,7 @@ class Sha512UpdatableTest {
|
||||
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
|
||||
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
|
||||
|
||||
@ -51,7 +52,7 @@ class Sha512UpdatableTest {
|
||||
val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
|
||||
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,7 +66,7 @@ class Sha512UpdatableTest {
|
||||
val resultDoubleBlock = sha512.digest()
|
||||
val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,7 +84,7 @@ class Sha512UpdatableTest {
|
||||
val resultDoubleBlock = sha512.digest()
|
||||
val expectedResultForDoubleBlock = "b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086"
|
||||
assertTrue {
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray())
|
||||
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -183,10 +184,10 @@ class AesTest {
|
||||
val key = "2b7e151628aed2a6abf7158809cf4f3c"
|
||||
val expectedResult = "3925841d02dc09fbdc118597196a0b32"
|
||||
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), input.hexStringToUByteArray())
|
||||
val result = aes.encrypt()
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
result.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
@ -196,11 +197,11 @@ class AesTest {
|
||||
val input = "3243f6a8885a308d313198a2e0370734"
|
||||
val key = "2b7e151628aed2a6abf7158809cf4f3c"
|
||||
val expectedResult = "3925841d02dc09fbdc118597196a0b32"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val original = input.hexStringToUByteArray()
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), original)
|
||||
val encrypted = aes.encrypt()
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
encrypted.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
|
||||
|
||||
@ -210,11 +211,11 @@ class AesTest {
|
||||
val input = "00112233445566778899aabbccddeeff"
|
||||
val key = "000102030405060708090a0b0c0d0e0f"
|
||||
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val original = input.hexStringToUByteArray()
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), original)
|
||||
val encrypted = aes.encrypt()
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
encrypted.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
val aesDec = AesPure(InternalAesKey.Aes128Key(key), encrypted)
|
||||
val decrypted = aesDec.decrypt()
|
||||
@ -228,10 +229,10 @@ class AesTest {
|
||||
val input = "00112233445566778899aabbccddeeff"
|
||||
val key = "000102030405060708090a0b0c0d0e0f"
|
||||
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val original = input.hexStringToUByteArray()
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes128Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
encrypted.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
@ -241,10 +242,10 @@ class AesTest {
|
||||
val input = "00112233445566778899aabbccddeeff"
|
||||
val key = "000102030405060708090a0b0c0d0e0f1011121314151617"
|
||||
val expectedResult = "dda97ca4864cdfe06eaf70a0ec0d7191"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val original = input.hexStringToUByteArray()
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes192Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
encrypted.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes192Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
@ -254,10 +255,10 @@ class AesTest {
|
||||
val input = "00112233445566778899aabbccddeeff"
|
||||
val key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
|
||||
val expectedResult = "8ea2b7ca516745bfeafc49904b496089"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val original = input.hexStringToUByteArray()
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes256Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
encrypted.contentEquals(expectedResult.hexStringToUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes256Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
@ -266,4 +267,4 @@ class AesTest {
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,6 @@ actual object SRNG {
|
||||
actual fun getRandomBytes(amount: Int): UByteArray {
|
||||
val byteArray = ByteArray(amount)
|
||||
secureRandom.nextBytes(byteArray)
|
||||
return byteArray.toUByteArray()
|
||||
return byteArray.asUByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ object Sample {
|
||||
blake2bUpdateable.update("test".encodeToUByteArray())
|
||||
println(blake2bUpdateable.digest().toHexString())
|
||||
println("Blake2b stateless")
|
||||
val statelessResult = CryptoPrimitives.Blake2b.stateless("test".encodeToByteArray().toUByteArray())
|
||||
val statelessResult = CryptoPrimitives.Blake2b.stateless("test".encodeToUByteArray())
|
||||
println("Blake2b stateless: ${statelessResult.toHexString()}")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user