Hmac class abstract

HMAC, a widely used MacAlgorithm.

DartHmac is the pure Dart implementation of the HMAC algorithm. It's used when no faster implementation is available.

In browsers, "package:cryptography" will automatically attempt to use Web Crypto API, which has very good HMAC performance.

Flutter developers should add cryptography_flutter, as a dependency for the best possible HMAC performance.

Things to know

  • You must choose some HashAlgorithm. We have shorthands constructors such as Hmac.sha256, but the hash algorithm can be anything.
  • HMAC does not support nonces. Our implementation ignores any nonce.
  • HMAC does not support AAD (Associated Authenticated Data). Our implementation throws ArgumentError if you try to give some.

Constructors

Example

import 'package:cryptography/cryptography.dart';

void main() async {
  final message = [1,2,3];
  final secretKey = SecretKey([4,5,6]);

  // In our example, we calculate HMAC-SHA256
  final hmac = Hmac.sha256();
  final mac = await hmac.calculateMac(
    message,
    secretKey: secretKey,
  );
}

Example: synchronous usage

DartHmac, a pure Dart implementation of HMAC, can be used when you absolutely need do computations synchronously:

import 'package:cryptography/cryptography.dart';
import 'package:cryptography/dart.dart';

void main() {
  final algorithm = DartHmac.sha256();
  final mac = algorithm.calculateMacSync(
    bytes,
    secretKey: secretKey,
  );
}
Inheritance
Implementers

Constructors

Hmac(HashAlgorithm hashAlgorithm)
Constructs HMAC with any hash algorithm.
factory
Hmac.blake2b()
HMAC-BLAKE2B.
factory
Hmac.blake2s()
HMAC-BLAKE2S.
factory
Hmac.constructor()
Constructor for classes that extend this class.
const
Hmac.sha1()
HMAC with Sha1.
factory
Hmac.sha224()
HMAC with Sha224.
factory
Hmac.sha256()
HMAC with Sha256.
factory
Hmac.sha384()
HMAC with Sha384.
factory
Hmac.sha512()
HMAC with Sha512.
factory

Properties

hashAlgorithm HashAlgorithm
Hash algorithm used by the HMAC.
no setter
hashCode int
The hash code for this object.
no setteroverride
keyStreamUsed int
Number of bytes in key stream used to initialize the MAC algorithm.
no setterinherited
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 setterinherited
supportsKeyStreamIndex bool
Whether the algorithm supports key stream index.
no setterinherited

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.
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
toString() String
A string representation of this object.
override
toSync() DartHmac
Returns a synchronous implementation of this algorithm.
override

Operators

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