Skip to content

Commit 5ac703a

Browse files
committed
feat: Reduce dependencies
* Use `pointycastle` for HMAC calculation instead of `crypto` * Implement custom deep list equality instead of using the `collection` package
1 parent 368d05a commit 5ac703a

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

lib/src/algorithms.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:math';
22
import 'dart:typed_data';
33

4-
import 'package:crypto/crypto.dart';
54
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
65
import 'package:pointycastle/pointycastle.dart' as pc;
76

@@ -148,12 +147,10 @@ class HMACAlgorithm extends JWTAlgorithm {
148147

149148
final keyBytes = decodeHMACSecret(secretKey.key, secretKey.isBase64Encoded);
150149

151-
final hmac = Hmac(
152-
_getHash(name),
153-
keyBytes,
154-
);
150+
final hmac = pc.Mac('${_getHash(name)}/HMAC');
151+
hmac.init(pc.KeyParameter(keyBytes));
155152

156-
return Uint8List.fromList(hmac.convert(body).bytes);
153+
return Uint8List.fromList(hmac.process(body));
157154
}
158155

159156
@override
@@ -171,14 +168,14 @@ class HMACAlgorithm extends JWTAlgorithm {
171168
return true;
172169
}
173170

174-
Hash _getHash(String name) {
171+
String _getHash(String name) {
175172
switch (name) {
176173
case 'HS256':
177-
return sha256;
174+
return 'SHA-256';
178175
case 'HS384':
179-
return sha384;
176+
return 'SHA-384';
180177
case 'HS512':
181-
return sha512;
178+
return 'SHA-512';
182179
default:
183180
throw ArgumentError.value(name, 'name', 'unknown hash name');
184181
}

lib/src/helpers.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,13 @@ ECDSAAlgorithm? ecCurveToAlgorithm(String curveName) {
159159
return null;
160160
}
161161
}
162+
163+
bool isListEquals<T>(List<T>? a, List<T>? b) {
164+
if (identical(a, b)) return true;
165+
if (a == null || b == null) return false;
166+
if (a.length != b.length) return false;
167+
for (var i = 0; i < a.length; i++) {
168+
if (a[i] != b[i]) return false;
169+
}
170+
return true;
171+
}

lib/src/jwt.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import 'dart:collection';
22
import 'dart:convert';
33
import 'dart:typed_data';
44

5-
import 'package:collection/collection.dart';
6-
75
import 'algorithms.dart';
86
import 'exceptions.dart';
97
import 'helpers.dart';
@@ -112,7 +110,7 @@ class JWT {
112110
if (payload['aud'] is String && payload['aud'] != audience.first) {
113111
throw JWTInvalidException('invalid audience');
114112
} else if (payload['aud'] is List &&
115-
!ListEquality().equals(payload['aud'], audience)) {
113+
!isListEquals(payload['aud'], audience)) {
116114
throw JWTInvalidException('invalid audience');
117115
}
118116
} else {

pubspec.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ false_secrets:
1313
- /README.md
1414

1515
dependencies:
16-
crypto: ^3.0.6
1716
pointycastle: ^4.0.0
1817
convert: ^3.1.2
19-
collection: ^1.17.1
2018
ed25519_edwards: ^0.3.1
2119
clock: ^1.1.2
2220

0 commit comments

Comments
 (0)