Skip to content

Commit c7f974c

Browse files
committed
Modifies MACHash and MACHashBase interfaces for accessibility
1 parent 1af6f44 commit c7f974c

21 files changed

+251
-271
lines changed

.pubignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/test
2-
/script
3-
/benchmark
42
/coverage
3+
/benchmark
4+
/doc
55
/build
6+
/scripts
67
/.github

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.20.1
2+
3+
- Modifies `MACHash` and `MACHashBase` interfaces for accessibility.
4+
15
# 1.20.0
26

37
- [![codecov](https://codecov.io/gh/bitanon/hashlib/graph/badge.svg?token=ISIYJ8MNI0)](https://codecov.io/gh/bitanon/hashlib)

lib/src/algorithms/blake2/blake2b_32bit.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
100100
int digestSize, {
101101
this.key,
102102
List<int>? salt,
103-
List<int>? personalization,
103+
List<int>? aad,
104104
}) : hashLength = digestSize,
105105
derivedKeyLength = digestSize,
106106
super(1024 >>> 3) {
@@ -152,21 +152,21 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
152152
}
153153
}
154154

155-
if (personalization != null && personalization.isNotEmpty) {
156-
if (personalization.length != 16) {
155+
if (aad != null && aad.isNotEmpty) {
156+
if (aad.length != 16) {
157157
throw ArgumentError('The valid length of personalization is 16 bytes');
158158
}
159159
for (int i = 0, p = 0; i < 4; i++, p += 8) {
160-
_s12 ^= (personalization[i] & 0xFF) << p;
160+
_s12 ^= (aad[i] & 0xFF) << p;
161161
}
162162
for (int i = 4, p = 0; i < 8; i++, p += 8) {
163-
_s13 ^= (personalization[i] & 0xFF) << p;
163+
_s13 ^= (aad[i] & 0xFF) << p;
164164
}
165165
for (int i = 8, p = 0; i < 12; i++, p += 8) {
166-
_s14 ^= (personalization[i] & 0xFF) << p;
166+
_s14 ^= (aad[i] & 0xFF) << p;
167167
}
168168
for (int i = 12, p = 0; i < 16; i++, p += 8) {
169-
_s15 ^= (personalization[i] & 0xFF) << p;
169+
_s15 ^= (aad[i] & 0xFF) << p;
170170
}
171171
}
172172

lib/src/algorithms/blake2/blake2b_64bit.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
7878
int digestSize, {
7979
this.key,
8080
List<int>? salt,
81-
List<int>? personalization,
81+
List<int>? aad,
8282
}) : hashLength = digestSize,
8383
derivedKeyLength = digestSize,
8484
super(1024 >>> 3) {
@@ -116,15 +116,15 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
116116
}
117117
}
118118

119-
if (personalization != null && personalization.isNotEmpty) {
120-
if (personalization.length != 16) {
119+
if (aad != null && aad.isNotEmpty) {
120+
if (aad.length != 16) {
121121
throw ArgumentError('The valid length of personalization is 16 bytes');
122122
}
123123
for (int i = 0, p = 0; i < 8; i++, p += 8) {
124-
_s6 ^= (personalization[i] & 0xFF) << p;
124+
_s6 ^= (aad[i] & 0xFF) << p;
125125
}
126126
for (int i = 8, p = 0; i < 16; i++, p += 8) {
127-
_s7 ^= (personalization[i] & 0xFF) << p;
127+
_s7 ^= (aad[i] & 0xFF) << p;
128128
}
129129
}
130130

lib/src/algorithms/blake2/blake2s.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Blake2sHash extends BlockHashSink implements MACSinkBase {
7878
int digestSize, {
7979
this.key,
8080
List<int>? salt,
81-
List<int>? personalization,
81+
List<int>? aad,
8282
}) : hashLength = digestSize,
8383
derivedKeyLength = digestSize,
8484
super(64) {
@@ -116,15 +116,15 @@ class Blake2sHash extends BlockHashSink implements MACSinkBase {
116116
}
117117
}
118118

119-
if (personalization != null && personalization.isNotEmpty) {
120-
if (personalization.length != 8) {
119+
if (aad != null && aad.isNotEmpty) {
120+
if (aad.length != 8) {
121121
throw ArgumentError('The valid length of personalization is 8 bytes');
122122
}
123123
for (int i = 0, p = 0; i < 4; i++, p += 8) {
124-
_s6 ^= (personalization[i] & 0xFF) << p;
124+
_s6 ^= (aad[i] & 0xFF) << p;
125125
}
126126
for (int i = 4, p = 0; i < 8; i++, p += 8) {
127-
_s7 ^= (personalization[i] & 0xFF) << p;
127+
_s7 ^= (aad[i] & 0xFF) << p;
128128
}
129129
}
130130

lib/src/algorithms/pbkdf2/pbkdf2.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PBKDF2 extends KeyDerivatorBase {
3232
String get name => '${algo.name}/PBKDF2';
3333

3434
/// The underlying algorithm used as Pseudo Random Function (PRF)
35-
final MACHashBase algo;
35+
final MACHash algo;
3636

3737
/// The byte array containing salt
3838
final List<int> salt;
@@ -52,7 +52,7 @@ class PBKDF2 extends KeyDerivatorBase {
5252

5353
/// Create a [PBKDF2] instance with a MAC instance.
5454
factory PBKDF2(
55-
MACHashBase mac,
55+
MACHash mac,
5656
int iterations, {
5757
List<int>? salt,
5858
int? keyLength,
@@ -84,7 +84,7 @@ class PBKDF2 extends KeyDerivatorBase {
8484
factory PBKDF2.fromSecurity(
8585
PBKDF2Security security, {
8686
List<int>? salt,
87-
MACHashBase? mac,
87+
MACHash? mac,
8888
int? iterations,
8989
int? keyLength,
9090
}) =>

lib/src/algorithms/pbkdf2/security.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PBKDF2Security {
2222
final int dklen;
2323

2424
/// The underlying algorithm
25-
final MACHashBase mac;
25+
final MACHash mac;
2626

2727
const PBKDF2Security(
2828
this.name, {

lib/src/blake2b.dart

+53-48
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ import 'package:hashlib/src/algorithms/blake2/blake2b.dart';
55
import 'package:hashlib/src/core/block_hash.dart';
66
import 'package:hashlib/src/core/mac_base.dart';
77

8+
export 'algorithms/blake2/blake2b.dart' show Blake2bHash;
9+
810
/// For generating un-keyed message digest with BLAKE2b-160.
911
///
1012
/// Use [Blake2b] for keyed hash generation.
11-
const Blake2b blake2b160 = Blake2b(160 >>> 3);
13+
const Blake2b blake2b160 = Blake2b._(160 >>> 3);
1214

1315
/// For generating un-keyed message digest with BLAKE2b-256.
1416
///
1517
/// Use [Blake2b] for keyed hash generation.
16-
const Blake2b blake2b256 = Blake2b(256 >>> 3);
18+
const Blake2b blake2b256 = Blake2b._(256 >>> 3);
1719

1820
/// For generating un-keyed message digest with BLAKE2b-384.
1921
///
2022
/// Use [Blake2b] for keyed hash generation.
21-
const Blake2b blake2b384 = Blake2b(384 >>> 3);
23+
const Blake2b blake2b384 = Blake2b._(384 >>> 3);
2224

2325
/// For generating un-keyed message digest with BLAKE2b-512.
2426
///
2527
/// Use [Blake2b] for keyed hash generation.
26-
const Blake2b blake2b512 = Blake2b(512 >>> 3);
28+
const Blake2b blake2b512 = Blake2b._(512 >>> 3);
2729

2830
/// Blake2b is a highly secure cryptographic hash function optimized for 64-bit
2931
/// platforms. It generates hash values of data ranging from 1 to 64 bytes in
@@ -34,34 +36,47 @@ const Blake2b blake2b512 = Blake2b(512 >>> 3);
3436
/// This implementation is based on the [RFC-7693][rfc]
3537
///
3638
/// [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.
3845
final int digestSize;
39-
final List<int>? salt;
40-
final List<int>? personalization;
4146

4247
@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}';
4457

4558
/// Creates an instance to generate hash using BLAKE-2b algorithm.
4659
///
4760
/// Parameters:
4861
/// - [digestSize] The number of bytes in the output.
4962
/// - [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.
5164
///
5265
/// See also:
5366
/// - [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);
5973

6074
@override
6175
Blake2bHash createSink() => Blake2bHash(
6276
digestSize,
63-
salt: salt,
64-
personalization: personalization,
77+
key: _key,
78+
salt: _salt,
79+
aad: _aad,
6580
);
6681

6782
/// Get a builder to generate MAC using this algorithm.
@@ -72,49 +87,39 @@ class Blake2b extends BlockHashBase {
7287
/// final message = 'plain message'.codeUnits;
7388
/// final mac = blake2s256.mac.by(key).convert(message);
7489
/// ```
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);
8991
}
9092

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;
9596

9697
@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';
98110

99-
/// Get an [MACHash] instance initialized by a [key].
111+
/// Get an [MACHashBase] instance initialized by a [key].
100112
///
101113
/// Parameters:
102114
/// - [key] An optional key for MAC generation. Should not exceed 64 bytes.
103115
/// - [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.
105117
@override
106118
@pragma('vm:prefer-inline')
107-
MACHash<Blake2bHash> by(
119+
MACHashBase<Blake2bHash> by(
108120
List<int> key, {
109121
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);
120125
}

0 commit comments

Comments
 (0)