GithubHelp home page GithubHelp logo

bierdav / khash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from komputing/khash

0.0 0.0 0.0 808 KB

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

License: MIT License

Kotlin 100.00%

khash's Introduction

CI

KHash

KHash is a Kotlin multiplatform library implementing common hashing functions.

A part 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

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

When 'username' could be anything and 'password' is an encoded access token for public access.

JitPack (JVM only!)

This library is available on Jitpack. The current version is:

badge

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

repositories {
    maven { url="https://jitpack.io" }
}

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

khash's People

Contributors

friedger avatar ligi avatar mirceanis avatar riccardom avatar

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.