Created
December 27, 2024 21:42
-
-
Save HamdaanAliQuatil/27eb0d804c70e5b2920dcc907daf1253 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:typed_data'; | |
import 'package:pem/pem.dart'; | |
import 'package:test/test.dart'; | |
import 'package:webcrypto/webcrypto.dart'; | |
void main() { | |
group('RsaOaepPrivateKey Tests', () { | |
final modulusLength = 2048; | |
final publicExponent = BigInt.from(65537); | |
final hash = Hash.sha256; | |
final plaintext = Uint8List.fromList(List<int>.generate(64, (i) => i)); | |
final label = utf8.encode('testLabel'); | |
test('Key Generation', () async { | |
final keyPair = await RsaOaepPrivateKey.generateKey( | |
modulusLength, publicExponent, hash); | |
expect(keyPair.privateKey, isNotNull); | |
expect(keyPair.publicKey, isNotNull); | |
}); | |
test('Decrypt (Bytes)', () async { | |
final keyPair = await RsaOaepPrivateKey.generateKey( | |
modulusLength, publicExponent, hash); | |
final privateKey = keyPair.privateKey; | |
final publicKey = keyPair.publicKey; | |
final ciphertext = await publicKey.encryptBytes(plaintext, | |
label: utf8.encode('testLabel')); | |
final decryptedText = await privateKey.decryptBytes(ciphertext, | |
label: utf8.encode('testLabel')); | |
expect(decryptedText, equals(plaintext)); // Ensure decryption is correct | |
}); | |
test('Key Export (PKCS8)', () async { | |
final keyPair = await RsaOaepPrivateKey.generateKey( | |
modulusLength, publicExponent, hash); | |
final privateKey = keyPair.privateKey; | |
final pkcs8Key = await privateKey.exportPkcs8Key(); | |
expect(pkcs8Key, isNotNull); | |
expect(pkcs8Key.length, greaterThan(0)); // Ensure key data is valid | |
}); | |
test('Key Export (JWK)', () async { | |
final keyPair = await RsaOaepPrivateKey.generateKey( | |
modulusLength, publicExponent, hash); | |
final privateKey = keyPair.privateKey; | |
final jwk = await privateKey.exportJsonWebKey(); | |
expect(jwk['kty'], equals('RSA')); | |
expect(jwk['n'], isNotNull); | |
expect(jwk['e'], isNotNull); | |
}); | |
}); | |
group('RsaOaepPublicKey Tests', () { | |
final hash = Hash.sha256; | |
test('importSpkiKey and exportSpkiKey match', () async { | |
// Key in SPKI format | |
List<int> publicKeyData = PemCodec(PemLabel.publicKey).decode(''' | |
-----BEGIN PUBLIC KEY----- | |
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5E93u+180pjiO+IewHl0 | |
ZTqQzGzAbNGvgI7xsplvKSrXmZ4RIN3ZdypJ5EKXdo59WdnujwFOFsFIfyP1Bofx | |
1Djskpqg1MIUWGiyekg7CGqojqGwcrOdx057scBCrZ/ixhbfdak9kX8d8A+rrYZR | |
mcBvf68TeMG4KGK2gjZPKBV1yLqY5AaCm77GKL+moVZyUqC3xuuVtmUK7Xgzsdsv | |
RFhqNdt0SwxszaIY5kJaegMESQaq88xs7qQbftWeTy/I+FIZ7N8WXnY+ha2UlGlW | |
lywBCtRxXkiYdGRi6J5BVnmNHyAbJk8hsGsP/9yd2BAgGFViLkf+yfqkS3v29gjs | |
jwIDAQAB | |
-----END PUBLIC KEY----- | |
'''); | |
final publicKey = await RsaOaepPublicKey.importSpkiKey( | |
publicKeyData, | |
hash, | |
); | |
final exportedKeyData = await publicKey.exportSpkiKey(); | |
print('Exported Public Key Data: ${base64.encode(exportedKeyData)}'); | |
// Validate roundtrip: imported and exported keys should match | |
expect( | |
base64.encode(exportedKeyData), equals(base64.encode(publicKeyData))); | |
}); | |
test('Encrypt and Decrypt using generated key', () async { | |
final modulusLength = 2048; | |
final publicExponent = BigInt.from(65537); | |
final keyPair = await RsaOaepPrivateKey.generateKey( | |
modulusLength, publicExponent, hash); | |
final publicKey = keyPair.publicKey; | |
final privateKey = keyPair.privateKey; | |
final plaintext = Uint8List.fromList(List<int>.generate(64, (i) => i)); | |
final ciphertext = await publicKey.encryptBytes(plaintext); | |
final decryptedText = await privateKey.decryptBytes(ciphertext); | |
expect(decryptedText, equals(plaintext)); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment