@@ -548,11 +548,20 @@ abstract class Argon2id extends KdfAlgorithm {
548548 ')' ;
549549}
550550
551- /// _BLAKE2B_ ([RFC 7693] (https://tools.ietf.org/html/rfc7693)) [HashAlgorithm] .
551+ /// _BLAKE2B_ ([RFC 7693] (https://tools.ietf.org/html/rfc7693)), which can be
552+ /// used both as [HashAlgorithm] and [MacAlgorithm] .
552553///
553554/// By default, [DartBlake2b] will be used.
554555///
555- /// ## Asynchronous usage
556+ /// ## Things to know
557+ /// * The default [hashLengthInBytes] / [macLength] is 64 bytes. You can
558+ /// choose a shorter hash length when you call the constructor. You should
559+ /// NOT truncate the hash yourself.
560+ /// * The algorithm was designed to be used directly as [MacAlgorithm] (no
561+ /// [Hmac] needed). Maximum secret key size is 64 bytes.
562+ /// * Blake2 hash/MAC function family includes also [Blake2s] .
563+ ///
564+ /// ## Example: Hashing a byte list
556565/// ```
557566/// import 'package:cryptography/cryptography.dart';
558567///
@@ -566,7 +575,7 @@ abstract class Argon2id extends KdfAlgorithm {
566575///
567576/// If you need synchronous computations, use [DartBlake2b] .
568577///
569- /// ## Streaming usage
578+ /// ## Example: Hashing a sequence of chunks
570579/// ```
571580/// import 'package:cryptography/cryptography.dart';
572581///
@@ -587,14 +596,47 @@ abstract class Argon2id extends KdfAlgorithm {
587596/// print('Hash: ${hash.bytes}');
588597/// }
589598/// ```
590- abstract class Blake2b extends HashAlgorithm {
591- factory Blake2b () {
592- return Cryptography .instance.blake2b ();
599+ abstract class Blake2b extends HashAlgorithm implements MacAlgorithm {
600+ /// Default value of [hashLengthInBytes] and [macLength] .
601+ static const int defaultHashLengthInBytes = 64 ;
602+
603+ factory Blake2b ({
604+ int hashLengthInBytes = defaultHashLengthInBytes,
605+ }) {
606+ if (hashLengthInBytes < 1 || hashLengthInBytes > defaultHashLengthInBytes) {
607+ throw ArgumentError .value (hashLengthInBytes);
608+ }
609+ return Cryptography .instance.blake2b (
610+ hashLengthInBytes: hashLengthInBytes,
611+ );
593612 }
594613
595- /// Constructor for classes that extend this class.
614+ /// Constructor for subclasses.
615+ const Blake2b .constructor ({
616+ this .hashLengthInBytes = defaultHashLengthInBytes,
617+ }) : assert (hashLengthInBytes > 0 ),
618+ assert (hashLengthInBytes <= defaultHashLengthInBytes);
596619
597- const Blake2b .constructor ();
620+ @override
621+ void checkParameters ({
622+ int ? length,
623+ required SecretKey secretKey,
624+ required int nonceLength,
625+ required int aadLength,
626+ required int keyStreamIndex,
627+ }) {}
628+
629+ @override
630+ int get keyStreamUsed => 0 ;
631+
632+ @override
633+ int get macLength => hashLengthInBytes;
634+
635+ @override
636+ bool get supportsAad => false ;
637+
638+ @override
639+ bool get supportsKeyStreamIndex => false ;
598640
599641 @override
600642 int get blockLengthInBytes => 64 ;
@@ -603,20 +645,39 @@ abstract class Blake2b extends HashAlgorithm {
603645 int get hashCode => (Blake2b ).hashCode;
604646
605647 @override
606- int get hashLengthInBytes => 64 ;
648+ final int hashLengthInBytes;
607649
608650 @override
609651 bool operator == (other) => other is Blake2b ;
610652
611653 @override
612- DartHashAlgorithm toSync () => const DartBlake2b ();
654+ DartBlake2b toSync () => DartBlake2b (
655+ hashLengthInBytes: hashLengthInBytes,
656+ );
657+
658+ @override
659+ String toString () {
660+ if (hashLengthInBytes == defaultHashLengthInBytes) {
661+ return 'Blake2b()' ;
662+ }
663+ return 'Blake2b(hashLengthInBytes: $hashLengthInBytes )' ;
664+ }
613665}
614666
615- /// _BLAKE2S_ ([RFC 7693] (https://tools.ietf.org/html/rfc7693)) [HashAlgorithm] .
667+ /// _BLAKE2S_ ([RFC 7693] (https://tools.ietf.org/html/rfc7693)), which can be
668+ /// used both as [HashAlgorithm] and [MacAlgorithm] .
616669///
617- /// By default, [DartBlake2s] , our pure Dart implementation, will be used.
670+ /// By default, [DartBlake2s] will be used.
618671///
619- /// ## Asynchronous usage
672+ /// ## Things to know
673+ /// * The default [hashLengthInBytes] / [macLength] is 32 bytes. You can
674+ /// choose a shorter hash length when you call the constructor. You should
675+ /// NOT truncate the hash yourself.
676+ /// * The algorithm was designed to be used directly as [MacAlgorithm] (no
677+ /// [Hmac] needed). Maximum secret key size is 32 bytes.
678+ /// * Blake2 hash/MAC function family includes also [Blake2b] .
679+ ///
680+ /// ## Example: Hashing a byte list
620681/// ```dart
621682/// import 'package:cryptography/cryptography.dart';
622683///
@@ -628,7 +689,7 @@ abstract class Blake2b extends HashAlgorithm {
628689/// }
629690/// ```
630691///
631- /// ## Streaming usage
692+ /// ## Example: Hashing a sequence of chunks
632693/// ```dart
633694/// import 'package:cryptography/cryptography.dart';
634695///
@@ -650,14 +711,26 @@ abstract class Blake2b extends HashAlgorithm {
650711/// }
651712/// ```
652713///
653- abstract class Blake2s extends HashAlgorithm {
654- factory Blake2s () {
655- return Cryptography .instance.blake2s ();
656- }
714+ abstract class Blake2s extends HashAlgorithm implements MacAlgorithm {
715+ /// Default value of [hashLengthInBytes] and [macLength] .
716+ static const int defaultHashLengthInBytes = 32 ;
657717
658- /// Constructor for classes that extend this class.
718+ factory Blake2s ({
719+ int hashLengthInBytes = defaultHashLengthInBytes,
720+ }) {
721+ if (hashLengthInBytes < 1 || hashLengthInBytes > defaultHashLengthInBytes) {
722+ throw ArgumentError .value (hashLengthInBytes);
723+ }
724+ return Cryptography .instance.blake2s (
725+ hashLengthInBytes: hashLengthInBytes,
726+ );
727+ }
659728
660- const Blake2s .constructor ();
729+ /// Constructor for subclasses.
730+ const Blake2s .constructor ({
731+ this .hashLengthInBytes = defaultHashLengthInBytes,
732+ }) : assert (hashLengthInBytes > 0 ),
733+ assert (hashLengthInBytes <= defaultHashLengthInBytes);
661734
662735 @override
663736 int get blockLengthInBytes => 32 ;
@@ -666,13 +739,44 @@ abstract class Blake2s extends HashAlgorithm {
666739 int get hashCode => (Blake2s ).hashCode;
667740
668741 @override
669- int get hashLengthInBytes => 32 ;
742+ final int hashLengthInBytes;
743+
744+ @override
745+ void checkParameters ({
746+ int ? length,
747+ required SecretKey secretKey,
748+ required int nonceLength,
749+ required int aadLength,
750+ required int keyStreamIndex,
751+ }) {}
752+
753+ @override
754+ int get keyStreamUsed => 0 ;
755+
756+ @override
757+ int get macLength => hashLengthInBytes;
758+
759+ @override
760+ bool get supportsAad => false ;
761+
762+ @override
763+ bool get supportsKeyStreamIndex => false ;
670764
671765 @override
672766 bool operator == (other) => other is Blake2s ;
673767
674768 @override
675- DartHashAlgorithm toSync () => const DartBlake2s ();
769+ String toString () {
770+ if (hashLengthInBytes == defaultHashLengthInBytes) {
771+ return 'Blake2s()' ;
772+ }
773+ return 'Blake2s(hashLengthInBytes: $hashLengthInBytes )' ;
774+ }
775+
776+ @override
777+ DartBlake2s toSync () => DartBlake2s (
778+ hashLengthInBytes: hashLengthInBytes,
779+ );
676780}
677781
678782/// _ChaCha20_ ([RFC 7539] (https://tools.ietf.org/html/rfc7539))
0 commit comments