Skip to content

KotlinCrypto/hash

Repository files navigation

hash

badge-license badge-latest-release

badge-kotlin badge-core badge-endians badge-sponges

badge-platform-android badge-platform-jvm badge-platform-js badge-platform-js-node badge-platform-wasm badge-platform-linux badge-platform-macos badge-platform-ios badge-platform-tvos badge-platform-watchos badge-platform-windows badge-support-android-native badge-support-apple-silicon badge-support-js-ir badge-support-linux-arm

Cryptographic hash functions for Kotlin Multiplatform

Utilized by KotlinCrypto/MACs

Usage

See HERE for basic usage example for Digest.

fun main() {
    // Digests that may be needed for backward compatibility but 
    // should no longer be utilized because they have been broken.
    MD5()
    SHA1()
}

SHA2 Digests

fun main() {
    SHA224()
    SHA256()
    SHA384()
    SHA512()

    SHA512_224()
    SHA512_256()
    SHA512t(504)
}

SHA3 Digests

fun main() {
    Keccak224()
    Keccak256()
    Keccak384()
    Keccak512()
    SHA3_224()
    SHA3_256()
    SHA3_384()
    SHA3_512()

    SHAKE128()
    SHAKE256(outputLength = 640) // returns 640 bytes instead of the default when digest() is invoked
    
    // NIST.SP.800-185 derived functions
    val S = "My Customization".encodeToByteArray()
    CSHAKE128(null, S, outputLength = 128)
    CSHAKE256(null, S)
    ParallelHash128(null, B = 123)
    ParallelHash256(S, B = 456, outputLength = 123)
    TupleHash128(S, outputLength = 320)
    TupleHash256(null)
}

SHA3 XOFs (i.e. Extendable-Output Functions)

See HERE for details on what XOFs are, and a basic usage example for Xof.

fun main() {
    SHAKE128.xOf()
    SHAKE256.xOf()

    // NIST.SP.800-185 derived functions
    val S = "My Customization".encodeToByteArray()
    CSHAKE128.xOf(null, S)
    CSHAKE256.xOf(null, S)
    ParallelHash128.xOf(S, B = 123)
    ParallelHash256.xOf(B = 654)
    TupleHash128.xOf(S)
    TupleHash256.xOf()
}

Get Started

The best way to keep KotlinCrypto dependencies up to date is by using the version-catalog. Alternatively, you can use the BOM as shown below.

// build.gradle.kts
dependencies {
    // define the BOM and its version
    implementation(platform("org.kotlincrypto.hash:bom:0.5.6"))

    // define artifacts without version
    
    // MD5
    implementation("org.kotlincrypto.hash:md")

    // SHA-1
    implementation("org.kotlincrypto.hash:sha1")
    
    // SHA-224, SHA-256, SHA-384, SHA-512
    // SHA-512/t, SHA-512/224, SHA-512/256
    implementation("org.kotlincrypto.hash:sha2")

    // Keccak-224, Keccak-256, Keccak-384, Keccak-512
    // SHA3-224, SHA3-256, SHA3-384, SHA3-512
    // SHAKE128, SHAKE256
    // CSHAKE128, CSHAKE256
    // ParallelHash128, ParallelHash256
    // TupleHash128, TupleHash256
    implementation("org.kotlincrypto.hash:sha3")
}