Skip to content

Commit

Permalink
Bump version for hashlib and hashlib_codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Sep 8, 2024
1 parent d387ac4 commit 452eb2f
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 99 deletions.
7 changes: 7 additions & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/test
/coverage
/benchmark
/doc
/build
/scripts
/.github
2 changes: 0 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ linter:
analyzer:
exclude:
- build/**
- test/**
- benchmark/**
4 changes: 2 additions & 2 deletions benchmark/salsa20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.Salsa20(key, nonce).convert(input);
cipher.Salsa20(key, nonce: nonce).convert(input);
}
}

Expand Down Expand Up @@ -60,7 +60,7 @@ class CipherlibStreamBenchmark extends AsyncBenchmark {

@override
Future<void> run() async {
await cipher.Salsa20(key, nonce).stream(inputStream).drain();
await cipher.Salsa20(key, nonce: nonce).stream(inputStream).drain();
}
}

Expand Down
2 changes: 1 addition & 1 deletion benchmark/salsa20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.Salsa20Poly1305(key: key, iv: nonce).convert(input);
cipher.Salsa20Poly1305(key: key, nonce: nonce).convert(input);
}
}

Expand Down
94 changes: 43 additions & 51 deletions lib/src/algorithms/aead_cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'dart:typed_data';

import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:hashlib/hashlib.dart' show HashDigest, MACHashBase, MACSinkBase;
import 'package:hashlib/hashlib.dart'
show HashDigest, HashDigestSink, MACHashBase;

/// The result fromo AEAD ciphers
class AEADResult {
Expand Down Expand Up @@ -43,61 +44,52 @@ class AEADResultWithIV extends AEADResult {

/// Extends the base [AEADCipherSink] to generate message digest for cipher
/// algorithms.
class AEADCipherSink implements CipherSink, MACSinkBase {
class AEADCipherSink<C extends CipherSink, H extends HashDigestSink>
extends CipherSink {
final H _sink;
final C _cipher;
final List<int>? _aad;
int _dataLength = 0;
bool _verifyMode;

AEADCipherSink(
this._cipher,
this._hasher, [
this._sink, [
this._aad,
this._verifyMode = false,
]) {
_hasher.reset();
_cipher.reset();
if (_aad != null) {
_sink.add(_aad!);
// pad with zero
int n = _aad!.length;
if (n & 15 != 0) {
_sink.add(Uint8List(16 - (n & 15)));
}
}
}

bool _verifyMode;
int _dataLength = 0;
final List<int>? _aad;
final CipherSink _cipher;
final MACSinkBase _hasher;

@override
int get hashLength => _hasher.hashLength;

@override
int get derivedKeyLength => _hasher.derivedKeyLength;
/// The length of generated hash in bytes
int get macLength => _sink.hashLength;

@override
bool get closed => _hasher.closed || _cipher.closed;
bool get closed => _sink.closed || _cipher.closed;

@override
void reset([bool asVerifyMode = false]) {
_hasher.reset();
void reset([bool forVerification = false]) {
_sink.reset();
_cipher.reset();
_verifyMode = asVerifyMode;
_verifyMode = forVerification;
}

@override
Uint8List close() {
return add([], 0, null, true);
}

@override
/// Finalizes the message-digest and returns a [HashDigest].
///
/// Throws [StateError] if this sink is not closed before generating digest.
HashDigest digest() {
if (!closed) close();
return _hasher.digest();
}

@override
void init([List<int>? keypair]) {
if (_aad != null) {
_hasher.add(_aad!);
// pad with zero
int n = _aad!.length;
if (n & 15 != 0) {
_hasher.add(Uint8List(16 - (n & 15)));
}
if (!closed) {
close();
}
_dataLength = 0;
return _sink.digest();
}

@override
Expand All @@ -111,18 +103,18 @@ class AEADCipherSink implements CipherSink, MACSinkBase {
var cipher = _cipher.add(data, start, end, last);
if (_verifyMode) {
_dataLength += end - start;
_hasher.add(data, start, end);
_sink.add(data, start, end);
} else {
_dataLength += cipher.length;
_hasher.add(cipher);
_sink.add(cipher);
}
if (last) {
// pad with zero
if (_dataLength & 15 != 0) {
_hasher.add(Uint8List(16 - (_dataLength & 15)));
_sink.add(Uint8List(16 - (_dataLength & 15)));
}
int n = _aad?.length ?? 0;
_hasher.add([
_sink.add([
n,
n >>> 8,
n >>> 16,
Expand All @@ -146,24 +138,24 @@ class AEADCipherSink implements CipherSink, MACSinkBase {
}

/// Provides support for AEAD (Authenticated Encryption with Associated Data) to
/// the any [Cipher] with any [MACHashBase] algorithm.
/// the any [Cipher] with any MAC algorithm.
abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
extends StreamCipherBase {
/// The MAC generator used by this AEAD construction
final M mac;

/// The cipher used by this AEAD construction
final C cipher;

/// The MAC generator used by this AEAD construction
final M hasher;

/// Additional authenticated data (optional)
final List<int>? aad;

@override
String get name => '${cipher.name}/${hasher.name}';
String get name => '${cipher.name}/${mac.name}';

const AEADCipher(
this.cipher,
this.hasher, [
this.mac, [
this.aad,
]);

Expand All @@ -172,10 +164,10 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
]) =>
AEADCipherSink(
cipher.createSink(),
hasher.createSink(),
mac.createSink(),
aad,
verifyMode,
)..init();
);

/// Transforms the [message] with an authentication tag.
@pragma('vm:prefer-inline')
Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class ChaCha20 extends SaltedCipher {
/// The initial block id
final Uint8List counter;

const ChaCha20(
const ChaCha20._(
this.key,
Uint8List nonce,
this.counter,
Expand All @@ -253,7 +253,7 @@ class ChaCha20 extends SaltedCipher {
/// Creates a [ChaCha20] with List<int> [key], and [nonce].
///
/// Every elements of the both list is transformed to unsigned 8-bit numbers.
factory ChaCha20.fromList(
factory ChaCha20(
List<int> key, {
List<int>? nonce,
Nonce64? counter,
Expand All @@ -263,7 +263,7 @@ class ChaCha20 extends SaltedCipher {
var counter8 = counter.bytes;
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
var nonce8 = nonce is Uint8List ? nonce : Uint8List.fromList(nonce);
return ChaCha20(key8, nonce8, counter8);
return ChaCha20._(key8, nonce8, counter8);
}

@override
Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/salsa20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class Salsa20 extends SaltedCipher {
/// The initial block id
final Uint8List counter;

const Salsa20(
const Salsa20._(
this.key,
Uint8List nonce,
this.counter,
Expand All @@ -222,7 +222,7 @@ class Salsa20 extends SaltedCipher {
/// Creates a [Salsa20] with List<int> [key], and [nonce].
///
/// Every elements of the both list is transformed to unsigned 8-bit numbers.
factory Salsa20.fromList(
factory Salsa20(
List<int> key, {
List<int>? nonce,
Nonce64? counter,
Expand All @@ -232,7 +232,7 @@ class Salsa20 extends SaltedCipher {
var counter8 = counter.bytes;
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
var nonce8 = nonce is Uint8List ? nonce : Uint8List.fromList(nonce);
return Salsa20(key8, nonce8, counter8);
return Salsa20._(key8, nonce8, counter8);
}

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Uint8List chacha20(
List<int>? nonce,
Nonce64? counter,
}) =>
ChaCha20.fromList(
ChaCha20(
key,
nonce: nonce,
counter: counter,
Expand All @@ -46,7 +46,7 @@ Stream<int> chacha20Stream(
List<int>? nonce,
Nonce64? counter,
}) =>
ChaCha20.fromList(
ChaCha20(
key,
nonce: nonce,
counter: counter,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/chacha20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/aead_cipher.dart';
import 'package:cipherlib/src/algorithms/chacha20.dart';
import 'package:cipherlib/src/chacha20.dart';
import 'package:cipherlib/src/utils/nonce.dart';
import 'package:hashlib/hashlib.dart' show Poly1305;

Expand Down Expand Up @@ -37,7 +38,7 @@ class ChaCha20Poly1305 extends AEADCipher<ChaCha20, Poly1305> {
Nonce64? counter,
List<int>? aad,
}) =>
ChaCha20.fromList(
ChaCha20(
key,
nonce: nonce,
counter: counter,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ abstract class StreamCipherBase

/// Template for Cipher algorithm that uses the same logic for
/// both encryption and decryption.
abstract class Cipher extends StreamCipherBase {
abstract class Cipher<S extends CipherSink> extends StreamCipherBase {
const Cipher();

/// Creates a sink for the algorithm
CipherSink createSink();
S createSink();

/// Transforms the [message].
@pragma('vm:prefer-inline')
Expand Down
4 changes: 2 additions & 2 deletions lib/src/salsa20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Uint8List salsa20(
List<int>? nonce,
Nonce64? counter,
}) =>
Salsa20.fromList(
Salsa20(
key,
nonce: nonce,
counter: counter,
Expand All @@ -44,7 +44,7 @@ Stream<int> salsa20Stream(
List<int>? nonce,
Nonce64? counter,
}) =>
Salsa20.fromList(
Salsa20(
key,
nonce: nonce,
counter: counter,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/salsa20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Salsa20Poly1305 extends AEADCipher<Salsa20, Poly1305> {
Nonce64? counter,
List<int>? aad,
}) =>
Salsa20.fromList(
Salsa20(
key,
nonce: nonce,
counter: counter,
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: cipherlib
description: Implementations of cryptographic algorithms for encryption and decryption in Dart.
version: 0.0.12
version: 0.0.13
repository: https://github.com/bitanon/cipherlib

environment:
sdk: '>=2.14.0 <4.0.0'

dependencies:
hashlib: ^1.19.2
hashlib_codecs: ^2.5.0
hashlib: ^1.20.2
hashlib_codecs: ^2.6.0

dev_dependencies:
lints: any
Expand Down
Loading

0 comments on commit 452eb2f

Please sign in to comment.