Stop testing Dartium on Travis (#46)

1 file changed
tree: ae5bf40b1c0d7f533d8f4337a7ddacb44e998610
  1. example/
  2. lib/
  3. test/
  4. .gitignore
  5. .status
  6. .test_config
  7. .travis.yml
  8. analysis_options.yaml
  9. AUTHORS
  10. CHANGELOG.md
  11. codereview.settings
  12. CONTRIBUTING.md
  13. LICENSE
  14. pubspec.yaml
  15. README.md
README.md

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)

Usage

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(firstChunk);
  s.add(secondChunk); // call `add` for every chunk of input data
  s.close();
  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.

HMAC

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");
}

Disclaimer

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.