@@ -5,25 +5,27 @@ import 'package:hashlib/src/algorithms/blake2/blake2b.dart';
5
5
import 'package:hashlib/src/core/block_hash.dart' ;
6
6
import 'package:hashlib/src/core/mac_base.dart' ;
7
7
8
+ export 'algorithms/blake2/blake2b.dart' show Blake2bHash;
9
+
8
10
/// For generating un-keyed message digest with BLAKE2b-160.
9
11
///
10
12
/// Use [Blake2b] for keyed hash generation.
11
- const Blake2b blake2b160 = Blake2b (160 >>> 3 );
13
+ const Blake2b blake2b160 = Blake2b ._ (160 >>> 3 );
12
14
13
15
/// For generating un-keyed message digest with BLAKE2b-256.
14
16
///
15
17
/// Use [Blake2b] for keyed hash generation.
16
- const Blake2b blake2b256 = Blake2b (256 >>> 3 );
18
+ const Blake2b blake2b256 = Blake2b ._ (256 >>> 3 );
17
19
18
20
/// For generating un-keyed message digest with BLAKE2b-384.
19
21
///
20
22
/// Use [Blake2b] for keyed hash generation.
21
- const Blake2b blake2b384 = Blake2b (384 >>> 3 );
23
+ const Blake2b blake2b384 = Blake2b ._ (384 >>> 3 );
22
24
23
25
/// For generating un-keyed message digest with BLAKE2b-512.
24
26
///
25
27
/// Use [Blake2b] for keyed hash generation.
26
- const Blake2b blake2b512 = Blake2b (512 >>> 3 );
28
+ const Blake2b blake2b512 = Blake2b ._ (512 >>> 3 );
27
29
28
30
/// Blake2b is a highly secure cryptographic hash function optimized for 64-bit
29
31
/// platforms. It generates hash values of data ranging from 1 to 64 bytes in
@@ -34,34 +36,47 @@ const Blake2b blake2b512 = Blake2b(512 >>> 3);
34
36
/// This implementation is based on the [RFC-7693][rfc]
35
37
///
36
38
/// [rfc] : https://www.ietf.org/rfc/rfc7693.html
37
- class Blake2b extends BlockHashBase {
39
+ class Blake2b extends BlockHashBase <Blake2bHash > with MACHashBase <Blake2bHash > {
40
+ final List <int >? _key;
41
+ final List <int >? _salt;
42
+ final List <int >? _aad;
43
+
44
+ /// The number of bytes in the output.
38
45
final int digestSize;
39
- final List <int >? salt;
40
- final List <int >? personalization;
41
46
42
47
@override
43
- String get name => 'BLAKE2b-${digestSize << 3 }' ;
48
+ final String name;
49
+
50
+ const Blake2b ._(
51
+ this .digestSize, [
52
+ this ._key,
53
+ this ._salt,
54
+ this ._aad,
55
+ String ? _name,
56
+ ]) : name = _name ?? 'BLAKE2b-${digestSize << 3 }' ;
44
57
45
58
/// Creates an instance to generate hash using BLAKE-2b algorithm.
46
59
///
47
60
/// Parameters:
48
61
/// - [digestSize] The number of bytes in the output.
49
62
/// - [salt] An optional nonce. Must be exactly 16 bytes long.
50
- /// - [personalization ] Second optional nonce. Must be exactly 16 bytes long.
63
+ /// - [aad ] Second optional nonce. Must be exactly 16 bytes long.
51
64
///
52
65
/// See also:
53
66
/// - [mac] or [Blake2bMAC] for generating MAC with this algorithm.
54
- const Blake2b (
55
- this .digestSize, {
56
- this .salt,
57
- this .personalization,
58
- });
67
+ factory Blake2b (
68
+ int digestSize, {
69
+ List <int >? salt,
70
+ List <int >? aad,
71
+ }) =>
72
+ Blake2b ._(digestSize, null , salt, aad);
59
73
60
74
@override
61
75
Blake2bHash createSink () => Blake2bHash (
62
76
digestSize,
63
- salt: salt,
64
- personalization: personalization,
77
+ key: _key,
78
+ salt: _salt,
79
+ aad: _aad,
65
80
);
66
81
67
82
/// Get a builder to generate MAC using this algorithm.
@@ -72,49 +87,39 @@ class Blake2b extends BlockHashBase {
72
87
/// final message = 'plain message'.codeUnits;
73
88
/// final mac = blake2s256.mac.by(key).convert(message);
74
89
/// ```
75
- Blake2bMAC get mac => Blake2bMAC ._(this );
76
-
77
- /// Get a new instance with different [salt] and [personalization] value.
78
- ///
79
- /// If a parameter is null, it passes the current one to the new instance.
80
- Blake2b config ({
81
- List <int >? salt,
82
- List <int >? personalization,
83
- }) =>
84
- Blake2b (
85
- digestSize,
86
- salt: salt ?? this .salt,
87
- personalization: personalization ?? this .personalization,
88
- );
90
+ Blake2bMAC get mac => Blake2bMAC (digestSize);
89
91
}
90
92
91
- class Blake2bMAC extends MACHashBase <Blake2bHash > {
92
- final Blake2b _algo;
93
-
94
- const Blake2bMAC ._(this ._algo);
93
+ class Blake2bMAC extends MACHash <Blake2bHash > {
94
+ /// The number of bytes in the output.
95
+ final int digestSize;
95
96
96
97
@override
97
- String get name => '${_algo .name }/MAC' ;
98
+ final String name;
99
+
100
+ /// Creates an instance to generate MAC using BLAKE-2b algorithm.
101
+ ///
102
+ /// Parameters:
103
+ /// - [digestSize] The number of bytes in the output.
104
+ ///
105
+ /// See also:
106
+ /// - [Blake2b] for generating hash only.
107
+ const Blake2bMAC (
108
+ this .digestSize,
109
+ ) : name = 'BLAKE2b-${digestSize << 3 }/MAC' ;
98
110
99
- /// Get an [MACHash ] instance initialized by a [key] .
111
+ /// Get an [MACHashBase ] instance initialized by a [key] .
100
112
///
101
113
/// Parameters:
102
114
/// - [key] An optional key for MAC generation. Should not exceed 64 bytes.
103
115
/// - [salt] An optional nonce. Must be exactly 16 bytes long.
104
- /// - [personalization ] Second optional nonce. Must be exactly 16 bytes long.
116
+ /// - [aad ] Second optional nonce. Must be exactly 16 bytes long.
105
117
@override
106
118
@pragma ('vm:prefer-inline' )
107
- MACHash <Blake2bHash > by (
119
+ MACHashBase <Blake2bHash > by (
108
120
List <int > key, {
109
121
List <int >? salt,
110
- List <int >? personalization,
111
- }) {
112
- final sink = Blake2bHash (
113
- _algo.digestSize,
114
- key: key,
115
- salt: salt ?? _algo.salt,
116
- personalization: personalization ?? _algo.personalization,
117
- );
118
- return MACHash (name, sink);
119
- }
122
+ List <int >? aad,
123
+ }) =>
124
+ Blake2b ._(digestSize, key, salt, aad, name);
120
125
}
0 commit comments