Kotlin SHA3 (with Keccak and Shake variants) implementation
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.ci
gradle/wrapper
sha3
.gitignore
LICENSE
README.md
build.gradle
gradle.properties
gradlew
settings.gradle

README.md

SHA3

Kotlin SHA3 implementation. Also supports Keccak and Shake.

Heavily based on this java implementation: @romus/sha

Tested with Nist test vectors

Why

I was using the spongycastle/bouncycastle implementation before. That was working but had 2 major drawbacks:

  • huge footprint
  • as it is java it can only be used in JVM projects and especially for KEthereum I would like to be able to also target other platforms like native, WASM, JS, ..

How

import org.walleth.sha3.SHA3Parameter
import org.walleth.sha3.calculateSHA3

val hash = "hello world".calculateSHA3(SHA3Parameter.KECCAK_256)

The hash will be a ByteArray

There is also an extension function that operates on a ByteArray. This is also the core function:

fun ByteArray.calculateSHA3(parameter: SHA3Parameter): ByteArray {
   ...
}

The "hello world" example above just uses the extension function from this library that converts the String to a ByteArray before.

fun String.calculateSHA3(parameter: SHA3Parameter) = toByteArray().calculateSHA3(parameter)

These are the parameters you can use:

enum class SHA3Parameter constructor(val rateInBytes: Int,
                                     val outputLengthInBytes: Int,
                                     val d: Int) {

    KECCAK_224(144, 28, 0x01),
    KECCAK_256(136, 32, 0x01),
    KECCAK_384(104, 48, 0x01),
    KECCAK_512(72, 64, 0x01),

    SHA3_224(144, 28, 0x06),
    SHA3_256(136, 32, 0x06),
    SHA3_384(104, 48, 0x06),
    SHA3_512(72, 64, 0x06),

    SHAKE128(168, 32, 0x1F),
    SHAKE256(136, 64, 0x1F)
}

Disclaimer

The results should be correct as the Nist test vectors pass and also second degree tests pass in KEthereum. That said there should be more eyes on this project before it is used in really critical situations. If you can spare some time please have a look at the code - feedback is very welcome.

Also this code is not hardened against side channel attacks. Keep this in mind when hashing sensitive content! I would be really happy about input from security researchers here - there is also an open issue with a bounty for this.

Links

Projects using this library

License

MIT