hash 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new66

Cryptographic hashing functions for Dart #

A set of cryptographic hashing functions implemented in pure Dart

The following hashing algorithms are supported:

  • MD5
  • RIPEMD-160
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512
  • HMAC

Depend on it #

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

dependencies:
  hash: ^1.0.1

MD5 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("MD5 digest as bytes: ${MD5().update(hash).digest()}");
  /// => MD5 digest as bytes: [157, 161, 39, 157, 129, 83, 38, 177, 20, 176, 28, 102, 71, 59, 161, 99]
}

RIPEMD-160 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("RIPEMD160 digest as bytes: ${RIPEMD().update(hash).digest()}");
  /// => RIPEMD160 digest as bytes: [106, 0, 130, 179, 127, 111, 174, 128, 101, 46, 44, 174, 206, 48, 221, 6, 84, 178, 249, 54]
}

SHA-1 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("SHA1 digest as bytes: ${SHA1().update(hash).digest()}");
  /// => SHA1 digest as bytes: [89, 77, 139, 19, 242, 92, 237, 227, 192, 203, 65, 46, 162, 206, 69, 86, 38, 40, 133, 129]
}

SHA-224 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("SHA224 digest as bytes: ${SHA224().update(hash).digest()}");
  /// => SHA224 digest as bytes: [65, 21, 152, 248, 205, 84, 119, 117, 173, 117, 239, 216, 31, 240, 51, 11, 2, 74, 57, 0, 240, 120, 16, 202, 89, 114, 195, 156]
}

SHA-256 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("SHA256 digest as bytes: ${SHA256().update(hash).digest()}");
  /// => SHA256 digest as bytes: [190, 194, 99, 8, 250, 99, 204, 92, 152, 27, 174, 217, 152, 110, 5, 15, 12, 18, 30, 166, 70, 222, 186, 8, 65, 188, 127, 150, 3, 103, 191, 161]
}

SHA-384 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("SHA384 digest as bytes: ${SHA384().update(hash).digest()}");
  /// => SHA384 digest as bytes: [255, 109, 245, 134, 18, 229, 151, 64, 118, 141, 233, 133, 118, 18, 79, 238, 244, 54, 40, 231, 169, 89, 34, 186, 138, 82, 38, 3, 66, 242, 245, 157, 34, 29, 82, 79, 157, 0, 79, 1, 29, 128, 141, 105, 7, 123, 41, 240]
}

SHA-512 #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("SHA512 digest as bytes: ${SHA512().update(hash).digest()}");
  /// => SHA512 digest as bytes: [68, 17, 163, 18, 60, 62, 59, 176, 168, 3, 74, 33, 193, 202, 168, 157, 140, 188, 220, 213, 245, 97, 162, 84, 13, 178, 152, 188, 176, 243, 238, 209, 194, 238, 44, 11, 59, 100, 228, 19, 43, 181, 140, 127, 165, 69, 11, 206, 146, 129, 254, 203, 135, 91, 186, 104, 175, 145, 8, 155, 7, 184, 56, 56]
}

HMAC #

import 'package:hash/hash.dart';

void main() {
  var hash = [96, 191, 108, 70, 168, 246, 217, 160, 43, 181, 160, 241, 248, 105, 30, 176, 215, 208, 207, 100, 148, 36, 244, 211, 133, 189, 243, 31, 194, 97, 180, 190];
  print("HMAC digest as bytes: ${Hmac(SHA256(), hash).update(hash).digest()}");
  /// => HMAC digest as bytes: [13, 122, 158, 40, 212, 24, 185, 91, 221, 252, 165, 239, 33, 137, 147, 40, 112, 91, 216, 241, 221, 170, 60, 149, 60, 154, 67, 144, 6, 158, 65, 91]
}

1.0.3 #

  • [FIX] Hmac reset

1.0.2 #

  • [FIX] outSize

1.0.1 #

  • Support MD5

1.0.0 #

  • modify version

0.0.1 #

  • Support RIPEMD160, Hmac, sha1, sha224, sha256, sha384, sha512
  • Initial version, created by litao

example/hash_example.dart

import 'package:hash/hash.dart';

/// hex encode
String encodeHEX(List<int> bytes) {
  var str = '';
  for (var i = 0; i < bytes.length; i++) {
    var s = bytes[i].toRadixString(16);
    str += s.padLeft(2 - s.length, '0');
  }
  return str;
}

/// hex decode
List<int> decodeHEX(String hex) {
  var bytes = <int>[];
  var len = hex.length ~/ 2;
  for (var i = 0; i < len; i++) {
    bytes.add(int.parse(hex.substring(i * 2, i * 2 + 2), radix: 16));
  }
  return bytes;
}

void main() {
  var hash = decodeHEX(
      '60bf6c46a8f6d9a02bb5a0f1f8691eb0d7d0cf649424f4d385bdf31fc261b4be');

  ///MD5
  var md5 = MD5();
  print(md5.update(hash).digest());

  /// => [157, 161, 39, 157, 129, 83, 38, 177, 20, 176, 28, 102, 71, 59, 161, 99]
  print(md5.outSize);

  /// => 16

  ///RIPEMD160
  var ripemd160 = RIPEMD160();
  print(ripemd160.update(hash).digest());

  /// => [106, 0, 130, 179, 127, 111, 174, 128, 101, 46, 44, 174, 206, 48, 221, 6, 84, 178, 249, 54]
  print(ripemd160.outSize);

  /// => 20

  ///sha1
  var sha1 = SHA1();
  print(sha1.update(hash).digest());

  /// => [89, 77, 139, 19, 242, 92, 237, 227, 192, 203, 65, 46, 162, 206, 69, 86, 38, 40, 133, 129]
  print(sha1.outSize);

  /// => 20

  ///sha2224
  var sha224 = SHA224();
  print(sha224.update(hash).digest());

  /// => [65, 21, 152, 248, 205, 84, 119, 117, 173, 117, 239, 216, 31, 240, 51, 11, 2, 74, 57, 0, 240, 120, 16, 202, 89, 114, 195, 156]
  print(sha224.outSize);

  /// => 28

  ///sha256
  var sha256 = SHA256();
  print(sha256.update(hash).digest());

  /// => [190, 194, 99, 8, 250, 99, 204, 92, 152, 27, 174, 217, 152, 110, 5, 15, 12, 18, 30, 166, 70, 222, 186, 8, 65, 188, 127, 150, 3, 103, 191, 161]
  print(sha256.outSize);

  /// => 32

  ///sha384
  var sha384 = SHA384();
  print(sha384.update(hash).digest());

  /// => [255, 109, 245, 134, 18, 229, 151, 64, 118, 141, 233, 133, 118, 18, 79, 238, 244, 54, 40, 231, 169, 89, 34, 186, 138, 82, 38, 3, 66, 242, 245, 157, 34, 29, 82, 79, 157, 0, 79, 1, 29, 128, 141, 105, 7, 123, 41, 240]
  print(sha384.outSize);

  /// => 48

  ///sha512
  var sha512 = SHA512();
  print(sha512.update(hash).digest());

  /// => [68, 17, 163, 18, 60, 62, 59, 176, 168, 3, 74, 33, 193, 202, 168, 157, 140, 188, 220, 213, 245, 97, 162, 84, 13, 178, 152, 188, 176, 243, 238, 209, 194, 238, 44, 11, 59, 100, 228, 19, 43, 181, 140, 127, 165, 69, 11, 206, 146, 129, 254, 203, 135, 91, 186, 104, 175, 145, 8, 155, 7, 184, 56, 56]
  print(sha512.outSize);

  /// => 64

  ///Hmac
  var hmac = Hmac(SHA256(), hash);
  print(hmac.update(hash).digest());

  /// => [13, 122, 158, 40, 212, 24, 185, 91, 221, 252, 165, 239, 33, 137, 147, 40, 112, 91, 216, 241, 221, 170, 60, 149, 60, 154, 67, 144, 6, 158, 65, 91]
  print(hmac.outSize);
  /// => 32
  hmac.reset();
  print(hmac.update(hash).digest());
  /// => [13, 122, 158, 40, 212, 24, 185, 91, 221, 252, 165, 239, 33, 137, 147, 40, 112, 91, 216, 241, 221, 170, 60, 149, 60, 154, 67, 144, 6, 158, 65, 91]
}

Use this package as a library

1. Depend on it

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


dependencies:
  hash: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:hash/hash.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0