Name and README update

This commit is contained in:
Ugljesa Jovanovic 2019-07-21 12:36:10 +02:00
parent 6d8024cf7f
commit 91616fe0e0
No known key found for this signature in database
GPG Key ID: 46D004C9820EBB98
9 changed files with 67 additions and 19 deletions

View File

@ -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<Byte>`
#### Using a `Blake2b` instance
##### Updatable instance version
You can create an instance and feed the data by using `update(input : Array<Byte>)` call. Once all data is supplied,
you should call `digest()` or `digestString()` convinence method that converts the `Array<Byte>` into hexadecimal string.
you should call `digest()` or `digestString()` convenience method that converts the `Array<Byte>` 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<Byte>` as input
or `String`. Result is always returned as `Array<Byte>` (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<Byte>`
##### 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()
```

View File

@ -27,7 +27,7 @@ interface Hash {
}
@ExperimentalUnsignedTypes
interface UpdateableHash : Hash {
interface UpdatableHash : Hash {
fun update(data : Array<UByte>)
fun update(data : String)

View File

@ -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<UByte>? = null, val hashLength: Int = 64) : UpdateableHash {
class Blake2b(val key: Array<UByte>? = null, val hashLength: Int = 64) : UpdatableHash {
companion object : StatelessHash {

View File

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

View File

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

View File

@ -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())
}
}
}

View File

@ -29,7 +29,7 @@ import kotlin.test.assertTrue
class Blake2bInstanceTest {
@Test
fun testUpdateableBlake2b() {
fun testUpdatableBlake2b() {
val updates = 14
val input = "1234567890"
val expectedResult = arrayOf<UByte>(

View File

@ -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
}

View File

@ -25,7 +25,7 @@ import kotlin.test.assertTrue
* ugljesa.jovanovic@ionspin.com
* on 21-Jul-2019
*/
class Sha512UpdateableTest {
class Sha512UpdatableTest {
@ExperimentalStdlibApi
@Test
fun testWellKnownValue() {