A pure Haskell implementation of RIPEMD-160 and HMAC-RIPEMD160 on strict and lazy ByteStrings.
A sample GHCi session:
> :set -XOverloadedStrings
>
> -- import qualified
> import qualified Crypto.Hash.RIPEMD160 as RIPEMD160
>
> -- 'hash' and 'hmac' operate on strict bytestrings
>
> let hash_s = RIPEMD160.hash "strict bytestring input"
> let hmac_s = RIPEMD160.hmac "strict secret" "strict bytestring input"
>
> -- 'hash_lazy' and 'hmac_lazy' operate on lazy bytestrings
> -- but note that the key for HMAC is always strict
>
> let hash_l = RIPEMD160.hash_lazy "lazy bytestring input"
> let hmac_l = RIPEMD160.hmac_lazy "strict secret" "lazy bytestring input"
>
> -- results are always unformatted 160-bit (20-byte) strict bytestrings
>
> import qualified Data.ByteString as BS
>
> BS.take 10 hash_s
"=\211\211\197]\NULJ\223n\223"
> BS.take 10 hmac_l
"\154\248\145[\196\ETX\f\ESC\NULs"
>
> -- you can use third-party libraries for rendering if needed
> -- e.g., using ppad-base16:
>
> import qualified Data.ByteString.Base16 as B16
>
> B16.encode hash_s
"3dd3d3c55d004adf6edf9e11cb01f9ac9c56441f"
> B16.encode hmac_l
"9af8915bc4030c1b007323c8531b3129d82f50bd"
Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/ripemd160.
The aim is best-in-class performance for pure, highly-auditable Haskell code.
Current benchmark figures on my M4 Silicon MacBook Air look like (use
cabal bench to run the benchmark suite):
benchmarking ppad-ripemd160/RIPEMD160 (32B input)/hash
time 179.1 ns (178.9 ns .. 179.3 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 179.7 ns (179.5 ns .. 179.9 ns)
std dev 715.4 ps (538.9 ps .. 988.3 ps)
benchmarking ppad-ripemd160/HMAC-RIPEMD160 (32B input)/hmac
time 661.4 ns (660.0 ns .. 663.3 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 667.3 ns (664.7 ns .. 669.9 ns)
std dev 8.607 ns (7.495 ns .. 9.627 ns)
variance introduced by outliers: 12% (moderately inflated)
Compile with the 'llvm' flag for maximum performance.
This library aims at the maximum security achievable in a garbage-collected language under an optimizing compiler such as GHC, in which strict constant-timeness can be challenging to achieve.
The RIPEMD-160 functions pass the vectors present in the official spec, and the HMAC-RIPEMD160 functions pass all vectors found contained in RFC2286.
If you discover any vulnerabilities, please disclose them via security@ppad.tech.
You'll require Nix with flake support enabled. Enter a development shell with:
$ nix develop
Then do e.g.:
$ cabal repl ppad-ripemd160
to get a REPL for the main library.