This commit is contained in:
Jonas Roussel
2023-08-21 16:00:13 +02:00
parent f979ff4855
commit fe696b8e25
5 changed files with 54 additions and 1164 deletions

View File

@ -1,3 +1,8 @@
## 2.9.0
- Adding `basic_utils` package to handle PEM & key parsing
- A lot of new class factory to create `Keys` (e.g. `RSAPublicKey.cert` and `.bytes`)
## 2.8.2 ## 2.8.2
- Downgraing `collection` to 1.7.1 to be compatible with flutter_test - Downgraing `collection` to 1.7.1 to be compatible with flutter_test

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,11 @@
import 'dart:typed_data';
import 'package:basic_utils/basic_utils.dart';
import 'package:dart_jsonwebtoken/src/utils.dart';
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
import 'package:pointycastle/pointycastle.dart' as pc; import 'package:pointycastle/pointycastle.dart' as pc;
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
import 'exceptions.dart'; import 'exceptions.dart';
import 'crypto_utils.dart';
abstract class JWTKey {} abstract class JWTKey {}
@ -27,6 +30,8 @@ class RSAPrivateKey extends JWTKey {
RSAPrivateKey.raw(pc.RSAPrivateKey _key) : key = _key; RSAPrivateKey.raw(pc.RSAPrivateKey _key) : key = _key;
RSAPrivateKey.clone(RSAPrivateKey _key) : key = _key.key; RSAPrivateKey.clone(RSAPrivateKey _key) : key = _key.key;
RSAPrivateKey.bytes(Uint8List bytes)
: key = CryptoUtils.rsaPrivateKeyFromDERBytes(bytes);
} }
/// For RSA algorithm, in verify method /// For RSA algorithm, in verify method
@ -43,6 +48,26 @@ class RSAPublicKey extends JWTKey {
RSAPublicKey.raw(pc.RSAPublicKey _key) : key = _key; RSAPublicKey.raw(pc.RSAPublicKey _key) : key = _key;
RSAPublicKey.clone(RSAPublicKey _key) : key = _key.key; RSAPublicKey.clone(RSAPublicKey _key) : key = _key.key;
RSAPublicKey.bytes(Uint8List bytes) {
try {
key = CryptoUtils.rsaPublicKeyFromDERBytesPkcs1(bytes);
} catch (_) {
key = CryptoUtils.rsaPublicKeyFromDERBytes(bytes);
}
}
RSAPublicKey.cert(String pem) {
final x509 = X509Utils.x509CertificateFromPem(pem);
final bytes = x509.tbsCertificate?.subjectPublicKeyInfo.bytes;
if (bytes == null) {
throw JWTParseException('x509 Certificate parsing failed');
}
try {
key = CryptoUtils.rsaPublicKeyFromDERBytesPkcs1(hexToUint8List(bytes));
} catch (_) {
key = CryptoUtils.rsaPublicKeyFromDERBytes(hexToUint8List(bytes));
}
}
} }
/// For ECDSA algorithm, in sign method /// For ECDSA algorithm, in sign method
@ -75,6 +100,8 @@ class ECPrivateKey extends JWTKey {
ECPrivateKey.clone(ECPrivateKey _key) ECPrivateKey.clone(ECPrivateKey _key)
: key = _key.key, : key = _key.key,
size = _key.size; size = _key.size;
ECPrivateKey.bytes(Uint8List bytes)
: key = CryptoUtils.ecPrivateKeyFromDerBytes(bytes);
} }
/// For ECDSA algorithm, in verify method /// For ECDSA algorithm, in verify method
@ -87,6 +114,8 @@ class ECPublicKey extends JWTKey {
ECPublicKey.raw(pc.ECPublicKey _key) : key = _key; ECPublicKey.raw(pc.ECPublicKey _key) : key = _key;
ECPublicKey.clone(ECPublicKey _key) : key = _key.key; ECPublicKey.clone(ECPublicKey _key) : key = _key.key;
ECPublicKey.bytes(Uint8List bytes)
: key = CryptoUtils.ecPublicKeyFromDerBytes(bytes);
} }
/// For EdDSA algorithm, in sign method /// For EdDSA algorithm, in sign method

View File

@ -89,3 +89,19 @@ BigInt bigIntFromBytes(Uint8List bytes) {
return bytes.fold(BigInt.zero, (a, b) => a * _b256 + BigInt.from(b)); return bytes.fold(BigInt.zero, (a, b) => a * _b256 + BigInt.from(b));
} }
Uint8List hexToUint8List(String hex) {
if (hex.length % 2 != 0) {
throw 'Odd number of hex digits';
}
var l = hex.length ~/ 2;
var result = Uint8List(l);
for (var i = 0; i < l; ++i) {
var x = int.parse(hex.substring(2 * i, 2 * (i + 1)), radix: 16);
if (x.isNaN) {
throw 'Expected hex string';
}
result[i] = x;
}
return result;
}

View File

@ -1,6 +1,6 @@
name: dart_jsonwebtoken name: dart_jsonwebtoken
description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT). description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT).
version: 2.8.2 version: 2.9.0
repository: https://github.com/jonasroussel/dart_jsonwebtoken repository: https://github.com/jonasroussel/dart_jsonwebtoken
homepage: https://github.com/jonasroussel/dart_jsonwebtoken#readme homepage: https://github.com/jonasroussel/dart_jsonwebtoken#readme
@ -19,6 +19,7 @@ dependencies:
convert: ^3.1.1 convert: ^3.1.1
collection: ^1.17.1 collection: ^1.17.1
ed25519_edwards: ^0.3.1 ed25519_edwards: ^0.3.1
basic_utils: ^5.6.1
dev_dependencies: dev_dependencies:
lints: ^2.1.1 lints: ^2.1.1