Blake2b class abstract

BLAKE2B (RFC 7693), which can be used both as HashAlgorithm and MacAlgorithm.

By default, DartBlake2b will be used.

Things to know

  • The default hashLengthInBytes / macLength is 64 bytes. You can choose a shorter hash length when you call the constructor. You should NOT truncate the hash yourself.
  • The algorithm was designed to be used directly as MacAlgorithm (no Hmac needed). Maximum secret key size is 64 bytes.
  • Blake2 hash/MAC function family includes also Blake2s.

Example: Hashing a byte list

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
  final algorithm = Blake2b();
  final message = <int>[1,2,3];
  final hash = await algorithm.hash(message);
  print('Hash: ${hash.bytes}');
}

If you need synchronous computations, use DartBlake2b.

Example: Hashing a sequence of chunks

import 'package:cryptography/cryptography.dart';

void main() async {
  final algorithm = Blake2b();

  // Create a sink
  final sink = algorithm.newSink();

  // Add any number of chunks
  sink.add(<int>[1,2,3]);
  sink.add(<int>[4,5]);

  // Calculate the hash
  sink.close();
  final hash = await sink.hash();

  print('Hash: ${hash.bytes}');
}
Inheritance
Implemented types
Implementers

Constructors

Blake2b({int hashLengthInBytes = defaultHashLengthInBytes})
factory
Blake2b.constructor({int hashLengthInBytes = defaultHashLengthInBytes})
Constructor for subclasses.
const

Properties

blockLengthInBytes → int
The internal block size in bytes. This information is required by some algorithms such as Hmac.
no setteroverride
hashCode → int
The hash code for this object.
no setteroverride
hashLengthInBytes → int
Digest size in bytes.
final
keyStreamUsed → int
Number of bytes in key stream used to initialize the MAC algorithm.
no setteroverride
macLength → int
Number of bytes in the message authentication code.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
supportsAad → bool
Whether the algorithm supports Associated Authenticated Data (AAD).
no setteroverride
supportsKeyStreamIndex → bool
Whether the algorithm supports key stream index.
no setteroverride

Methods

calculateMac(List<int> bytes, {required SecretKey secretKey, List<int> nonce = const <int>[], List<int> aad = const <int>[]}) → Future<Mac>
Calculates message authentication code.
inherited
checkParameters({int? length, required SecretKey secretKey, required int nonceLength, required int aadLength, required int keyStreamIndex}) → void
Checks parameters and throws ArgumentError if they are invalid.
override
hash(List<int> input) → Future<Hash>
Calculates hash for the argument.
inherited
newHashSink() → HashSink
Constructs a sink for hashing chunks.
inherited
newMacSink({required SecretKey secretKey, List<int> nonce = const <int>[], List<int> aad = const <int>[]}) → Future<MacSink>
Constructs a sink for calculating a Mac.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
replace({int? hashLength}) → Blake2b
Enables you to replace hashLengthInBytes.
toString() → String
A string representation of this object.
override
toSync() → DartBlake2b
For synchronous computations, returns a pure Dart implementation of the hash algorithm.
override

Operators

operator ==(Object other) → bool
The equality operator.
override

Constants

defaultHashLengthInBytes → const int
Default value of hashLengthInBytes and macLength.