diff --git a/README.md b/README.md index f232204..0aeaf5b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This is an extremely early release, currently only consisting of Blake2b and SHA **The API will move fast and break often until v1.0** -Make SHA hashes "updateable" like Blake2b +Make SHA hashes "updatable" like Blake2b After that tenative plan is to add 25519 curve based signing and key exchange next. @@ -48,11 +48,17 @@ implementation("com.ionspin.kotlin:crypto:0.0.1-SNAPSHOT") ## Usage -### Blake2b +### Hashes + +Hashes are provided in two versions, "stateless", usually the companion object of the hash, +which takes the data to be hashed in one go, and "updatable" which can be fed data in chunks. + + +#### Blake2b You can use Blake 2b in two modes -#### Using a `Blake2b` object +##### Stateless version You need to deliver the complete data that is to be hashed in one go ```kotlin @@ -62,9 +68,9 @@ val result = Blake2b.digest(input) Result is returned as a `Array` -#### Using a `Blake2b` instance +##### Updatable instance version You can create an instance and feed the data by using `update(input : Array)` call. Once all data is supplied, -you should call `digest()` or `digestString()` convinence method that converts the `Array` into hexadecimal string. +you should call `digest()` or `digestString()` convenience method that converts the `Array` into hexadecimal string. If you want to use Blake2b with a key, you should supply it when creating the `Blake2b` instance. @@ -77,7 +83,9 @@ val result = blake2b.digest() ``` After digest is called, the instance is reset and can be reused (Keep in mind key stays the same for the particular instance). -### SHA2 (SHA256 and SHA512) +#### SHA2 (SHA256 and SHA512) + +##### Stateless version You need to deliver the complete data that is to be hashed in one go. You can either provide the `Array` as input or `String`. Result is always returned as `Array` (At least in verision 0.0.1) @@ -94,6 +102,22 @@ val result = Sha512.digest(message = input.encodeToByteArray().map { it.toUByte( Result is returned as a `Array` +##### Updateable version + +Or you can use the updatable instance version + +```kotlin +val sha256 = Sha256() +sha256.update("abc") +val result = sha256.digest() +``` + +```kotlin +val sha512 = Sha512() +sha512.update("abc") +val result = sha512.digest() +``` + diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt index 21add43..23f9cfd 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt @@ -27,7 +27,7 @@ interface Hash { } @ExperimentalUnsignedTypes -interface UpdateableHash : Hash { +interface UpdatableHash : Hash { fun update(data : Array) fun update(data : String) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt index 5c435f7..b462faa 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt @@ -20,7 +20,7 @@ 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.StatelessHash -import com.ionspin.kotlin.crypto.hash.UpdateableHash +import com.ionspin.kotlin.crypto.hash.UpdatableHash /** * Created by Ugljesa Jovanovic @@ -29,7 +29,7 @@ import com.ionspin.kotlin.crypto.hash.UpdateableHash */ @ExperimentalUnsignedTypes -class Blake2b(val key: Array? = null, val hashLength: Int = 64) : UpdateableHash { +class Blake2b(val key: Array? = null, val hashLength: Int = 64) : UpdatableHash { companion object : StatelessHash { diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256.kt index 58e47c2..34995a7 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256.kt @@ -18,7 +18,7 @@ package com.ionspin.kotlin.crypto.hash.sha import com.ionspin.kotlin.crypto.chunked import com.ionspin.kotlin.crypto.hash.StatelessHash -import com.ionspin.kotlin.crypto.hash.UpdateableHash +import com.ionspin.kotlin.crypto.hash.UpdatableHash import com.ionspin.kotlin.crypto.rotateRight @@ -30,7 +30,7 @@ import com.ionspin.kotlin.crypto.rotateRight @ExperimentalUnsignedTypes -class Sha256 : UpdateableHash { +class Sha256 : UpdatableHash { override val MAX_HASH_BYTES: Int = 32 diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt index 95a934c..217175e 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt @@ -18,7 +18,7 @@ package com.ionspin.kotlin.crypto.hash.sha import com.ionspin.kotlin.crypto.chunked import com.ionspin.kotlin.crypto.hash.StatelessHash -import com.ionspin.kotlin.crypto.hash.UpdateableHash +import com.ionspin.kotlin.crypto.hash.UpdatableHash import com.ionspin.kotlin.crypto.rotateRight /** @@ -28,7 +28,7 @@ import com.ionspin.kotlin.crypto.rotateRight */ @ExperimentalUnsignedTypes -class Sha512 : UpdateableHash { +class Sha512 : UpdatableHash { override val MAX_HASH_BYTES: Int = 32 diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt index 2a1d703..ee449cb 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt @@ -96,5 +96,32 @@ class ReadmeTest { } + } + + @ExperimentalStdlibApi + @Test + fun sha256UpdatableExample() { + val sha256 = Sha256() + sha256.update("abc") + val result = sha256.digest() + val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + assertTrue { + result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toTypedArray()) + } + } + + @ExperimentalStdlibApi + @Test + fun sha512UpdatableExample() { + val sha512 = Sha512() + sha512.update("abc") + val result = sha512.digest() + val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" + assertTrue { + result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toTypedArray()) + } + + } } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bInstanceTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bInstanceTest.kt index 24f1181..44eaaa0 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bInstanceTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bInstanceTest.kt @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class Blake2bInstanceTest { @Test - fun testUpdateableBlake2b() { + fun testUpdatableBlake2b() { val updates = 14 val input = "1234567890" val expectedResult = arrayOf( diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256UpdateableTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256UpdateableTest.kt index 8790573..620d2fd 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256UpdateableTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256UpdateableTest.kt @@ -26,7 +26,7 @@ import kotlin.test.assertTrue * on 17-Jul-2019 */ @ExperimentalUnsignedTypes -class Sha256UpdateableTest { +class Sha256UpdatableTest { @ExperimentalStdlibApi @Test @@ -38,8 +38,6 @@ class Sha256UpdateableTest { assertTrue { result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toTypedArray()) } - - } @ExperimentalStdlibApi @@ -99,5 +97,4 @@ class Sha256UpdateableTest { resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toTypedArray()) } } - //50e72a0e 26442fe2 552dc393 8ac58658 228c0cbf b1d2ca87 2ae43526 6fcd055e } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512UpdateableTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512UpdateableTest.kt index 19ecd40..9facd3b 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512UpdateableTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512UpdateableTest.kt @@ -25,7 +25,7 @@ import kotlin.test.assertTrue * ugljesa.jovanovic@ionspin.com * on 21-Jul-2019 */ -class Sha512UpdateableTest { +class Sha512UpdatableTest { @ExperimentalStdlibApi @Test fun testWellKnownValue() {