Multipart API continuation

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-23 19:54:30 +02:00
1 parent f107db3312
commit 233ee1bf55
14 files changed
+226 -102

No files matched your search

@@ -0,0 +1,36 @@
package com.ionspin.kotlin.crypto.authenticated
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020
*/
interface AuthenticatedEncryption {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray
}
data class EncryptedDataPart(val data : UByteArray)
data class DecryptedDataPart(val data : UByteArray)
data class MultipartEncryptedDataDescriptor(val data: UByteArray, val nonce: UByteArray)
interface MultipartAuthenticatedVerification {
fun verifyPartialData(data: EncryptedDataPart)
fun finalizeVerificationAndPrepareDecryptor() : MultipartAuthenticatedDecryption
}
interface MultipartAuthenticatedDecryption {
fun decryptPartialData(data: EncryptedDataPart) : DecryptedDataPart
}
interface MultipartAuthenticatedEncryption {
fun encryptPartialData(data: UByteArray) : EncryptedDataPart
fun finish() : MultipartEncryptedDataDescriptor
}
@@ -22,12 +22,12 @@ package com.ionspin.kotlin.crypto.hash
* ugljesa.jovanovic@ionspin.com
* on 20-Jul-2019
*/
interface Hash {
interface HashFunction {
val MAX_HASH_BYTES : Int
}
interface UpdatableHash : Hash {
interface MultiPartHash : HashFunction {
fun update(data : UByteArray)
fun digest() : UByteArray
@@ -35,7 +35,7 @@ interface UpdatableHash : Hash {
}
interface StatelessHash : Hash {
interface Hash : HashFunction {
}
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.hash.StatelessHash
import com.ionspin.kotlin.crypto.hash.UpdatableHash
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
/**
* Created by Ugljesa Jovanovic
@@ -13,12 +13,12 @@ object Blake2bProperties {
const val MAX_HASH_BYTES = 64
}
interface Blake2b : UpdatableHash {
interface Blake2b : MultiPartHash {
override val MAX_HASH_BYTES: Int
get() = Blake2bProperties.MAX_HASH_BYTES
}
interface Blake2bStateless : StatelessHash {
interface Blake2bStateless : Hash {
override val MAX_HASH_BYTES: Int
get() = Blake2bProperties.MAX_HASH_BYTES
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.StatelessHash
import com.ionspin.kotlin.crypto.hash.UpdatableHash
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
/**
* Created by Ugljesa Jovanovic
@@ -12,11 +12,11 @@ object Sha256Properties {
const val MAX_HASH_BYTES = 32
}
interface Sha256 : UpdatableHash {
interface Sha256 : MultiPartHash {
override val MAX_HASH_BYTES: Int
get() = Sha256Properties.MAX_HASH_BYTES
}
interface StatelessSha256 : StatelessHash {
interface StatelessSha256 : Hash {
override val MAX_HASH_BYTES: Int
get() = Sha256Properties.MAX_HASH_BYTES
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.StatelessHash
import com.ionspin.kotlin.crypto.hash.UpdatableHash
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
/**
* Created by Ugljesa Jovanovic
@@ -11,11 +11,11 @@ import com.ionspin.kotlin.crypto.hash.UpdatableHash
object Sha512Properties {
const val MAX_HASH_BYTES = 64
}
interface Sha512 : UpdatableHash {
interface Sha512 : MultiPartHash {
override val MAX_HASH_BYTES: Int
get() = Sha256Properties.MAX_HASH_BYTES
}
interface StatelessSha512 : StatelessHash {
interface StatelessSha512 : Hash {
override val MAX_HASH_BYTES: Int
get() = Sha512Properties.MAX_HASH_BYTES
@@ -0,0 +1,7 @@
package com.ionspin.kotlin.crypto.util
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020
*/
@@ -0,0 +1,18 @@
package com.ionspin.kotlin.crypto.util
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020
*/
fun UByteArray.overwriteWithZeroes() {
for (i in 0 until size) {
this[i] = 0U
}
}
fun UIntArray.overwriteWithZeroes() {
for (i in 0 until size) {
this[i] = 0U
}
}
@@ -0,0 +1,56 @@
package com.ionspin.kotlin.crypto.util
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020
*/
fun Array<Byte>.hexColumsPrint() {
val printout = this.map { it.toString(16) }.chunked(16)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun Array<UByte>.hexColumsPrint(chunk : Int = 16) {
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun UByteArray.hexColumsPrint(chunk : Int = 16) {
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun Array<ULong>.hexColumsPrint(chunk: Int = 3) {
val printout = this.map { it.toString(16) }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun String.hexStringToTypedUByteArray() : Array<UByte> {
return this.chunked(2).map { it.toUByte(16) }.toTypedArray()
}
fun String.hexStringToUByteArray() : UByteArray {
return this.chunked(2).map { it.toUByte(16) }.toUByteArray()
}
fun Array<UByte>.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}
fun UByteArray.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}