Skip to content

Commit

Permalink
Modifies MACHash and MACHashBase interfaces for accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Sep 8, 2024
1 parent 1af6f44 commit c7f974c
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 271 deletions.
5 changes: 3 additions & 2 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/test
/script
/benchmark
/coverage
/benchmark
/doc
/build
/scripts
/.github
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.20.1

- Modifies `MACHash` and `MACHashBase` interfaces for accessibility.

# 1.20.0

- [![codecov](https://codecov.io/gh/bitanon/hashlib/graph/badge.svg?token=ISIYJ8MNI0)](https://codecov.io/gh/bitanon/hashlib)
Expand Down
14 changes: 7 additions & 7 deletions lib/src/algorithms/blake2/blake2b_32bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
int digestSize, {
this.key,
List<int>? salt,
List<int>? personalization,
List<int>? aad,
}) : hashLength = digestSize,
derivedKeyLength = digestSize,
super(1024 >>> 3) {
Expand Down Expand Up @@ -152,21 +152,21 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
}
}

if (personalization != null && personalization.isNotEmpty) {
if (personalization.length != 16) {
if (aad != null && aad.isNotEmpty) {
if (aad.length != 16) {
throw ArgumentError('The valid length of personalization is 16 bytes');
}
for (int i = 0, p = 0; i < 4; i++, p += 8) {
_s12 ^= (personalization[i] & 0xFF) << p;
_s12 ^= (aad[i] & 0xFF) << p;
}
for (int i = 4, p = 0; i < 8; i++, p += 8) {
_s13 ^= (personalization[i] & 0xFF) << p;
_s13 ^= (aad[i] & 0xFF) << p;
}
for (int i = 8, p = 0; i < 12; i++, p += 8) {
_s14 ^= (personalization[i] & 0xFF) << p;
_s14 ^= (aad[i] & 0xFF) << p;
}
for (int i = 12, p = 0; i < 16; i++, p += 8) {
_s15 ^= (personalization[i] & 0xFF) << p;
_s15 ^= (aad[i] & 0xFF) << p;
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/algorithms/blake2/blake2b_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
int digestSize, {
this.key,
List<int>? salt,
List<int>? personalization,
List<int>? aad,
}) : hashLength = digestSize,
derivedKeyLength = digestSize,
super(1024 >>> 3) {
Expand Down Expand Up @@ -116,15 +116,15 @@ class Blake2bHash extends BlockHashSink implements MACSinkBase {
}
}

if (personalization != null && personalization.isNotEmpty) {
if (personalization.length != 16) {
if (aad != null && aad.isNotEmpty) {
if (aad.length != 16) {
throw ArgumentError('The valid length of personalization is 16 bytes');
}
for (int i = 0, p = 0; i < 8; i++, p += 8) {
_s6 ^= (personalization[i] & 0xFF) << p;
_s6 ^= (aad[i] & 0xFF) << p;
}
for (int i = 8, p = 0; i < 16; i++, p += 8) {
_s7 ^= (personalization[i] & 0xFF) << p;
_s7 ^= (aad[i] & 0xFF) << p;
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/algorithms/blake2/blake2s.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Blake2sHash extends BlockHashSink implements MACSinkBase {
int digestSize, {
this.key,
List<int>? salt,
List<int>? personalization,
List<int>? aad,
}) : hashLength = digestSize,
derivedKeyLength = digestSize,
super(64) {
Expand Down Expand Up @@ -116,15 +116,15 @@ class Blake2sHash extends BlockHashSink implements MACSinkBase {
}
}

if (personalization != null && personalization.isNotEmpty) {
if (personalization.length != 8) {
if (aad != null && aad.isNotEmpty) {
if (aad.length != 8) {
throw ArgumentError('The valid length of personalization is 8 bytes');
}
for (int i = 0, p = 0; i < 4; i++, p += 8) {
_s6 ^= (personalization[i] & 0xFF) << p;
_s6 ^= (aad[i] & 0xFF) << p;
}
for (int i = 4, p = 0; i < 8; i++, p += 8) {
_s7 ^= (personalization[i] & 0xFF) << p;
_s7 ^= (aad[i] & 0xFF) << p;
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/pbkdf2/pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PBKDF2 extends KeyDerivatorBase {
String get name => '${algo.name}/PBKDF2';

/// The underlying algorithm used as Pseudo Random Function (PRF)
final MACHashBase algo;
final MACHash algo;

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

/// Create a [PBKDF2] instance with a MAC instance.
factory PBKDF2(
MACHashBase mac,
MACHash mac,
int iterations, {
List<int>? salt,
int? keyLength,
Expand Down Expand Up @@ -84,7 +84,7 @@ class PBKDF2 extends KeyDerivatorBase {
factory PBKDF2.fromSecurity(
PBKDF2Security security, {
List<int>? salt,
MACHashBase? mac,
MACHash? mac,
int? iterations,
int? keyLength,
}) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/pbkdf2/security.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PBKDF2Security {
final int dklen;

/// The underlying algorithm
final MACHashBase mac;
final MACHash mac;

const PBKDF2Security(
this.name, {
Expand Down
101 changes: 53 additions & 48 deletions lib/src/blake2b.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import 'package:hashlib/src/algorithms/blake2/blake2b.dart';
import 'package:hashlib/src/core/block_hash.dart';
import 'package:hashlib/src/core/mac_base.dart';

export 'algorithms/blake2/blake2b.dart' show Blake2bHash;

/// For generating un-keyed message digest with BLAKE2b-160.
///
/// Use [Blake2b] for keyed hash generation.
const Blake2b blake2b160 = Blake2b(160 >>> 3);
const Blake2b blake2b160 = Blake2b._(160 >>> 3);

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

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

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

/// Blake2b is a highly secure cryptographic hash function optimized for 64-bit
/// platforms. It generates hash values of data ranging from 1 to 64 bytes in
Expand All @@ -34,34 +36,47 @@ const Blake2b blake2b512 = Blake2b(512 >>> 3);
/// This implementation is based on the [RFC-7693][rfc]
///
/// [rfc]: https://www.ietf.org/rfc/rfc7693.html
class Blake2b extends BlockHashBase {
class Blake2b extends BlockHashBase<Blake2bHash> with MACHashBase<Blake2bHash> {
final List<int>? _key;
final List<int>? _salt;
final List<int>? _aad;

/// The number of bytes in the output.
final int digestSize;
final List<int>? salt;
final List<int>? personalization;

@override
String get name => 'BLAKE2b-${digestSize << 3}';
final String name;

const Blake2b._(
this.digestSize, [
this._key,
this._salt,
this._aad,
String? _name,
]) : name = _name ?? 'BLAKE2b-${digestSize << 3}';

/// Creates an instance to generate hash using BLAKE-2b algorithm.
///
/// Parameters:
/// - [digestSize] The number of bytes in the output.
/// - [salt] An optional nonce. Must be exactly 16 bytes long.
/// - [personalization] Second optional nonce. Must be exactly 16 bytes long.
/// - [aad] Second optional nonce. Must be exactly 16 bytes long.
///
/// See also:
/// - [mac] or [Blake2bMAC] for generating MAC with this algorithm.
const Blake2b(
this.digestSize, {
this.salt,
this.personalization,
});
factory Blake2b(
int digestSize, {
List<int>? salt,
List<int>? aad,
}) =>
Blake2b._(digestSize, null, salt, aad);

@override
Blake2bHash createSink() => Blake2bHash(
digestSize,
salt: salt,
personalization: personalization,
key: _key,
salt: _salt,
aad: _aad,
);

/// Get a builder to generate MAC using this algorithm.
Expand All @@ -72,49 +87,39 @@ class Blake2b extends BlockHashBase {
/// final message = 'plain message'.codeUnits;
/// final mac = blake2s256.mac.by(key).convert(message);
/// ```
Blake2bMAC get mac => Blake2bMAC._(this);

/// Get a new instance with different [salt] and [personalization] value.
///
/// If a parameter is null, it passes the current one to the new instance.
Blake2b config({
List<int>? salt,
List<int>? personalization,
}) =>
Blake2b(
digestSize,
salt: salt ?? this.salt,
personalization: personalization ?? this.personalization,
);
Blake2bMAC get mac => Blake2bMAC(digestSize);
}

class Blake2bMAC extends MACHashBase<Blake2bHash> {
final Blake2b _algo;

const Blake2bMAC._(this._algo);
class Blake2bMAC extends MACHash<Blake2bHash> {
/// The number of bytes in the output.
final int digestSize;

@override
String get name => '${_algo.name}/MAC';
final String name;

/// Creates an instance to generate MAC using BLAKE-2b algorithm.
///
/// Parameters:
/// - [digestSize] The number of bytes in the output.
///
/// See also:
/// - [Blake2b] for generating hash only.
const Blake2bMAC(
this.digestSize,
) : name = 'BLAKE2b-${digestSize << 3}/MAC';

/// Get an [MACHash] instance initialized by a [key].
/// Get an [MACHashBase] instance initialized by a [key].
///
/// Parameters:
/// - [key] An optional key for MAC generation. Should not exceed 64 bytes.
/// - [salt] An optional nonce. Must be exactly 16 bytes long.
/// - [personalization] Second optional nonce. Must be exactly 16 bytes long.
/// - [aad] Second optional nonce. Must be exactly 16 bytes long.
@override
@pragma('vm:prefer-inline')
MACHash<Blake2bHash> by(
MACHashBase<Blake2bHash> by(
List<int> key, {
List<int>? salt,
List<int>? personalization,
}) {
final sink = Blake2bHash(
_algo.digestSize,
key: key,
salt: salt ?? _algo.salt,
personalization: personalization ?? _algo.personalization,
);
return MACHash(name, sink);
}
List<int>? aad,
}) =>
Blake2b._(digestSize, key, salt, aad, name);
}
Loading

0 comments on commit c7f974c

Please sign in to comment.