Skip to content

Hashing & KDF

hashlib

Secure hashes, MACs, password KDFs, OTPs and secure random — in pure Dart.

A pure-Dart library of secure hash functions, checksums, MACs, key-derivation functions, OTP generators, and secure random — a broad, fast, dependency-light toolbox that runs everywhere Dart does.

Highlights

Runs on every platform

Pure Dart with no native code or FFI, so the same library works everywhere Dart does — the VM, Flutter (Android, iOS, Windows, macOS, Linux), and the web (dart2js and dart2wasm).

Batteries included

Ten hash families (MD, SHA-1, SHA-2, SHA-3, SHAKE, Keccak, BLAKE2, RIPEMD, SM3, xxHash), checksums (CRC, Adler-32), MACs (HMAC, Poly1305), password KDFs (Argon2, scrypt, bcrypt, PBKDF2), TOTP/HOTP, and RNG/UUID generators.

One-shot or streaming

Hash a String or byte buffer in a single call, or feed data incrementally through a reusable sink for large or chunked input.

Password-grade KDFs

Argon2id, scrypt, bcrypt, and PBKDF2 with named security presets that make the cost/latency trade-off explicit.

Fast

Outruns crypto, PointyCastle, and hash in most benchmarks (crypto stays slightly ahead on some 10-byte inputs), see the benchmarks below.

Codecs and secure random built in

Companion codecs and random libraries ship in the same package.

Quickstart

Install it, import it, and you are ready. This example comes straight from the README on the repository’s default branch, so it never drifts from the docs.

More recipes in the guide
main.dart
import 'package:hashlib/hashlib.dart';

void main() {
  // A hex fingerprint of some text — that's the whole idea.
  print(sha256.string('Happy Hashing!').hex());
}

Performance

Throughput against PointyCastle, crypto, and hash at a typical input size (shown on each card). Higher is better.

56/63

head-to-head benchmarks won (89%)

MD51KB message
  • hashlib1.35 Gbps
  • crypto1.29 Gbps
  • hash929 Mbps
  • PointyCastle729 Mbps

1.05× the fastest alternative

SHA-11KB message
  • hashlib1.17 Gbps
  • crypto1.07 Gbps
  • PointyCastle472 Mbps
  • hash534 Mbps

1.09× the fastest alternative

SHA-2561KB message
  • hashlib945 Mbps
  • crypto891 Mbps
  • hash234 Mbps
  • PointyCastle220 Mbps

1.06× the fastest alternative

SHA-5121KB message
  • hashlib1.64 Gbps
  • crypto561 Mbps
  • hash164 Mbps
  • PointyCastle46.35 Mbps

2.9× the fastest alternative

SHA3-2561KB message
  • hashlib936 Mbps
  • PointyCastle29.19 Mbps

32× the fastest alternative

BLAKE-2b1KB message
  • hashlib2.07 Gbps
  • PointyCastle119 Mbps

17× the fastest alternative

All benchmarks are done on 36GB Apple M3 Pro using compiled exe, Dart 3.12.2.Explore all benchmarks →

Supported algorithms

Block hash algorithms

Algorithm Available methods Source
MD2 md2, md2sum RFC-1319
MD4 md4, md4sum RFC-1320
MD5 md5, md5sum RFC-1321
SHA-1 sha1, sha1sum RFC-3174
SHA-2 sha224, sha256, sha384, sha512, sha512t224, sha512t256 RFC-6234
SHA-3 sha3_224, sha3_256, sha3_384, sha3_512 FIPS-202
SHAKE-128 Shake128, shake128, shake128_128, shake128_256 FIPS-202
SHAKE-256 Shake256, shake256, shake256_256, shake256_512 FIPS-202
Keccak keccak224, keccak256, keccak384, keccak512 Team Keccak
Blake2b blake2b160, blake2b256, blake2b384, blake2b512 RFC-7693
Blake2s blake2s128, blake2s160, blake2s224, blake2s256 RFC-7693
xxHash-32 XXHash32, xxh32, xxh32code Cyan4973
xxHash-64 XXHash64, xxh64, xxh64code Cyan4973
xxHash3-64 XXH3, xxh3, xxh3code Cyan4973
xxHash3-128 XXH128, xxh128, xxh128sum Cyan4973
RIPEMD ripemd128, ripemd256, ripemd160, ripemd320 ISO/IEC 10118-3:2018
SM3 sm3, sm3sum GB/T 32905-2016

Note: On the web, XXHash64, XXH3, and XXH128 run on 32-bit implementations with identical output. Seeds must fit in 53 bits there, and xxh64code/xxh3code may round results above 2^53; use the sum/hex variants for exact values.

Password / key derivation algorithms

Algorithm Available methods Source
Argon2 Argon2, argon2d, argon2i, argon2id, argon2Verify RFC-9106
PBKDF2 PBKDF2, pbkdf2, #.pbkdf2 RFC-8081
scrypt Scrypt, scrypt RFC-7914
bcrypt Bcrypt, bcrypt, bcryptSalt, bcryptVerify, bcryptDigest

Message authentication codes (MAC)

Algorithm Available methods Source
HMAC HMAC, #.hmac RFC-2104
Poly1305 Poly1305, poly1305, poly1305auth RFC-8439

One-time passwords (2FA)

Algorithm Available methods Source
HOTP HOTP RFC-4226
TOTP TOTP RFC-6238

Checksums

Algorithm Available methods Source
CRC crc16, crc32, crc64 Wikipedia
Adler-32 adler32 Wikipedia

Random and UUID

The random library provides random number generators through HashlibRandom (secure, system, keccak, sha256, md5, xxh64, sm3), plus helpers like randomBytes, randomNumbers, and randomString. UUID versions v1, v3, v4, v5, v6, v7, and v8 are available through uuid.

What's new in 2.4.4

Changelog →
  • Fixes Argon2 32bit implementation for dart2js #38

2.4.42.4.32.4.22.4.12.4.02.3.4

Part of the family