Giter Club home page Giter Club logo

hash's Introduction

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.1"))

    // 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")
}

hash's People

Contributors

05nelsonm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hash's Issues

Is the addition of fingerprinting algorithms in scope?

Specifically, I'm looking for a Winnowing implementation with configurable gram and window size. I could think of other fingerprinting algorithms as well, but first wanted to check if they're eligible to get added, as they're based on hashes, but they're not (cryptographic) hashes themselves.

Fix `CI` caching

Currently, caching for CI is hashing all **.gradle.kts files and using that hash as a key. This invalidates the GitHub Action cache whenever a new module is added. This should instead hash only the gradle/libs.versions.toml file such that the PR where a module was added, but other modules were not touched re-uses those unmodified modules' caches.

Rename `Sha2` digest abstractions

Current naming convention is Sha2IntDigest and Sha2LongDigest. This should be renamed to Bit32Digest and Bit64Digest, respectively. Module names should also be updated to sha2-digest-32 and sha2-digest-64, respectively.

There will be a refactor of these digests to use BigEdian when performing bitwise operations for performance purposes, so it makes sense now name them appropriately now while unrelease.

`SHA3` extended function variable output.

According to NIST.SP.800-185 , the extended functions should all have L (desired number of bits to output) as an input to produce variable length output when digest (or doFinal for Macs) is called. It specifies L as an input parameter for producing L / 8 bytes of output. The derived functions KMAC, TupleHash and ParallelHash are to rightEncode this value and then apply it before producing output. When the functions are being utilized as XOFs, this value will be 0 to indicate an arbitrary length.

After looking around at some different implementations (go, rust), I only see the option to utilize SHAKE, CSHAKE and functions derived from CSHAKE, as having the ability to be used as XOFs and skipping the customization of L all together.

As the publication specifies L as a function input, I believe the best course of action is to add the customization via an additional constructor which can be utilized when the default digest length is not wanted.

public class CSHAKE128: SHAKEDigest {
    public constructor(
        N: ByteArray?,
        S: ByteArray?,
        outputByteLength: Int,
        // ...
    )
}

I see in BouncyCastle that they simply have on their Xof interface the doFinal and doOutput functions which provides this functionality, but holy shit does it wreak havoc on the implementations. I am very happy that I went with the XofDelegate route for a clean separation of concerns.

Class name clashing

#23 refactored modules to combine all SHA2 functions. There's a problem here that I think could rear its head. If someone is already relying on module sha2-256 or sha2-512, it could cause a name clash if/when they import sha2.

To prevent this, sha2-256 and sha2-512 modules should be published, with api dependencies on sha2 to provide Sha256 and Sha512. In order to notify the library consumer that there was a module name change (and to bring the library more in line with other libs), Sha* classes should be renamed to SHA* (all uppercase) and Sha256 and Sha512 should be typealias to SHA256 and SHA512 respectively + deprecation notice on the typealias to let consumers know they should no longer import the sha2-256 and sha2-512 dependencies, but import sha2 instead.

Renaming + typealias should also occur with current Md5 and Sha1 classes.

JPMS split packages

core version 0.3.0-SNAPSHOT is available containing API breaking changes.

See KotlinCrypto/core#48

This issue will require a few things:

  • Update import package names for Digest and Xof
  • Move MD5 to new module md
  • Add md module as dependency for md5 and deprecate md5 module
    • The current md5 module contains a typealias that needs to be removed.
    • By subbing in the md module, it opens up the ability to add, in the future, things like MD2 w/o breakage.
  • Remove the org.kotlincrypto.hash.Sha1 typealias

Remove `sha2-256` and `sha2-512` modules

For release 0.3.0, modules sha2-256 and sha2-512 will no longer be published. They were there for supporting backward compatibility with consumers of 0.1.X versions and will be going away as there has been ample time to heed the deprecation notice.

Create abstractions for `sha-2`

Instead of copy/pastaing, create abstraction modules:

  • sha2-digest-int for SHA-224 and SHA-256
  • sha2-digest-long for SHA-384 and SHA-512

Trait constants

Add constants to each algorithm with its traits, much like primitives, such as Byte.SIZE_BITS or Int.SIZE_BYTES

e.g.

public class MD5: Digest {

    // ...

    public companion object {
        public const val NAME: String = "MD5"
        public const val BLOCK_SIZE: Int = 64
        public const val LENGTH_BYTES: Int = 16
        public const val LENGTH_BITS: Int = LENGTH_BYTES * Byte.SIZE_BITS
    }
}

Merge `SHA2` into single module

Current code is too modularized, highly regarded. The only thing changing are inputs/outputs.

Merge everything into sha2 module. Same for sha3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.