Skip to content

Different hash functions in Kotlin (SHA3 with Keccak and Shake variants, sha256, ripemd160)

License

Notifications You must be signed in to change notification settings

komputing/KHash

Repository files navigation

CI

KHash

KHash is a Kotlin multiplatform library implementing common hashing functions.

Apart from that, it also provides some useful extensions functions.

The implemented hashing functions are the following.

Get it

GitHub Packages

This library is available through GitHub Packages.

badge badge badge badge badge badge badge

In order to use it, first include the GitHub Packages maven repository inside your project build.gradle.kts file:

repositories {
    maven {
        name = "komputing/KHash GitHub Packages"
        url = uri("https://maven.pkg.github.com/komputing/KHash")
        credentials {
            username = "token"
            password = "\u0039\u0032\u0037\u0034\u0031\u0064\u0038\u0033\u0064\u0036\u0039\u0061\u0063\u0061\u0066\u0031\u0062\u0034\u0061\u0030\u0034\u0035\u0033\u0061\u0063\u0032\u0036\u0038\u0036\u0062\u0036\u0032\u0035\u0065\u0034\u0061\u0065\u0034\u0032\u0062"
        }
    }
}

Set it up

Include the modules inside your project:

dependencies {
    implementation("com.github.komputing.khash:<module>:<version>")
}

Where <version> can be either a release or <branch>-SNAPSHOT such as master-SNAPHOT.

Usage

keccak

Module containing the implementation of the Kecccak hashing algorithms.

Object usage

// Compute the Keccak digest of a byte array based on the given parameter
Keccak.digest(byteArrayOf(1, 2, 3), KeccakParameter.KECCAK_512) 
Keccak.digest(byteArrayOf(1, 2, 3), KeccakParameter.SHA3_224) 
Keccak.digest(byteArrayOf(1, 2, 3), KeccakParameter.SHAKE_128) 

Extension functions

// Compute a specific Keccak digest of a byte array based on the given parameter
byteArrayOf(1, 2, 3).digestKeccak(KeccakParameter.KECCAK_512)

// Compute a specific Keccak digest of a string based on the given parameter
"The quick brown fox jumps over the lazy dog".digestKeccak(parameter = KeccakParameter.SHA3_384)

ripemd160

Module containing the implementation of the RIPEMD160 hashing algorithms.

Object usage

// Compute the RIPEMD160 digest of a byte array
val input = byteArrayOf(1, 2, 3)
val output = ByteArray(Ripemd160Digest.DIGEST_LENGTH)
Ripemd160Digest().apply {
  update(input, 0, input.size)
  doFinal(output, 0)
} 

Extension functions

// Compute the RIPEMD160 digest of a byte array
byteArrayOf(1, 2, 3).ripemd160()

// Compute the RIPEMD160 digest of a string
"The quick brown fox jumps over the lazy dog".ripemd160()

sha256

Module containing the implementation of the SHA256 hashing algorithms.

Object usage

// Compute the SHA-256 digest of a byte array 
Sha256.digest(byteArrayOf(1, 2, 3))

Extension functions

// Compute the SHA-256 digest of a byte array
byteArrayOf(1, 2, 3).sha256()

// Compute the SHA-256 digest of a string
"The quick brown fox jumps over the lazy dog".sha256()

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