Skip to content

Commit 6ca9d27

Browse files
committed
Bump version for hashlib and hashlib_codecs
1 parent d387ac4 commit 6ca9d27

19 files changed

+99
-100
lines changed

.pubignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/test
2+
/coverage
3+
/benchmark
4+
/doc
5+
/build
6+
/scripts
7+
/.github

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 0.0.12
1+
## 0.0.13
22

33
- `XOR` cipher.
44
- `Salsa20` cipher with `Poly1305` tag.

analysis_options.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ linter:
2222
analyzer:
2323
exclude:
2424
- build/**
25-
- test/**
26-
- benchmark/**

benchmark/salsa20.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CipherlibBenchmark extends Benchmark {
2323

2424
@override
2525
void run() {
26-
cipher.Salsa20(key, nonce).convert(input);
26+
cipher.Salsa20(key, nonce: nonce).convert(input);
2727
}
2828
}
2929

@@ -60,7 +60,7 @@ class CipherlibStreamBenchmark extends AsyncBenchmark {
6060

6161
@override
6262
Future<void> run() async {
63-
await cipher.Salsa20(key, nonce).stream(inputStream).drain();
63+
await cipher.Salsa20(key, nonce: nonce).stream(inputStream).drain();
6464
}
6565
}
6666

benchmark/salsa20_poly1305.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CipherlibBenchmark extends Benchmark {
2121

2222
@override
2323
void run() {
24-
cipher.Salsa20Poly1305(key: key, iv: nonce).convert(input);
24+
cipher.Salsa20Poly1305(key: key, nonce: nonce).convert(input);
2525
}
2626
}
2727

lib/src/algorithms/aead_cipher.dart

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'dart:typed_data';
66

77
import 'package:cipherlib/src/core/cipher.dart';
88
import 'package:cipherlib/src/core/cipher_sink.dart';
9-
import 'package:hashlib/hashlib.dart' show HashDigest, MACHashBase, MACSinkBase;
9+
import 'package:hashlib/hashlib.dart'
10+
show HashDigest, HashDigestSink, MACHashBase;
1011

1112
/// The result fromo AEAD ciphers
1213
class AEADResult {
@@ -43,61 +44,52 @@ class AEADResultWithIV extends AEADResult {
4344

4445
/// Extends the base [AEADCipherSink] to generate message digest for cipher
4546
/// algorithms.
46-
class AEADCipherSink implements CipherSink, MACSinkBase {
47+
class AEADCipherSink<C extends CipherSink, H extends HashDigestSink>
48+
extends CipherSink {
49+
final H _sink;
50+
final C _cipher;
51+
final List<int>? _aad;
52+
int _dataLength = 0;
53+
bool _verifyMode;
54+
4755
AEADCipherSink(
4856
this._cipher,
49-
this._hasher, [
57+
this._sink, [
5058
this._aad,
5159
this._verifyMode = false,
5260
]) {
53-
_hasher.reset();
5461
_cipher.reset();
62+
if (_aad != null) {
63+
_sink.add(_aad!);
64+
// pad with zero
65+
int n = _aad!.length;
66+
if (n & 15 != 0) {
67+
_sink.add(Uint8List(16 - (n & 15)));
68+
}
69+
}
5570
}
5671

57-
bool _verifyMode;
58-
int _dataLength = 0;
59-
final List<int>? _aad;
60-
final CipherSink _cipher;
61-
final MACSinkBase _hasher;
62-
63-
@override
64-
int get hashLength => _hasher.hashLength;
65-
66-
@override
67-
int get derivedKeyLength => _hasher.derivedKeyLength;
72+
/// The length of generated hash in bytes
73+
int get macLength => _sink.hashLength;
6874

6975
@override
70-
bool get closed => _hasher.closed || _cipher.closed;
76+
bool get closed => _sink.closed || _cipher.closed;
7177

7278
@override
73-
void reset([bool asVerifyMode = false]) {
74-
_hasher.reset();
79+
void reset([bool forVerification = false]) {
80+
_sink.reset();
7581
_cipher.reset();
76-
_verifyMode = asVerifyMode;
82+
_verifyMode = forVerification;
7783
}
7884

79-
@override
80-
Uint8List close() {
81-
return add([], 0, null, true);
82-
}
83-
84-
@override
85+
/// Finalizes the message-digest and returns a [HashDigest].
86+
///
87+
/// Throws [StateError] if this sink is not closed before generating digest.
8588
HashDigest digest() {
86-
if (!closed) close();
87-
return _hasher.digest();
88-
}
89-
90-
@override
91-
void init([List<int>? keypair]) {
92-
if (_aad != null) {
93-
_hasher.add(_aad!);
94-
// pad with zero
95-
int n = _aad!.length;
96-
if (n & 15 != 0) {
97-
_hasher.add(Uint8List(16 - (n & 15)));
98-
}
89+
if (!closed) {
90+
close();
9991
}
100-
_dataLength = 0;
92+
return _sink.digest();
10193
}
10294

10395
@override
@@ -111,18 +103,18 @@ class AEADCipherSink implements CipherSink, MACSinkBase {
111103
var cipher = _cipher.add(data, start, end, last);
112104
if (_verifyMode) {
113105
_dataLength += end - start;
114-
_hasher.add(data, start, end);
106+
_sink.add(data, start, end);
115107
} else {
116108
_dataLength += cipher.length;
117-
_hasher.add(cipher);
109+
_sink.add(cipher);
118110
}
119111
if (last) {
120112
// pad with zero
121113
if (_dataLength & 15 != 0) {
122-
_hasher.add(Uint8List(16 - (_dataLength & 15)));
114+
_sink.add(Uint8List(16 - (_dataLength & 15)));
123115
}
124116
int n = _aad?.length ?? 0;
125-
_hasher.add([
117+
_sink.add([
126118
n,
127119
n >>> 8,
128120
n >>> 16,
@@ -146,24 +138,24 @@ class AEADCipherSink implements CipherSink, MACSinkBase {
146138
}
147139

148140
/// Provides support for AEAD (Authenticated Encryption with Associated Data) to
149-
/// the any [Cipher] with any [MACHashBase] algorithm.
141+
/// the any [Cipher] with any MAC algorithm.
150142
abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
151143
extends StreamCipherBase {
144+
/// The MAC generator used by this AEAD construction
145+
final M mac;
146+
152147
/// The cipher used by this AEAD construction
153148
final C cipher;
154149

155-
/// The MAC generator used by this AEAD construction
156-
final M hasher;
157-
158150
/// Additional authenticated data (optional)
159151
final List<int>? aad;
160152

161153
@override
162-
String get name => '${cipher.name}/${hasher.name}';
154+
String get name => '${cipher.name}/${mac.name}';
163155

164156
const AEADCipher(
165157
this.cipher,
166-
this.hasher, [
158+
this.mac, [
167159
this.aad,
168160
]);
169161

@@ -172,10 +164,10 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
172164
]) =>
173165
AEADCipherSink(
174166
cipher.createSink(),
175-
hasher.createSink(),
167+
mac.createSink(),
176168
aad,
177169
verifyMode,
178-
)..init();
170+
);
179171

180172
/// Transforms the [message] with an authentication tag.
181173
@pragma('vm:prefer-inline')

lib/src/algorithms/chacha20.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class ChaCha20 extends SaltedCipher {
244244
/// The initial block id
245245
final Uint8List counter;
246246

247-
const ChaCha20(
247+
const ChaCha20._(
248248
this.key,
249249
Uint8List nonce,
250250
this.counter,
@@ -253,7 +253,7 @@ class ChaCha20 extends SaltedCipher {
253253
/// Creates a [ChaCha20] with List<int> [key], and [nonce].
254254
///
255255
/// Every elements of the both list is transformed to unsigned 8-bit numbers.
256-
factory ChaCha20.fromList(
256+
factory ChaCha20(
257257
List<int> key, {
258258
List<int>? nonce,
259259
Nonce64? counter,
@@ -263,7 +263,7 @@ class ChaCha20 extends SaltedCipher {
263263
var counter8 = counter.bytes;
264264
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
265265
var nonce8 = nonce is Uint8List ? nonce : Uint8List.fromList(nonce);
266-
return ChaCha20(key8, nonce8, counter8);
266+
return ChaCha20._(key8, nonce8, counter8);
267267
}
268268

269269
@override

lib/src/algorithms/salsa20.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class Salsa20 extends SaltedCipher {
213213
/// The initial block id
214214
final Uint8List counter;
215215

216-
const Salsa20(
216+
const Salsa20._(
217217
this.key,
218218
Uint8List nonce,
219219
this.counter,
@@ -222,7 +222,7 @@ class Salsa20 extends SaltedCipher {
222222
/// Creates a [Salsa20] with List<int> [key], and [nonce].
223223
///
224224
/// Every elements of the both list is transformed to unsigned 8-bit numbers.
225-
factory Salsa20.fromList(
225+
factory Salsa20(
226226
List<int> key, {
227227
List<int>? nonce,
228228
Nonce64? counter,
@@ -232,7 +232,7 @@ class Salsa20 extends SaltedCipher {
232232
var counter8 = counter.bytes;
233233
var key8 = key is Uint8List ? key : Uint8List.fromList(key);
234234
var nonce8 = nonce is Uint8List ? nonce : Uint8List.fromList(nonce);
235-
return Salsa20(key8, nonce8, counter8);
235+
return Salsa20._(key8, nonce8, counter8);
236236
}
237237

238238
@override

lib/src/chacha20.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Uint8List chacha20(
2424
List<int>? nonce,
2525
Nonce64? counter,
2626
}) =>
27-
ChaCha20.fromList(
27+
ChaCha20(
2828
key,
2929
nonce: nonce,
3030
counter: counter,
@@ -46,7 +46,7 @@ Stream<int> chacha20Stream(
4646
List<int>? nonce,
4747
Nonce64? counter,
4848
}) =>
49-
ChaCha20.fromList(
49+
ChaCha20(
5050
key,
5151
nonce: nonce,
5252
counter: counter,

lib/src/chacha20_poly1305.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:typed_data';
55

66
import 'package:cipherlib/src/algorithms/aead_cipher.dart';
77
import 'package:cipherlib/src/algorithms/chacha20.dart';
8+
import 'package:cipherlib/src/chacha20.dart';
89
import 'package:cipherlib/src/utils/nonce.dart';
910
import 'package:hashlib/hashlib.dart' show Poly1305;
1011

@@ -37,7 +38,7 @@ class ChaCha20Poly1305 extends AEADCipher<ChaCha20, Poly1305> {
3738
Nonce64? counter,
3839
List<int>? aad,
3940
}) =>
40-
ChaCha20.fromList(
41+
ChaCha20(
4142
key,
4243
nonce: nonce,
4344
counter: counter,

lib/src/core/cipher.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ abstract class StreamCipherBase
3434

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

4040
/// Creates a sink for the algorithm
41-
CipherSink createSink();
41+
S createSink();
4242

4343
/// Transforms the [message].
4444
@pragma('vm:prefer-inline')

lib/src/salsa20.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Uint8List salsa20(
2323
List<int>? nonce,
2424
Nonce64? counter,
2525
}) =>
26-
Salsa20.fromList(
26+
Salsa20(
2727
key,
2828
nonce: nonce,
2929
counter: counter,
@@ -44,7 +44,7 @@ Stream<int> salsa20Stream(
4444
List<int>? nonce,
4545
Nonce64? counter,
4646
}) =>
47-
Salsa20.fromList(
47+
Salsa20(
4848
key,
4949
nonce: nonce,
5050
counter: counter,

lib/src/salsa20_poly1305.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Salsa20Poly1305 extends AEADCipher<Salsa20, Poly1305> {
3131
Nonce64? counter,
3232
List<int>? aad,
3333
}) =>
34-
Salsa20.fromList(
34+
Salsa20(
3535
key,
3636
nonce: nonce,
3737
counter: counter,

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name: cipherlib
22
description: Implementations of cryptographic algorithms for encryption and decryption in Dart.
3-
version: 0.0.12
3+
version: 0.0.13
44
repository: https://github.com/bitanon/cipherlib
55

66
environment:
77
sdk: '>=2.14.0 <4.0.0'
88

99
dependencies:
10-
hashlib: ^1.19.2
11-
hashlib_codecs: ^2.5.0
10+
hashlib: ^1.20.2
11+
hashlib_codecs: ^2.6.0
1212

1313
dev_dependencies:
1414
lints: any

0 commit comments

Comments
 (0)