forked from sergeych/crypto2
0.5.4: compound hash
This commit is contained in:
1 parent
a947271c3c
commit
d0fa74e089
5 files changed
+55
-21
No files matched your search
@@ -21,21 +21,24 @@ private interface StreamProcessor {
|
||||
* - [ofFlow] to calculate hash of the data in the [Flow<UByteArray>]
|
||||
*/
|
||||
@Suppress("unused")
|
||||
enum class Hash(private val direct: ((UByteArray) -> UByteArray)? = null,private val streamProcessor: () -> StreamProcessor) {
|
||||
enum class Hash(
|
||||
private val direct: ((UByteArray) -> UByteArray)? = null,
|
||||
private val streamProcessor: () -> StreamProcessor,
|
||||
) {
|
||||
|
||||
Blake2b(
|
||||
// direct blacke2 is faster than stream:
|
||||
{ GenericHash.genericHash(it) }, {
|
||||
object : StreamProcessor {
|
||||
val state = GenericHash.genericHashInit()
|
||||
override fun update(data: UByteArray) {
|
||||
GenericHash.genericHashUpdate(state, data)
|
||||
}
|
||||
object : StreamProcessor {
|
||||
val state = GenericHash.genericHashInit()
|
||||
override fun update(data: UByteArray) {
|
||||
GenericHash.genericHashUpdate(state, data)
|
||||
}
|
||||
|
||||
override fun final(): UByteArray =
|
||||
GenericHash.genericHashFinal(state)
|
||||
}
|
||||
}),
|
||||
override fun final(): UByteArray =
|
||||
GenericHash.genericHashFinal(state)
|
||||
}
|
||||
}),
|
||||
Sha3_384(
|
||||
// direct Keccaak currently is slower
|
||||
null,
|
||||
@@ -59,6 +62,24 @@ enum class Hash(private val direct: ((UByteArray) -> UByteArray)? = null,private
|
||||
|
||||
override fun final(): UByteArray = state.digest().asUByteArray()
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Synthetic hash contains of [Blake2b] and [Sha3_384] hashes concatenated in this order. Let calculate
|
||||
* compound hash using [ofFlow] or [ofChannel] effectively.
|
||||
*/
|
||||
Sha3AndBlake(null,
|
||||
{
|
||||
object : StreamProcessor {
|
||||
val blake = Blake2b.streamProcessor()
|
||||
val sha384 = Sha3_384.streamProcessor()
|
||||
override fun update(data: UByteArray) {
|
||||
blake.update(data)
|
||||
sha384.update(data)
|
||||
}
|
||||
|
||||
override fun final(): UByteArray = blake.final() + sha384.final()
|
||||
}
|
||||
});
|
||||
|
||||
@Deprecated("will be removed in favor of digest()", ReplaceWith("digest()"))
|
||||
@@ -69,7 +90,7 @@ enum class Hash(private val direct: ((UByteArray) -> UByteArray)? = null,private
|
||||
* @param src data to calculate hash digest over
|
||||
* @return calculated hash value
|
||||
*/
|
||||
fun digest(src: UByteArray): UByteArray = direct?.invoke(src) ?: streamProcessor().also { it.update(src)}.final()
|
||||
fun digest(src: UByteArray): UByteArray = direct?.invoke(src) ?: streamProcessor().also { it.update(src) }.final()
|
||||
|
||||
/**
|
||||
* Collect the flow and return the hash digest of all the data. Let calculate hashes on data
|
||||
@@ -87,7 +108,7 @@ enum class Hash(private val direct: ((UByteArray) -> UByteArray)? = null,private
|
||||
*/
|
||||
suspend fun ofChannel(source: ReceiveChannel<UByteArray>): UByteArray {
|
||||
val sp = streamProcessor()
|
||||
for( block in source) sp.update(block)
|
||||
for (block in source) sp.update(block)
|
||||
return sp.final()
|
||||
}
|
||||
|
||||
@@ -106,7 +127,7 @@ fun blake2b(src: UByteArray): UByteArray = Hash.Blake2b.digest(src)
|
||||
* brute force.collision attack than just [blake2b]. Note that different suffixes provide different
|
||||
* results.
|
||||
*/
|
||||
fun blake2b2l(src: UByteArray,suffix: UByteArray = defaultSuffix1): UByteArray =
|
||||
fun blake2b2l(src: UByteArray, suffix: UByteArray = defaultSuffix1): UByteArray =
|
||||
blake2b(blake2b(src) + suffix + src)
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user