Skip to content
/ KHash Public
forked from komputing/KHash

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

License

Notifications You must be signed in to change notification settings

z-chu/KHash

 
 

Repository files navigation

KHash

KHash is a Kotlin library implementing common hashing functions.

A part from that, it also provides some useful extensions functions.

The implemented hashing functions are the following.

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%