hashlib 2.2.0

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

Secure hash functions, checksum generators, and key derivation algorithms optimized for Dart.

hashlib

plugin version test codecov dart support likes pub points Pub Monthly Downloads

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

AlgorithmAvailable methodsSource
MD2md2 , md2sumRFC-1319
MD4md4, md4sumRFC-1320
MD5md5 , md5sumRFC-1321
SHA-1sha1 , sha1sumRFC-3174
SHA-2sha224, sha256, sha384, sha512, sha512t224, sha512t256RFC-6234
SHA-3sha3_224, sha3_256, sha3_384, sha3_512FIPS-202
SHAKE-128Shake128, shake128, shake128_128, shake128_256FIPS-202
SHAKE-256Shake256, shake256, shake256_256, shake256_512FIPS-202
Keccakkeccak224, keccak256, keccak384, keccak512Team Keccak
Blake2bblake2b160, blake2b256, blake2b384, blake2b512RFC-7693
Blake2sblake2s128, blake2s160, blake2s224, blake2s256RFC-7693
xxHash-32XXHash32,xxh32,xxh32codeCyan4973
xxHash-64XXHash64,xxh64,xxh64codeCyan4973
xxHash3-64XXH3, xxh3, xxh3codeCyan4973
xxHash3-128XXH128, xxh128, xxh128codeCyan4973
RIPEMDripemd128, ripemd256, ripemd160, ripemd320ISO/IEC 10118-3:2018
SM3sm3 , sm3sumGB/T 32905-2016

Password / Key Derivation Algorithms

AlgorithmAvailable methodsSource
Argon2Argon2, argon2d, argon2i, argon2id, argon2VerifyRFC-9106
PBKDF2PBKDF2, pbkdf2, #.pbkdf2RFC-8081
scryptScrypt, scrypt,RFC-7914
bcryptBcrypt, bcrypt, bcryptSalt, bcryptVerify, bcryptDigest

Message Authentication Code (MAC) Generators

AlgorithmsAvailable methodsSource
HMACHMAC, #.hmacRFC-2104
Poly1305Poly1305, poly1305, poly1305authRFC-8439

OTP generation for 2FA

AlgorithmsAvailable methodsSource
HOTPHOTPRFC-4226
TOTPTOTPRFC-6238

Other Hash Algorithms

AlgorithmsAvailable methodsSource
CRCcrc16, crc32, crc64Wikipedia
Adler32adler32Wikipedia

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: ${uuid.v3()}');
  print('UUIDv4: ${uuid.v4()}');
  print('UUIDv5: ${uuid.v5()}');
  print('UUIDv6: ${uuid.v6()}');
  print('UUIDv7: ${uuid.v7()}');
  print('UUIDv8: ${uuid.v8()}');
  print('');

  print('Random Generation:');
  print(randomNumbers(4));
  print(toHex(randomBytes(16)));
  print(randomString(32, lower: true, whitelist: '_'.codeUnits));
  print('');
}

Benchmarks

Libraries:


With 5MB message (10 iterations):

AlgorithmshashlibPointyCastlecryptohash
MD41.64 Gbps352 Mbps
4.66x slow
MD51.45 Gbps347 Mbps
4.18x slow
1.01 Gbps
1.44x slow
651 Mbps
2.23x slow
HMAC(MD5)1.33 Gbps991 Mbps
1.34x slow
653 Mbps
2.04x slow
SHA-11.27 Gbps248 Mbps
5.13x slow
794 Mbps
1.61x slow
388 Mbps
3.28x slow
HMAC(SHA-1)1.28 Gbps793 Mbps
1.61x slow
SHA-224856 Mbps152 Mbps
5.65x slow
685 Mbps
1.25x slow
198 Mbps
4.32x slow
SHA-256859 Mbps151 Mbps
5.67x slow
686 Mbps
1.25x slow
198 Mbps
4.35x slow
HMAC(SHA-256)858 Mbps688 Mbps
1.25x slow
SHA-3841.36 Gbps17.35 Mbps
78.24x slow
466 Mbps
2.91x slow
162 Mbps
8.37x slow
SHA-5121.36 Gbps17.59 Mbps
77.15x slow
470 Mbps
2.89x slow
160 Mbps
8.46x slow
SHA3-224857 Mbps14.97 Mbps
57.25x slow
SHA3-256857 Mbps14.17 Mbps
60.48x slow
SHA3-3841.36 Gbps10.85 Mbps
125.22x slow
SHA3-5121.37 Gbps7.49 Mbps
182.3x slow
RIPEMD-1281.79 Gbps247 Mbps
7.24x slow
RIPEMD-160698 Mbps174 Mbps
4x slow
290 Mbps
2.41x slow
RIPEMD-2562 Gbps218 Mbps
9.16x slow
RIPEMD-320684 Mbps161 Mbps
4.26x slow
BLAKE-2s1.49 Gbps
BLAKE-2b1.53 Gbps71.06 Mbps
21.6x slow
Poly13053.79 Gbps362 Mbps
10.48x slow
XXH324.5 Gbps
XXH642.42 Gbps
XXH3976 Mbps
XXH1281.03 Gbps
SM3751 Mbps135 Mbps
5.57x slow

With 1KB message (5000 iterations):

AlgorithmshashlibPointyCastlecryptohash
MD41.54 Gbps576 Mbps
2.67x slow
MD51.37 Gbps514 Mbps
2.66x slow
916 Mbps
1.49x slow
643 Mbps
2.12x slow
HMAC(MD5)1 Gbps735 Mbps
1.36x slow
482 Mbps
2.08x slow
SHA-11.16 Gbps348 Mbps
3.33x slow
714 Mbps
1.62x slow
371 Mbps
3.12x slow
HMAC(SHA-1)784 Mbps505 Mbps
1.55x slow
SHA-224780 Mbps177 Mbps
4.41x slow
616 Mbps
1.27x slow
187 Mbps
4.18x slow
SHA-256784 Mbps175 Mbps
4.48x slow
615 Mbps
1.27x slow
187 Mbps
4.19x slow
HMAC(SHA-256)549 Mbps433 Mbps
1.27x slow
SHA-3841.13 Gbps26.94 Mbps
42.01x slow
402 Mbps
2.82x slow
169 Mbps
6.71x slow
SHA-5121.13 Gbps27.88 Mbps
40.48x slow
402 Mbps
2.81x slow
170 Mbps
6.64x slow
SHA3-224782 Mbps20.34 Mbps
38.46x slow
SHA3-256783 Mbps20.55 Mbps
38.11x slow
SHA3-3841.13 Gbps16.25 Mbps
69.21x slow
SHA3-5121.13 Gbps10.89 Mbps
103.51x slow
RIPEMD-1281.62 Gbps334 Mbps
4.87x slow
RIPEMD-160642 Mbps207 Mbps
3.1x slow
280 Mbps
2.29x slow
RIPEMD-2561.81 Gbps339 Mbps
5.34x slow
RIPEMD-320636 Mbps198 Mbps
3.22x slow
BLAKE-2s1.43 Gbps
BLAKE-2b1.47 Gbps95.76 Mbps
15.38x slow
Poly13053.33 Gbps768 Mbps
4.33x slow
XXH324.12 Gbps
XXH642.27 Gbps
XXH3784 Mbps
XXH128825 Mbps
SM3698 Mbps163 Mbps
4.28x slow

With 10B message (100000 iterations):

AlgorithmshashlibPointyCastlecryptohash
MD4210 Mbps101 Mbps
2.09x slow
MD5177 Mbps89.19 Mbps
1.98x slow
92.89 Mbps
1.9x slow
52.25 Mbps
3.38x slow
HMAC(MD5)36.18 Mbps31.69 Mbps
1.14x slow
14.23 Mbps
2.54x slow
SHA-1120 Mbps49.47 Mbps
2.42x slow
69.51 Mbps
1.72x slow
34.29 Mbps
3.49x slow
HMAC(SHA-1)19.64 Mbps14.71 Mbps
1.34x slow
SHA-22488.2 Mbps26.95 Mbps
3.27x slow
58.57 Mbps
1.51x slow
21.69 Mbps
4.07x slow
SHA-25688.39 Mbps27.12 Mbps
3.26x slow
60.75 Mbps
1.45x slow
22.48 Mbps
3.93x slow
HMAC(SHA-256)14.35 Mbps12.45 Mbps
1.15x slow
SHA-38465.18 Mbps2.33 Mbps
27.95x slow
26.99 Mbps
2.42x slow
11.16 Mbps
5.84x slow
SHA-51264.21 Mbps2.35 Mbps
27.3x slow
26.59 Mbps
2.41x slow
11.39 Mbps
5.64x slow
SHA3-22488.02 Mbps1.57 Mbps
55.89x slow
SHA3-25689.16 Mbps1.56 Mbps
57.23x slow
SHA3-38464.5 Mbps1.57 Mbps
41.07x slow
SHA3-51263.11 Mbps1.57 Mbps
40.2x slow
RIPEMD-128177 Mbps58.35 Mbps
3.03x slow
RIPEMD-16086.81 Mbps33.18 Mbps
2.62x slow
32.16 Mbps
2.7x slow
RIPEMD-256180 Mbps53.17 Mbps
3.39x slow
RIPEMD-32085.18 Mbps30.48 Mbps
2.79x slow
BLAKE-2s152 Mbps
BLAKE-2b120 Mbps6.89 Mbps
17.44x slow
Poly1305269 Mbps167 Mbps
1.61x slow
XXH32413 Mbps
XXH64332 Mbps
XXH332.94 Mbps
XXH12833.16 Mbps
SM393.33 Mbps24.81 Mbps
3.76x slow

Key derivator algorithm benchmarks on different security parameters:

Algorithmslittlemoderategoodstrong
scrypt1.092 ms11.899 ms69.138 ms2100.983 ms
bcrypt1.803 ms14.474 ms226.341 ms1811.425 ms
pbkdf20.668 ms16.363 ms267.526 ms3211.098 ms
argon2i2.359 ms17.448 ms205.518 ms2375.301 ms
argon2d2.272 ms16.064 ms201.827 ms2374.41 ms
argon2id2.306 ms16.38 ms199.66 ms2376.102 ms

All benchmarks are done on AMD Ryzen 7 5800X processor and 3200MHz RAM using compiled exe

Dart SDK version: 3.7.0 (stable) (Wed Feb 5 04:53:58 2025 -0800) on "windows_x64"