Hashlib
Secure hash functions, checksum generators, and key derivation algorithms in pure Dart
Install / Use
/learn @bitanon/HashlibREADME
hashlib
This library contains implementations of secure hash functions, checksum generators, and key derivation algorithms optimized for Dart.
Dependencies
There is only 1 dependency used by this package:
Features
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, xxh128code | Cyan4973 |
| RIPEMD | ripemd128, ripemd256, ripemd160, ripemd320 | ISO/IEC 10118-3:2018 |
| SM3 | sm3 , sm3sum | GB/T 32905-2016 |
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 Code (MAC) Generators
| Algorithms | Available methods | Source |
| ---------- | -------------------------------------- | -------- |
| HMAC | HMAC, #.hmac | RFC-2104 |
| Poly1305 | Poly1305, poly1305, poly1305auth | RFC-8439 |
OTP generation for 2FA
| Algorithms | Available methods | Source |
| ---------- | ----------------- | -------- |
| HOTP | HOTP | RFC-4226 |
| TOTP | TOTP | RFC-6238 |
Other Hash Algorithms
| Algorithms | Available methods | Source |
| ---------- | ------------------------- | --------- |
| CRC | crc16, crc32, crc64 | Wikipedia |
| Adler32 | adler32 | Wikipedia |
Random Algorithm
Random number generators
Accessible through HashlibRandom:
- secure
- system
- keccak
- sha256
- md5
- xxh64
- sm3
UUID generators
Accessible through uuid
- v1
- v3
- v4
- v5
- v6
- v7
- v8
Getting Started
The following import will give you access to all of the algorithms in this package.
import 'package:hashlib/hashlib.dart' as hashlib;
Check the API Reference for details.
Usage
Examples can be found inside the example folder.
Hashlib Example
import 'package:hashlib/codecs.dart';
import 'package:hashlib/hashlib.dart';
void main() {
var text = "Happy Hashing!";
print("text => $text");
final key = "password";
final salt = "some salt";
print("key => $key");
print("salt => $salt");
print('');
final pw = key.codeUnits;
final iv = salt.codeUnits;
// Example of hash-code generations
print('XXH32 => ${xxh32code(text)}');
print('CRC32 => ${crc32code(text)}');
print('Adler32 => ${adler32code(text)}');
print('CRC16 => ${crc16code(text)}');
print('');
// Examples of Hash generation
print('CRC64 => ${crc64sum(text)}');
print('XXH64 => ${xxh64sum(text)}');
print('XXH3 => ${xxh3sum(text)}');
print('XXH128 => ${xxh128sum(text)}');
print('MD2 => ${md2.string(text)}');
print('MD4 => ${md4.string(text)}');
print('MD5 => ${md5.string(text)}');
print('SHA-1 => ${sha1.string(text)}');
print('SHA-224 => ${sha224.string(text)}');
print('SHA-256 => ${sha256.string(text)}');
print('SHA-384 => ${sha384.string(text)}');
print('SHA-512 => ${sha512.string(text)}');
print('SHA-512/224 => ${sha512t224.string(text)}');
print('SHA-512/256 => ${sha512t256.string(text)}');
print('SHA3-224 => ${sha3_224.string(text)}');
print('SHA3-256 => ${sha3_256.string(text)}');
print('SHA3-384 => ${sha3_384.string(text)}');
print('SHA3-512 => ${sha3_512.string(text)}');
print('Keccak-224 => ${keccak224.string(text)}');
print('Keccak-256 => ${keccak256.string(text)}');
print('Keccak-384 => ${keccak384.string(text)}');
print('Keccak-512 => ${keccak512.string(text)}');
print('SHAKE-128 => ${shake128.of(20).string(text)}');
print('SHAKE-256 => ${shake256.of(20).string(text)}');
print('BLAKE2s-256 => ${blake2s256.string(text)}');
print('BLAKE2b-512 => ${blake2b512.string(text)}');
print('SM3 => ${sm3.string(text)}');
print('');
// Examples of MAC generations
print('HMAC/MD5 => ${md5.hmac.by(pw).string(text)}');
print('HMAC/SHA1 => ${sha1.hmac.byString(text)}');
print('HMAC/SHA256 => ${sha256.hmac.byString(key).string(text)}');
print('HMAC/SHA3-256 => ${HMAC(sha3_256).by(pw).string(text)}');
print("HMAC/BLAKE2b-256 => ${blake2b512.hmac.by(pw).string(text)}");
print("BLAKE-2b-MAC/256 => ${blake2b256.mac.by(pw).string(text)}");
print("BLAKE-2b-MAC/224 => ${Blake2b(28).mac.by(pw).string(text)}");
print('');
// Examples of OTP generation
int nw = DateTime.now().millisecondsSinceEpoch ~/ 30000;
var counter = fromHex(nw.toRadixString(16).padLeft(16, '0'));
print('TOTP[time=$nw] => ${TOTP(iv).value()}');
print('HOTP[counter=$nw] => ${HOTP(iv, counter: counter).value()}');
print('');
}
Key Generation Example
import 'package:hashlib/hashlib.dart';
void main() {
final key = "password";
final salt = "some salt";
print("key => $key");
print("salt => $salt");
print('');
final pw = key.codeUnits;
final iv = salt.codeUnits;
// Examples of Argon2 key derivation
final argon2Test = Argon2Security.test;
print("[Argon2i] => ${argon2i(pw, iv, security: argon2Test)}");
print("[Argon2d] => ${argon2d(pw, iv, security: argon2Test)}");
print("[Argon2id] => ${argon2id(pw, iv, security: argon2Test)}");
// Examples of scrypt key derivation
final scryptLittle = ScryptSecurity.little;
print("[scrypt] => ${scrypt(pw, iv, security: scryptLittle, dklen: 24)}");
print('');
// Examples of bcrypt key derivation
final bcryptLittle = BcryptSecurity.little;
print("[bcrypt] => ${bcrypt(pw, bcryptSalt(security: bcryptLittle))}");
print('');
// Examples of PBKDF2 key derivation
print("SHA256/HMAC/PBKDF2 => ${pbkdf2(pw, iv).hex()}");
print("BLAKE2b-256/HMAC/PBKDF2 => ${blake2b256.pbkdf2(iv).hex(pw)}");
print("BLAKE2b-256/MAC/PBKDF2 => ${blake2b256.mac.pbkdf2(iv).hex(pw)}");
print("SHA1/HMAC/PBKDF2 => ${sha1.pbkdf2(iv, iterations: 100).hex(pw)}");
print('');
}
Random Example
import 'package:hashlib/codecs.dart';
import 'package:hashlib/random.dart';
void main() {
print('UUID Generation:');
print('UUIDv1: ${uuid.v1()}');
print('UUIDv3:
