diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b7b9d2..f859c7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.20.2 + +- Modifies interface for `Poly1305` + # 1.20.1 - Modifies `MACHash` and `MACHashBase` interfaces for accessibility. diff --git a/lib/src/poly1305.dart b/lib/src/poly1305.dart index c10162e..627a9b9 100644 --- a/lib/src/poly1305.dart +++ b/lib/src/poly1305.dart @@ -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 with MACHashBase { - final Uint8List key; - - const _Poly1305(this.key); - - @override - final String name = 'Poly1305'; - - @override - Poly1305Sink createSink() => Poly1305Sink(key); -} - -class Poly1305 extends MACHash { - const Poly1305(); +class _Poly1305 extends MACHash { + 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. /// @@ -47,12 +31,12 @@ class Poly1305 extends MACHash { /// 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 by(List 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. @@ -68,7 +52,7 @@ class Poly1305 extends MACHash { /// 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 pair(List key, [List? secret]) { if (secret == null) { return by(key); @@ -82,10 +66,42 @@ class Poly1305 extends MACHash { 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 with MACHashBase { + 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. /// diff --git a/pubspec.yaml b/pubspec.yaml index 180e703..35d05b4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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' diff --git a/test/poly1305_test.dart b/test/poly1305_test.dart index 098d40f..43d90c6 100644 --- a/test/poly1305_test.dart +++ b/test/poly1305_test.dart @@ -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(