Skip to content

Commit

Permalink
Modifies interface for Poly1305
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Sep 8, 2024
1 parent c7f974c commit e9940da
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.20.2

- Modifies interface for `Poly1305`

# 1.20.1

- Modifies `MACHash` and `MACHashBase` interfaces for accessibility.
Expand Down
64 changes: 40 additions & 24 deletions lib/src/poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,13 @@ import 'package:hashlib/src/core/mac_base.dart';

export 'algorithms/poly1305/poly1305_sink.dart' show Poly1305Sink;

/// The Poly1305 MAC (message authentication code) generator for an input
/// message using either 16 or 32-byte long authentication key.
const poly1305 = Poly1305();

class _Poly1305 extends HashBase<Poly1305Sink> with MACHashBase<Poly1305Sink> {
final Uint8List key;

const _Poly1305(this.key);

@override
final String name = 'Poly1305';

@override
Poly1305Sink createSink() => Poly1305Sink(key);
}

class Poly1305 extends MACHash<Poly1305Sink> {
const Poly1305();
class _Poly1305 extends MACHash<Poly1305Sink> {
const _Poly1305();

@override
final String name = 'Poly1305';

/// Create a new instance of [Poly1305] with a 16 or 32-byte long keypair.
/// Create a new instance of [_Poly1305] with a 16 or 32-byte long keypair.
/// The first 16-bytes will be used as a secret key to encode the message,
/// and the last 16-bytes will be used as the authentication key to sign it.
///
Expand All @@ -47,12 +31,12 @@ class Poly1305 extends MACHash<Poly1305Sink> {
/// allow for forgeries.
///
/// See also:
/// - [Poly1305.pair] to input key(`r`) and secret(`s`) pair separately.
/// - [_Poly1305.pair] to input key(`r`) and secret(`s`) pair separately.
@override
MACHashBase<Poly1305Sink> by(List<int> keypair) =>
_Poly1305(keypair is Uint8List ? keypair : Uint8List.fromList(keypair));
Poly1305(keypair is Uint8List ? keypair : Uint8List.fromList(keypair));

/// Creates a new instance of [Poly1305].
/// Creates a new instance of [_Poly1305].
///
/// Parameters:
/// - [key] is required and must contain exactly 16 bytes.
Expand All @@ -68,7 +52,7 @@ class Poly1305 extends MACHash<Poly1305Sink> {
/// allow for forgeries.
///
/// See also:
/// - [Poly1305.by] to input key(`r`) and secret(`s`) pair together.
/// - [_Poly1305.by] to input key(`r`) and secret(`s`) pair together.
MACHashBase<Poly1305Sink> pair(List<int> key, [List<int>? secret]) {
if (secret == null) {
return by(key);
Expand All @@ -82,10 +66,42 @@ class Poly1305 extends MACHash<Poly1305Sink> {
var pair = Uint8List(32);
pair.setAll(0, key);
pair.setAll(16, secret);
return _Poly1305(pair);
return Poly1305(pair);
}
}

/// The Poly1305 MAC (message authentication code) generator for an input
/// message using either 16 or 32-byte long authentication key.
const poly1305 = _Poly1305();

class Poly1305 extends HashBase<Poly1305Sink> with MACHashBase<Poly1305Sink> {
final Uint8List keypair;

@override
final String name = 'Poly1305';

/// Create a new instance of [_Poly1305] with a 16 or 32-byte long keypair.
/// The first 16-bytes will be used as a secret key to encode the message,
/// and the last 16-bytes will be used as the authentication key to sign it.
///
/// Parameters:
/// - [keypair] is required and must contain exactly 16 or 32 bytes.
///
/// If [keypair] length is 16 bytes, the final digest will not be signed.
///
/// **Warning**:
/// The algorithm is designed to ensure unforgeability of a message with a
/// random key. Authenticating multiple messages using the same key could
/// allow for forgeries.
///
/// See also:
/// - [_Poly1305.pair] to input key(`r`) and secret(`s`) pair separately.
const Poly1305(this.keypair);

@override
Poly1305Sink createSink() => Poly1305Sink(keypair);
}

/// Computes the Poly1305 MAC (message authentication code) of the given
/// [message] using the given the 16 or 32-byte long [keypair] for authentication.
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: hashlib
description: Secure hash functions, checksum generators, and key derivation algorithms optimized for Dart.
homepage: https://github.com/bitanon/hashlib
version: 1.20.1
version: 1.20.2

environment:
sdk: '>=2.14.0 <4.0.0'
Expand Down
2 changes: 1 addition & 1 deletion test/poly1305_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void main() {
0x28, 0x99, 0x57, 0x94, 0x41, 0x27, 0xd7, 0x5e,
];

var sink = Poly1305().by(key).createSink();
var sink = poly1305.by(key).createSink();
for (int i = 0; i < 256; i++) {
var mac = poly1305
.pair(
Expand Down

0 comments on commit e9940da

Please sign in to comment.