crypto 2.0.6

  • Installing
  • Versions
  • 99

Cryptographic hashing functions for Dart

A set of cryptographic hashing functions implemented in pure Dart

The following hashing algorithms are supported:

  • SHA-1
  • SHA-256
  • MD5
  • HMAC (i.e. HMAC-MD5, HMAC-SHA1, HMAC-SHA256)


Digest on a single input

To hash a list of bytes, invoke the convert method on the sha1, sha256 or md5 objects.

import 'package:crypto/crypto.dart';
import 'dart:convert'; // for the utf8.encode method

void main() {
  var bytes = utf8.encode("foobar"); // data being hashed

  var digest = sha1.convert(bytes);

  print("Digest as bytes: ${digest.bytes}");
  print("Digest as hex string: $digest");

Digest on chunked input

If the input data is not available as a single list of bytes, use the chunked conversion approach.

Invoke the startChunkedConversion method to create a sink for the input data. On the sink, invoke the add method for each chunk of input data, and invoke the close method when all the chunks have been added. The digest can then be retrieved from the Sink<Digest> used to create the input data sink.

import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:crypto/src/digest_sink.dart';

void main() {
  var firstChunk = utf8.encode("foo");
  var secondChunk = utf8.encode("bar");

  var ds = new DigestSink();
  var s = sha1.startChunkedConversion(ds);
  s.add(secondChunk); // call `add` for every chunk of input data
  var digest = ds.value;

  print("Digest as bytes: ${digest.bytes}");
  print("Digest as hex string: $digest");

The above example uses the DigestSink class that comes with the crypto package. Its value property retrieves the last Digest that was added to it, which is fine for this purpose since only one Digest is added to it when the data sink's close method was invoked.


Create an instance of the Hmac class with the hash function and secret key being used. The object can then be used like the other hash calculating objects.

import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:crypto/src/digest_sink.dart';

void main() {
  var key = utf8.encode('p@ssw0rd');
  var bytes = utf8.encode("foobar");

  var hmacSha256 = new Hmac(sha256, key); // HMAC-SHA256
  var digest = hmacSha256.convert(bytes);
  print("HMAC digest as bytes: ${digest.bytes}");
  print("HMAC digest as hex string: $digest");


Support for this library is given as best effort.

This library has not been reviewed or vetted by security professionals.

Features and bugs

Please file feature requests and bugs at the issue tracker.


  • Changed the max message size instead to 0x3ffffffffffff, which is the largest portable value for both JS and the Dart VM.


  • Made max message size a BigNum instead of an int so that dart2js can compile with crypto.


  • Updated SDK version to 2.0.0-dev.17.0


  • Fix SDK constraint.


  • Prepare HashSink implementation for limiting integers to 64 bits in Dart language.


  • Support convert 2.0.0.


Note: There are no APIs in 2.0.0 that weren't also in 0.9.2. Packages that would use 2.0.0 as a lower bound should use 0.9.2 instead—for example, crypto: ">=0.9.2 <3.0.0".

  • Hash and Hmac no longer extend ChunkedConverter.


  • Properly close sinks passed to Hash.startChunkedConversion() when ByteConversionSink.close() is called.


  • Hmac and Hash now extend the new ChunkedConverter class from dart:convert.

  • Fix all strong mode warnings.


  • All APIs that were deprecated in 0.9.2 have been removed. No new APIs have been added. Packages that would use 1.0.0 as a lower bound should use 0.9.2 instead—for example, crypto: ">=0.9.2 <2.0.0".


  • Avoid core library methods that don't work on dart2js.


  • Hash, MD5, SHA1, and SHA256 now implement Converter. They convert between List<int>s and the new Digest class, which represents a hash digest. The Converter APIs—Hash.convert() and Hash.startChunkedConversion—should be used in preference to the old APIs, which are now deprecated.

  • SHA1, SHA256, and HMAC have been renamed to Sha1, Sha256, and Hmac, respectively. The old names still work, but are deprecated.

  • Top-level sha1, sha256, and md5 fields have been added to make it easier to use those hash algorithms without having to instantiate new instances.

  • Hashing now works correctly for input sizes up to 2^64 bytes.


  • Hash.add, Hash.close, and Hash.newInstance are deprecated. Hash.convert should be used for hashing single values, and Hash.startChunkedConversion should be used for hashing streamed values.

  • SHA1 and SHA256 are deprecated. Use the top-level sha1 and sha256 fields instead.

  • While the MD5 class is not deprecated, the new MD5() constructor is. Use the top-level md5 field instead.

  • HMAC is deprecated. Use Hmac instead.

  • Base64Codec, Base64Encoder, Base64Decoder, Base64EncoderSink, Base64DecoderSink, and BASE64 are deprecated. Use the Base64 APIs in dart:convert instead.

  • CryptoUtils is deprecated. Use the Base64 APIs in dart:convert and the hex APIs in the convert package instead.


  • Base64 convert returns an Uint8List
  • Base64 codec and encoder can now take an encodePaddingCharacter
  • Implement a Base64 codec similar to codecs in 'dart:convert'


  • ChangeLog starts here.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  crypto: ^2.0.6

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:crypto/crypto.dart';
Version Uploaded Documentation Archive
2.0.6 Jul 13, 2018 Go to the documentation of crypto 2.0.6 Download crypto 2.0.6 archive
2.0.5 Jun 15, 2018 Go to the documentation of crypto 2.0.5 Download crypto 2.0.5 archive
2.0.3 May 3, 2018 Go to the documentation of crypto 2.0.3 Download crypto 2.0.3 archive
2.0.2+1 Sep 19, 2017 Go to the documentation of crypto 2.0.2+1 Download crypto 2.0.2+1 archive
2.0.2 Jul 19, 2017 Go to the documentation of crypto 2.0.2 Download crypto 2.0.2 archive
2.0.1 Jun 3, 2016 Go to the documentation of crypto 2.0.1 Download crypto 2.0.1 archive
2.0.0 Jun 3, 2016 Go to the documentation of crypto 2.0.0 Download crypto 2.0.0 archive
1.1.1 Apr 21, 2016 Go to the documentation of crypto 1.1.1 Download crypto 1.1.1 archive
1.1.0 Apr 14, 2016 Go to the documentation of crypto 1.1.0 Download crypto 1.1.0 archive
1.0.0 Mar 28, 2016 Go to the documentation of crypto 1.0.0 Download crypto 1.0.0 archive

All 63 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:crypto/crypto.dart.


The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use crypto.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.17.0 <3.0.0
collection ^1.0.0 1.14.11
convert >=1.0.0 <3.0.0 2.0.2
typed_data ^1.0.0 1.1.6
Transitive dependencies
charcode 1.1.2
Dev dependencies
test ^1.0.0