PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
270 stars 76 forks source link

`Blake2bDigest` with explicit key triggers `RangeError` #186

Open objectx opened 5 years ago

objectx commented 5 years ago

Following code should run without errors, but h.process throws a RangeError.

import 'dart:convert';
import 'dart:typed_data';

import 'package:typed_data/typed_data.dart';
import 'package:pointycastle/digests/blake2b.dart';

main() {
  final enc = AsciiEncoder();
  final key = enc.convert('foo');
  //final key = null;
  final h = Blake2bDigest(key: key, salt: makeBlake2Salt(enc.convert('bar')));
  final msg = enc.convert('baz');
  final out = h.process(msg);
  print('out = ${_dumpHex(out)}');
}

Uint8List makeBlake2Salt(Uint8List s) {
  final b = Uint8Buffer(16);
  b
    ..fillRange(0, 16, 0)
    ..setRange(0, s.length, s);
  assert(b.length == 16);
  return b.buffer.asUint8List();
}

String _dumpHex(Uint8List l) {
  return l.map((x) => x.toRadixString(16).padLeft(2, '0')).join('');
}

Digging into the source, I've found:

duanyytop commented 4 years ago

I also encountered similar problems. https://github.com/PointyCastle/pointycastle/issues/221

duanyytop commented 4 years ago

I have found another blake2b library which can work well. https://github.com/ilap/pinenacl-dart