Use unsafe cast instead of conversion that creates new array

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-07-07 21:47:10 +02:00
1 parent 9751f80347
commit 5d3c14de2f
26 files changed
+101 -86

No files matched your search

@@ -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) {