From 2fa20778fd8a30fde881f41f98f9275a8fafbb9c Mon Sep 17 00:00:00 2001 From: YATIN072007 Date: Sun, 18 Jan 2026 14:43:32 +0530 Subject: [PATCH 1/4] Use canonical link for all RFC references (#189) --- lib/src/testing/webcrypto/pbkdf2.dart | 2 +- lib/src/testing/webcrypto/rsapss.dart | 2 +- lib/src/webcrypto/webcrypto.aescbc.dart | 12 +++---- lib/src/webcrypto/webcrypto.aesctr.dart | 6 ++-- lib/src/webcrypto/webcrypto.aesgcm.dart | 6 ++-- lib/src/webcrypto/webcrypto.ecdh.dart | 16 ++++----- lib/src/webcrypto/webcrypto.hkdf.dart | 2 +- lib/src/webcrypto/webcrypto.hmac.dart | 4 +-- lib/src/webcrypto/webcrypto.pbkdf2.dart | 4 +-- lib/src/webcrypto/webcrypto.rsaoaep.dart | 28 +++++++-------- lib/src/webcrypto/webcrypto.rsapss.dart | 34 +++++++++---------- .../webcrypto/webcrypto.rsassapkcs1v15.dart | 28 +++++++-------- src/CMakeLists.txt | 5 ++- 13 files changed, 76 insertions(+), 73 deletions(-) diff --git a/lib/src/testing/webcrypto/pbkdf2.dart b/lib/src/testing/webcrypto/pbkdf2.dart index 7e5ca254..cf971840 100644 --- a/lib/src/testing/webcrypto/pbkdf2.dart +++ b/lib/src/testing/webcrypto/pbkdf2.dart @@ -75,7 +75,7 @@ void main() async { await runner.tests().runTests(); } -// TODO: Augments tests with test vectors from: https://datatracker.ietf.org/doc/html/rfc6070 +// TODO: Augments tests with test vectors from: https://www.rfc-editor.org/rfc/rfc6070 // Allow single quotes for hardcoded testData written as JSON: // ignore_for_file: prefer_single_quotes diff --git a/lib/src/testing/webcrypto/rsapss.dart b/lib/src/testing/webcrypto/rsapss.dart index fe926caa..b54d6b2d 100644 --- a/lib/src/testing/webcrypto/rsapss.dart +++ b/lib/src/testing/webcrypto/rsapss.dart @@ -130,7 +130,7 @@ final _testData = [ /// [3]: https://opensource.apple.com/source/xnu/xnu-4570.41.2/EXTERNAL_HEADERS/corecrypto/ccrsa.h.auto.html /// [4]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf /// [5]: https://www.rfc-editor.org/rfc/rfc3447 - /// [6]: https://tools.ietf.org/html/rfc3447#section-9.1 + /// [6]: https://www.rfc-editor.org/rfc/rfc3447#section-9.1 /// [7]: https://bugs.webkit.org/show_bug.cgi?id=216750 ...(nullOnSafari(_testDataWithLongSaltLength) ?? []), ]; diff --git a/lib/src/webcrypto/webcrypto.aescbc.dart b/lib/src/webcrypto/webcrypto.aescbc.dart index 39e5c838..6f5df25c 100644 --- a/lib/src/webcrypto/webcrypto.aescbc.dart +++ b/lib/src/webcrypto/webcrypto.aescbc.dart @@ -32,8 +32,8 @@ part of 'webcrypto.dart'; /// {@macro AesCbcSecretKey-encryptBytes/decryptBytes:example} /// /// [1]: https://csrc.nist.gov/publications/detail/sp/800-38a/final -/// [2]: https://tools.ietf.org/html/rfc2315#section-10.3 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [2]: https://www.rfc-editor.org/rfc/rfc2315#section-10.3 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class AesCbcSecretKey { final AesCbcSecretKeyImpl _impl; @@ -106,7 +106,7 @@ final class AesCbcSecretKey { /// print(jsonEncode(keyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 // TODO: Decide if we want restrictions on "use" property" (we probably have it on web, if we don't strip it) // TODO: Decide if we want place restrictions on key_ops static Future importJsonWebKey( @@ -151,7 +151,7 @@ final class AesCbcSecretKey { /// /// {@template AesCbcSecretKey-encrypt:padding} /// Encrypted output is always padded in PKCS#7 mode, as described in - /// [RFC 2315 Section 10.3 step 2](https://tools.ietf.org/html/rfc2315#section-10.3). + /// [RFC 2315 Section 10.3 step 2](https://www.rfc-editor.org/rfc/rfc2315#section-10.3). /// This padding is stripped when the message is decrypted. /// {@endtemplate} /// @@ -238,7 +238,7 @@ final class AesCbcSecretKey { /// {@template AesCbcSecretKey-decrypt:padding} /// The encrypted [data] is always assumed to be padded in PKCS#7 mode, /// as described in - /// [RFC 2315 Section 10.3 step 2](https://tools.ietf.org/html/rfc2315#section-10.3). + /// [RFC 2315 Section 10.3 step 2](https://www.rfc-editor.org/rfc/rfc2315#section-10.3). /// This padding is stripped from the decrypted return value. /// The [encryptBytes] and [encryptStream] methods always apply this padding. /// {@endtemplate} @@ -305,6 +305,6 @@ final class AesCbcSecretKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.aesctr.dart b/lib/src/webcrypto/webcrypto.aesctr.dart index e42b3fda..861e3755 100644 --- a/lib/src/webcrypto/webcrypto.aesctr.dart +++ b/lib/src/webcrypto/webcrypto.aesctr.dart @@ -27,7 +27,7 @@ part of 'webcrypto.dart'; /// {@macro AesCtrSecretKey-encryptBytes/decryptBytes:example} /// /// [1]: https://csrc.nist.gov/publications/detail/sp/800-38a/final -/// [2]: https://tools.ietf.org/html/rfc7517 +/// [2]: https://www.rfc-editor.org/rfc/rfc7517 final class AesCtrSecretKey { final AesCtrSecretKeyImpl _impl; @@ -103,7 +103,7 @@ final class AesCtrSecretKey { /// print(jsonEncode(keyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, ) async { @@ -326,6 +326,6 @@ final class AesCtrSecretKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.aesgcm.dart b/lib/src/webcrypto/webcrypto.aesgcm.dart index 5c1ccd1e..a8821eaa 100644 --- a/lib/src/webcrypto/webcrypto.aesgcm.dart +++ b/lib/src/webcrypto/webcrypto.aesgcm.dart @@ -30,7 +30,7 @@ part of 'webcrypto.dart'; /// {@macro AesGcmSecretKey-encryptBytes/decryptBytes:example} /// /// [1]: https://csrc.nist.gov/pubs/sp/800/38/d/final -/// [2]: https://tools.ietf.org/html/rfc7517 +/// [2]: https://www.rfc-editor.org/rfc/rfc7517 /// [3]: https://en.wikipedia.org/wiki/Authenticated_encryption final class AesGcmSecretKey { final AesGcmSecretKeyImpl _impl; @@ -103,7 +103,7 @@ final class AesGcmSecretKey { /// print(jsonEncode(keyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, ) async { @@ -289,6 +289,6 @@ final class AesGcmSecretKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.ecdh.dart b/lib/src/webcrypto/webcrypto.ecdh.dart index 6a7770c2..064dbb17 100644 --- a/lib/src/webcrypto/webcrypto.ecdh.dart +++ b/lib/src/webcrypto/webcrypto.ecdh.dart @@ -92,7 +92,7 @@ final class EcdhPrivateKey { /// } /// ``` /// - /// [1]: https://datatracker.ietf.org/doc/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 static Future importPkcs8Key( List keyData, EllipticCurve curve, @@ -155,7 +155,7 @@ final class EcdhPrivateKey { /// } /// ``` /// - /// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 + /// [1]: https://www.rfc-editor.org/rfc/rfc7518#section-6.2 static Future importJsonWebKey( Map jwk, EllipticCurve curve, @@ -215,9 +215,9 @@ final class EcdhPrivateKey { // null for length (in this primitive). However, you can always know the right // length from the curve. Note p512 can provide up to: 528 bits!!! // - // See: https://tools.ietf.org/html/rfc6090#section-4 + // See: https://www.rfc-editor.org/rfc/rfc6090#section-4 // Notice that this is not uniformly distributed, see also: - // https://tools.ietf.org/html/rfc6090#appendix-B + // https://www.rfc-editor.org/rfc/rfc6090#appendix-B Future deriveBits(int length, EcdhPublicKey publicKey) async { final publicKeyImpl = publicKey._impl; @@ -246,7 +246,7 @@ final class EcdhPrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(exportedPkcs8Key)); /// } /// ``` - /// [1]: https://datatracker.ietf.org/doc/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export the [EcdhPrivateKey] as a [JSON Web Key][1]. @@ -270,7 +270,7 @@ final class EcdhPrivateKey { /// print(jsonEncode(exportedPrivateKey)); /// } /// ``` - /// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 + /// [1]: https://www.rfc-editor.org/rfc/rfc7518#section-6.2 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } @@ -364,7 +364,7 @@ final class EcdhPublicKey { /// } /// ``` /// - /// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 + /// [1]: https://www.rfc-editor.org/rfc/rfc7518#section-6.2 static Future importJsonWebKey( Map jwk, EllipticCurve curve, @@ -409,6 +409,6 @@ final class EcdhPublicKey { /// print(jsonEncode(exportedPublicKey)); /// } /// ``` - /// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 + /// [1]: https://www.rfc-editor.org/rfc/rfc7518#section-6.2 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.hkdf.dart b/lib/src/webcrypto/webcrypto.hkdf.dart index 8dd23049..288a0443 100644 --- a/lib/src/webcrypto/webcrypto.hkdf.dart +++ b/lib/src/webcrypto/webcrypto.hkdf.dart @@ -47,7 +47,7 @@ part of 'webcrypto.dart'; /// ``` /// {@endtemplate} /// -/// [1]: https://tools.ietf.org/html/rfc5869 +/// [1]: https://www.rfc-editor.org/rfc/rfc5869 // TODO: It might be wise to use a random salt, then suggest that the non-secret // salt is stored or exchanged... final class HkdfSecretKey { diff --git a/lib/src/webcrypto/webcrypto.hmac.dart b/lib/src/webcrypto/webcrypto.hmac.dart index a7e38cb5..63f4dd9e 100644 --- a/lib/src/webcrypto/webcrypto.hmac.dart +++ b/lib/src/webcrypto/webcrypto.hmac.dart @@ -143,7 +143,7 @@ final class HmacSecretKey { /// print(jsonEncode(keyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( // TODO: Determine if the "alg" property can be omitted, and update documentation accordingly // also make tests covering cases where "alg" is omitted. @@ -383,6 +383,6 @@ final class HmacSecretKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.pbkdf2.dart b/lib/src/webcrypto/webcrypto.pbkdf2.dart index f639ffef..c82544b2 100644 --- a/lib/src/webcrypto/webcrypto.pbkdf2.dart +++ b/lib/src/webcrypto/webcrypto.pbkdf2.dart @@ -48,7 +48,7 @@ part of 'webcrypto.dart'; /// ``` /// {@endtemplate} /// -/// [1]: https://tools.ietf.org/html/rfc8018 +/// [1]: https://www.rfc-editor.org/rfc/rfc8018 // TODO: Rewrite all RFC links to use https://www.rfc-editor.org/rfc/rfcXXXX final class Pbkdf2SecretKey { @@ -87,7 +87,7 @@ final class Pbkdf2SecretKey { /// /// {@macro Pbkdf2SecretKey:example} /// - /// [1]: https://tools.ietf.org/html/rfc8018 + /// [1]: https://www.rfc-editor.org/rfc/rfc8018 Future deriveBits( int length, Hash hash, diff --git a/lib/src/webcrypto/webcrypto.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index 94de34f6..155059f3 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -69,9 +69,9 @@ part of 'webcrypto.dart'; /// encrypt/decrypt larger messages. /// {@endtemplate} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5208 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5208 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsaOaepPrivateKey { final RsaOaepPrivateKeyImpl _impl; @@ -113,7 +113,7 @@ final class RsaOaepPrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 static Future importPkcs8Key( List keyData, Hash hash, @@ -164,7 +164,7 @@ final class RsaOaepPrivateKey { /// /// {@macro RSA-importJsonWebKey:use-key_ops} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -307,7 +307,7 @@ final class RsaOaepPrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export RSAES-OAEP private key in [JSON Web Key][1] format. @@ -335,7 +335,7 @@ final class RsaOaepPrivateKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } @@ -355,9 +355,9 @@ final class RsaOaepPrivateKey { /// /// {@macro RSAES-OAEP-message-size-limit} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5280 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5280 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsaOaepPublicKey { final RsaOaepPublicKeyImpl _impl; @@ -399,7 +399,7 @@ final class RsaOaepPublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 static Future importSpkiKey( List keyData, Hash hash, @@ -444,7 +444,7 @@ final class RsaOaepPublicKey { /// /// {@macro RSA-importJsonWebKey:use-key_ops} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -551,7 +551,7 @@ final class RsaOaepPublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 Future exportSpkiKey() => _impl.exportSpkiKey(); /// Export RSAES-OAEP public key in [JSON Web Key][1] format. @@ -579,6 +579,6 @@ final class RsaOaepPublicKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/lib/src/webcrypto/webcrypto.rsapss.dart b/lib/src/webcrypto/webcrypto.rsapss.dart index 33aa2c54..5fa3bc9c 100644 --- a/lib/src/webcrypto/webcrypto.rsapss.dart +++ b/lib/src/webcrypto/webcrypto.rsapss.dart @@ -61,9 +61,9 @@ part of 'webcrypto.dart'; /// ``` /// {@endtemplate} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5208 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5208 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsaPssPrivateKey { final RsaPssPrivateKeyImpl _impl; @@ -105,7 +105,7 @@ final class RsaPssPrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 static Future importPkcs8Key( List keyData, Hash hash, @@ -156,7 +156,7 @@ final class RsaPssPrivateKey { /// /// {@macro RSA-importJsonWebKey:use-key_ops} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -280,7 +280,7 @@ final class RsaPssPrivateKey { /// print('signature: ${base64.encode(signature)}'); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc3447#section-9.1 + /// [1]: https://www.rfc-editor.org/rfc/rfc3447#section-9.1 /// [2]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf /// // Notes on saltLength for maintainers: @@ -354,7 +354,7 @@ final class RsaPssPrivateKey { /// print('signature: ${base64.encode(signature)}'); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc3447#section-9.1 + /// [1]: https://www.rfc-editor.org/rfc/rfc3447#section-9.1 /// [2]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf Future signStream(Stream> data, int saltLength) => _impl.signStream(data, saltLength); @@ -385,7 +385,7 @@ final class RsaPssPrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export RSASSA-PSS private key in [JSON Web Key][1] format. @@ -413,7 +413,7 @@ final class RsaPssPrivateKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } @@ -431,9 +431,9 @@ final class RsaPssPrivateKey { /// /// {@macro RSASSA-PSS-Example:generate-sign-verify} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5280 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5280 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsaPssPublicKey { final RsaPssPublicKeyImpl _impl; @@ -475,7 +475,7 @@ final class RsaPssPublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 static Future importSpkiKey( List keyData, Hash hash, @@ -520,7 +520,7 @@ final class RsaPssPublicKey { /// /// {@macro RSA-importJsonWebKey:use-key_ops} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -651,7 +651,7 @@ final class RsaPssPublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 Future exportSpkiKey() => _impl.exportSpkiKey(); /// Export RSASSA-PSS public key in [JSON Web Key][1] format. @@ -679,6 +679,6 @@ final class RsaPssPublicKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); -} +} \ No newline at end of file diff --git a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart index dc695f36..6002b05d 100644 --- a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart +++ b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart @@ -55,9 +55,9 @@ part of 'webcrypto.dart'; /// ``` /// {@endtemplate} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5208 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5208 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsassaPkcs1V15PrivateKey { final RsaSsaPkcs1V15PrivateKeyImpl _impl; @@ -101,7 +101,7 @@ final class RsassaPkcs1V15PrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 static Future importPkcs8Key( List keyData, Hash hash, @@ -164,7 +164,7 @@ final class RsassaPkcs1V15PrivateKey { /// before the JWK is passed to the browser. /// {@endtemplate} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -354,7 +354,7 @@ final class RsassaPkcs1V15PrivateKey { /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5208 + /// [1]: https://www.rfc-editor.org/rfc/rfc5208 Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export RSASSA-PKCS1-v1_5 private key in [JSON Web Key][1] format. @@ -385,7 +385,7 @@ final class RsassaPkcs1V15PrivateKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } @@ -405,9 +405,9 @@ final class RsassaPkcs1V15PrivateKey { /// /// {@macro RSASSA-PKCS1-v1_5-Example:generate-sign-verify} /// -/// [1]: https://tools.ietf.org/html/rfc3447 -/// [2]: https://tools.ietf.org/html/rfc5280 -/// [3]: https://tools.ietf.org/html/rfc7517 +/// [1]: https://www.rfc-editor.org/rfc/rfc3447 +/// [2]: https://www.rfc-editor.org/rfc/rfc5280 +/// [3]: https://www.rfc-editor.org/rfc/rfc7517 final class RsassaPkcs1V15PublicKey { final RsaSsaPkcs1V15PublicKeyImpl _impl; @@ -449,7 +449,7 @@ final class RsassaPkcs1V15PublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 static Future importSpkiKey( List keyData, Hash hash, @@ -494,7 +494,7 @@ final class RsassaPkcs1V15PublicKey { /// /// {@macro RSA-importJsonWebKey:use-key_ops} /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 static Future importJsonWebKey( Map jwk, Hash hash, @@ -600,7 +600,7 @@ final class RsassaPkcs1V15PublicKey { /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc5280 + /// [1]: https://www.rfc-editor.org/rfc/rfc5280 Future exportSpkiKey() => _impl.exportSpkiKey(); /// Export RSASSA-PKCS1-v1_5 public key in [JSON Web Key][1] format. @@ -628,6 +628,6 @@ final class RsassaPkcs1V15PublicKey { /// print(jsonEncode(jwk)); /// ``` /// - /// [1]: https://tools.ietf.org/html/rfc7517 + /// [1]: https://www.rfc-editor.org/rfc/rfc7517 Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 86e65e89..1f0cf47f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -119,6 +119,9 @@ endif() if(WIN32) # Windows/MSVC option from CMakeLists.txt in BoringSSL # Use NASM on Windows. + if(NOT CMAKE_ASM_NASM_COMPILER) + find_program(CMAKE_ASM_NASM_COMPILER NAMES nasm nasm.exe PATHS "$ENV{USERPROFILE}/scoop/shims" "$ENV{USERPROFILE}/scoop/apps/nasm/current" "C:/Users/yatin/scoop/shims") + endif() enable_language(ASM_NASM) set(OPENSSL_NASM TRUE) set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8") @@ -160,4 +163,4 @@ set_target_properties( C_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1 -) +) \ No newline at end of file From 278a333c99ac3234da834819924ae84050d76ad9 Mon Sep 17 00:00:00 2001 From: YATIN072007 Date: Tue, 20 Jan 2026 22:17:07 +0530 Subject: [PATCH 2/4] Refactor Dartdoc examples to be self-contained --- lib/src/webcrypto/webcrypto.aescbc.dart | 147 ++--- lib/src/webcrypto/webcrypto.aesctr.dart | 181 +++--- lib/src/webcrypto/webcrypto.aesgcm.dart | 131 +++-- lib/src/webcrypto/webcrypto.digest.dart | 85 +-- lib/src/webcrypto/webcrypto.ecdh.dart | 18 +- lib/src/webcrypto/webcrypto.hkdf.dart | 30 +- lib/src/webcrypto/webcrypto.hmac.dart | 163 +++--- lib/src/webcrypto/webcrypto.pbkdf2.dart | 30 +- lib/src/webcrypto/webcrypto.rsaoaep.dart | 468 +++++++-------- lib/src/webcrypto/webcrypto.rsapss.dart | 538 +++++++++--------- .../webcrypto/webcrypto.rsassapkcs1v15.dart | 494 ++++++++-------- 11 files changed, 1223 insertions(+), 1062 deletions(-) diff --git a/lib/src/webcrypto/webcrypto.aescbc.dart b/lib/src/webcrypto/webcrypto.aescbc.dart index 6f5df25c..771abf25 100644 --- a/lib/src/webcrypto/webcrypto.aescbc.dart +++ b/lib/src/webcrypto/webcrypto.aescbc.dart @@ -56,19 +56,21 @@ final class AesCbcSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// final rawKey = Uint8List(16); - /// fillRandomBytes(rawKey); + /// Future main() async { + /// final rawKey = Uint8List(16); + /// fillRandomBytes(rawKey); /// - /// final k = await AesCbcSecretKey.importRawKey(rawKey); + /// final k = await AesCbcSecretKey.importRawKey(rawKey); /// - /// // Use a unique IV for each message. - /// final iv = Uint8List(16); - /// fillRandomBytes(iv); + /// // Use a unique IV for each message. + /// final iv = Uint8List(16); + /// fillRandomBytes(iv); /// - /// // Encrypt a message - /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); + /// // Encrypt a message + /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); /// - /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// } /// ``` static Future importRawKey(List keyData) async { final impl = await webCryptImpl.aesCbcSecretKey.importRawKey(keyData); @@ -95,15 +97,17 @@ final class AesCbcSecretKey { /// import 'dart:convert' show jsonEncode, jsonDecode; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "oct", "alg": "A256CBC", "k": ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "oct", "alg": "A256CBC", "k": "Y0ztPO2iDca0H0iM6y0_s0ztPO2iDca0H0iM6y0_s0w"}'; /// - /// // Import secret key from decoded JSON. - /// final key = await AesCbcSecretKey.importJsonWebKey(jsonDecode(jwk)); + /// // Import secret key from decoded JSON. + /// final key = await AesCbcSecretKey.importJsonWebKey(jsonDecode(jwk)); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await key.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await key.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -129,8 +133,10 @@ final class AesCbcSecretKey { /// ```dart /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CBC secret key for AES-256. - /// final key = await AesCbcSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-CBC secret key for AES-256. + /// final key = await AesCbcSecretKey.generateKey(256); + /// } /// ``` static Future generateKey(int length) async { final impl = await webCryptImpl.aesCbcSecretKey.generateKey(length); @@ -162,18 +168,20 @@ final class AesCbcSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CBC secret key for AES-256. - /// final k = await AesCbcSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-CBC secret key for AES-256. + /// final k = await AesCbcSecretKey.generateKey(256); /// - /// // Use a unique IV for each message. - /// final iv = Uint8List(16); - /// fillRandomBytes(iv); + /// // Use a unique IV for each message. + /// final iv = Uint8List(16); + /// fillRandomBytes(iv); /// - /// // Encrypt a message - /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); + /// // Encrypt a message + /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); /// - /// // Decrypt message (requires the same iv) - /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// // Decrypt message (requires the same iv) + /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// } /// ``` /// {@endtemplate} /// @@ -197,28 +205,32 @@ final class AesCbcSecretKey { /// import 'package:async/async.dart' show collectBytes; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CBC secret key for AES-256. - /// final k = await AesCbcSecretKey.generate(256); - /// - /// // Use a unique IV for each message. - /// final iv = Uint8List(16); - /// fillRandomBytes(iv); - /// - /// // Encrypt a message from file and write to file - /// final inputFile = File('message.txt'); - /// final encryptedFile = File('encrypted-message.binary'); - /// final c = await k.encryptStream( - /// inputFile.openRead(), - /// iv, - /// ).pipe(encryptedFile.openWrite()); - /// - /// // Decrypt message (requires the same iv) - /// final decryptedBytes = await collectBytes(k.decryptStream( - /// encryptedFile.openRead(), - /// iv, // same iv as used for encryption - /// )); - /// // decryptedBytes should be equal to contents of inputFile - /// assert(utf8.decode(decryptedBytes) == inputFile.readAsStringSync()); + /// Future main() async { + /// // Generate a new random AES-CBC secret key for AES-256. + /// final k = await AesCbcSecretKey.generateKey(256); + /// + /// // Use a unique IV for each message. + /// final iv = Uint8List(16); + /// fillRandomBytes(iv); + /// + /// // Encrypt a message from file and write to file + /// final inputFile = File('message.txt'); + /// await inputFile.writeAsString('hello world'); + /// final encryptedFile = File('encrypted-message.binary'); + /// await k.encryptStream( + /// inputFile.openRead(), + /// iv, + /// ).pipe(encryptedFile.openWrite()); + /// + /// // Decrypt message (requires the same iv) + /// final decryptedBytes = await collectBytes(k.decryptStream( + /// encryptedFile.openRead(), + /// iv, // same iv as used for encryption + /// )); + /// // decryptedBytes should be equal to contents of inputFile + /// // assert(utf8.decode(decryptedBytes) == inputFile.readAsStringSync()); + /// print(utf8.decode(decryptedBytes)); + /// } /// ``` /// {@endtemplate} /// @@ -269,18 +281,21 @@ final class AesCbcSecretKey { /// **Example** /// ```dart /// import 'package:webcrypto/webcrypto.dart'; + /// import 'dart:convert' show base64; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesCbcSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesCbcSecretKey.generateKey(256); /// - /// // Extract the secret key. - /// final secretBytes = await key.exportRawKey(); + /// // Extract the secret key. + /// final secretBytes = await key.exportRawKey(); /// - /// // Print the key as base64 - /// print(base64.encode(secretBytes)); + /// // Print the key as base64 + /// print(base64.encode(secretBytes)); /// - /// // If we wanted to we could import the key as follows: - /// // key = await AesCbcSecretKey.importRawKey(secretBytes); + /// // If we wanted to we could import the key as follows: + /// // key = await AesCbcSecretKey.importRawKey(secretBytes); + /// } /// ``` Future exportRawKey() => _impl.exportRawKey(); @@ -293,16 +308,18 @@ final class AesCbcSecretKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesCbcSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesCbcSecretKey.generateKey(256); /// - /// // Export the secret key. - /// final jwk = await key.exportJsonWebKey(); + /// // Export the secret key. + /// final jwk = await key.exportJsonWebKey(); /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "oct", "alg": "A256CBC", "k": ...} - /// print(jsonEncode(jwk)); + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "oct", "alg": "A256CBC", "k": ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.aesctr.dart b/lib/src/webcrypto/webcrypto.aesctr.dart index 861e3755..cb3628c7 100644 --- a/lib/src/webcrypto/webcrypto.aesctr.dart +++ b/lib/src/webcrypto/webcrypto.aesctr.dart @@ -47,25 +47,27 @@ final class AesCtrSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// final rawKey = Uint8List(16); - /// fillRandomBytes(rawKey); + /// Future main() async { + /// final rawKey = Uint8List(16); + /// fillRandomBytes(rawKey); /// - /// // Import key from raw bytes - /// final k = await AesCtrSecretKey.importRawKey(rawKey); + /// // Import key from raw bytes + /// final k = await AesCtrSecretKey.importRawKey(rawKey); /// - /// // Use a unique counter for each message. - /// final ctr = Uint8List(16); // always 16 bytes - /// fillRandomBytes(ctr); + /// // Use a unique counter for each message. + /// final ctr = Uint8List(16); // always 16 bytes + /// fillRandomBytes(ctr); /// - /// // Length of the counter, the N'th right most bits of ctr are incremented - /// // for each block, the left most 128 - N bits are used as static nonce. - /// final N = 64; + /// // Length of the counter, the N'th right most bits of ctr are incremented + /// // for each block, the left most 128 - N bits are used as static nonce. + /// final N = 64; /// - /// // Encrypt a message - /// final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N); + /// // Encrypt a message + /// final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N); /// - /// // Decrypt message (requires the same counter ctr and length N) - /// print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world + /// // Decrypt message (requires the same counter ctr and length N) + /// print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world + /// } /// ``` static Future importRawKey(List keyData) async { final impl = await webCryptImpl.aesCtrSecretKey.importRawKey(keyData); @@ -92,15 +94,17 @@ final class AesCtrSecretKey { /// import 'dart:convert' show jsonEncode, jsonDecode; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "oct", "alg": "A256CTR", "k": ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "oct", "alg": "A256CTR", "k": "Y0ztPO2iDca0H0iM6y0_s0ztPO2iDca0H0iM6y0_s0w"}'; /// - /// // Import secret key from decoded JSON. - /// final key = await AesCtrSecretKey.importJsonWebKey(jsonDecode(jwk)); + /// // Import secret key from decoded JSON. + /// final key = await AesCtrSecretKey.importJsonWebKey(jsonDecode(jwk)); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await key.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await key.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -124,8 +128,10 @@ final class AesCtrSecretKey { /// ```dart /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CTR secret key for AES-256. - /// final key = await AesCtrSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-CTR secret key for AES-256. + /// final key = await AesCtrSecretKey.generateKey(256); + /// } /// ``` static Future generateKey(int length) async { final impl = await webCryptImpl.aesCtrSecretKey.generateKey(length); @@ -153,23 +159,25 @@ final class AesCtrSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CTR secret key for AES-256. - /// final k = await AesCtrSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-CTR secret key for AES-256. + /// final k = await AesCtrSecretKey.generateKey(256); /// - /// // Use a unique counter for each message. - /// final ctr = Uint8List(16); // always 16 bytes - /// fillRandomBytes(ctr); + /// // Use a unique counter for each message. + /// final ctr = Uint8List(16); // always 16 bytes + /// fillRandomBytes(ctr); /// - /// // Length of the counter, the N'th right most bits of ctr are incremented - /// // for each block, the left most 128 - N bits are used as static nonce. - /// // Thus, messages must be less than 2^64 * 16 bytes. - /// final N = 64; + /// // Length of the counter, the N'th right most bits of ctr are incremented + /// // for each block, the left most 128 - N bits are used as static nonce. + /// // Thus, messages must be less than 2^64 * 16 bytes. + /// final N = 64; /// - /// // Encrypt a message - /// final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N); + /// // Encrypt a message + /// final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N); /// - /// // Decrypt message (requires the same counter ctr and length N) - /// print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world + /// // Decrypt message (requires the same counter ctr and length N) + /// print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world + /// } /// ``` /// {@endtemplate} /// @@ -203,36 +211,40 @@ final class AesCtrSecretKey { /// import 'package:async/async.dart' show collectBytes; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-CTR secret key for AES-256. - /// final k = await AesCtrSecretKey.generate(256); - /// - /// // Use a unique counter for each message. - /// final ctr = Uint8List(16); // always 16 bytes - /// fillRandomBytes(ctr); - /// - /// // Length of the counter, the N'th right most bits of ctr are incremented - /// // for each block, the left most 128 - N bits are used as static nonce. - /// // Thus, messages must be less than 2^64 * 16 bytes. - /// final N = 64; - /// - /// // Encrypt a message from file and write to file - /// final inputFile = File('message.txt'); - /// final encryptedFile = File('encrypted-message.binary'); - /// final c = await k.encryptStream( - /// inputFile.openRead(), - /// ctr, - /// N, - /// ).pipe(encryptedFile.openWrite()); - /// - /// - /// // Decrypt message (requires the same counter ctr and length N) - /// final decryptedBytes = await collectBytes(k.decryptStream( - /// encryptedFile.openRead(), - /// ctr, // same ctr as used for encryption - /// N, // same N as used for encryption - /// )); - /// // decryptedBytes should be equal to contents of inputFile - /// assert(utf8.decode(decryptedBytes) == inputFile.readAsStringSync()); + /// Future main() async { + /// // Generate a new random AES-CTR secret key for AES-256. + /// final k = await AesCtrSecretKey.generateKey(256); + /// + /// // Use a unique counter for each message. + /// final ctr = Uint8List(16); // always 16 bytes + /// fillRandomBytes(ctr); + /// + /// // Length of the counter, the N'th right most bits of ctr are incremented + /// // for each block, the left most 128 - N bits are used as static nonce. + /// // Thus, messages must be less than 2^64 * 16 bytes. + /// final N = 64; + /// + /// // Encrypt a message from file and write to file + /// final inputFile = File('message.txt'); + /// await inputFile.writeAsString('hello world'); + /// final encryptedFile = File('encrypted-message.binary'); + /// await k.encryptStream( + /// inputFile.openRead(), + /// ctr, + /// N, + /// ).pipe(encryptedFile.openWrite()); + /// + /// + /// // Decrypt message (requires the same counter ctr and length N) + /// final decryptedBytes = await collectBytes(k.decryptStream( + /// encryptedFile.openRead(), + /// ctr, // same ctr as used for encryption + /// N, // same N as used for encryption + /// )); + /// // decryptedBytes should be equal to contents of inputFile + /// // assert(utf8.decode(decryptedBytes) == inputFile.readAsStringSync()); + /// print(utf8.decode(decryptedBytes)); + /// } /// ``` /// {@endtemplate} /// @@ -290,18 +302,21 @@ final class AesCtrSecretKey { /// **Example** /// ```dart /// import 'package:webcrypto/webcrypto.dart'; + /// import 'dart:convert' show base64; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesCtrSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesCtrSecretKey.generateKey(256); /// - /// // Extract the secret key. - /// final secretBytes = await key.exportRawKey(); + /// // Extract the secret key. + /// final secretBytes = await key.exportRawKey(); /// - /// // Print the key as base64 - /// print(base64.encode(secretBytes)); + /// // Print the key as base64 + /// print(base64.encode(secretBytes)); /// - /// // If we wanted to we could import the key as follows: - /// // key = await AesCtrSecretKey.importRawKey(secretBytes); + /// // If we wanted to we could import the key as follows: + /// // key = await AesCtrSecretKey.importRawKey(secretBytes); + /// } /// ``` Future exportRawKey() => _impl.exportRawKey(); @@ -314,16 +329,18 @@ final class AesCtrSecretKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesCtrSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesCtrSecretKey.generateKey(256); /// - /// // Export the secret key. - /// final jwk = await key.exportJsonWebKey(); + /// // Export the secret key. + /// final jwk = await key.exportJsonWebKey(); /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "oct", "alg": "A256CTR", "k": ...} - /// print(jsonEncode(jwk)); + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "oct", "alg": "A256CTR", "k": ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.aesgcm.dart b/lib/src/webcrypto/webcrypto.aesgcm.dart index a8821eaa..014c05fd 100644 --- a/lib/src/webcrypto/webcrypto.aesgcm.dart +++ b/lib/src/webcrypto/webcrypto.aesgcm.dart @@ -51,21 +51,23 @@ final class AesGcmSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// final rawKey = Uint8List(16); - /// fillRandomBytes(rawKey); + /// Future main() async { + /// final rawKey = Uint8List(16); + /// fillRandomBytes(rawKey); /// - /// // Import key from raw bytes - /// final k = await AesGcmSecretKey.importRawKey(rawKey); + /// // Import key from raw bytes + /// final k = await AesGcmSecretKey.importRawKey(rawKey); /// - /// // Use a unique IV for each message. - /// final iv = Uint8List(16); - /// fillRandomBytes(iv); + /// // Use a unique IV for each message. + /// final iv = Uint8List(16); + /// fillRandomBytes(iv); /// - /// // Encrypt a message - /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); + /// // Encrypt a message + /// final c = await k.encryptBytes(utf8.encode('hello world'), iv); /// - /// // Decrypt message (requires the same iv) - /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// // Decrypt message (requires the same iv) + /// print(utf8.decode(await k.decryptBytes(c, iv))); // hello world + /// } /// ``` static Future importRawKey(List keyData) async { final impl = await webCryptImpl.aesGcmSecretKey.importRawKey(keyData); @@ -92,15 +94,17 @@ final class AesGcmSecretKey { /// import 'dart:convert' show jsonEncode, jsonDecode; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "oct", "alg": "A256GCM", "k": ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "oct", "alg": "A256GCM", "k": "Y0ztPO2iDca0H0iM6y0_s0ztPO2iDca0H0iM6y0_s0w"}'; /// - /// // Import secret key from decoded JSON. - /// final key = await AesGcmSecretKey.importJsonWebKey(jsonDecode(jwk)); + /// // Import secret key from decoded JSON. + /// final key = await AesGcmSecretKey.importJsonWebKey(jsonDecode(jwk)); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await key.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await key.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -124,8 +128,10 @@ final class AesGcmSecretKey { /// ```dart /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-GCM secret key for AES-256. - /// final key = await AesGcmSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-GCM secret key for AES-256. + /// final key = await AesGcmSecretKey.generateKey(256); + /// } /// ``` static Future generateKey(int length) async { final impl = await webCryptImpl.aesGcmSecretKey.generateKey(length); @@ -181,29 +187,31 @@ final class AesGcmSecretKey { /// import 'dart:typed_data' show Uint8List; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random AES-GCM secret key for AES-256. - /// final k = await AesGcmSecretKey.generate(256); - /// - /// // Use a unique IV for each message. - /// final iv = Uint8List(16); - /// fillRandomBytes(iv); - /// - /// // Specify optional additionalData - /// final ad = utf8.encode('my-test-message'); - /// - /// // Encrypt a message - /// final c = await k.encryptBytes( - /// utf8.encode('hello world'), - /// iv, - /// additionalData: ad, - /// ); - /// - /// // Decrypt message (requires the same iv) - /// print(utf8.decode(await k.decryptBytes( - /// c, - /// iv, - /// additionalData: ad, - /// ))); // hello world + /// Future main() async { + /// // Generate a new random AES-GCM secret key for AES-256. + /// final k = await AesGcmSecretKey.generateKey(256); + /// + /// // Use a unique IV for each message. + /// final iv = Uint8List(16); + /// fillRandomBytes(iv); + /// + /// // Specify optional additionalData + /// final ad = utf8.encode('my-test-message'); + /// + /// // Encrypt a message + /// final c = await k.encryptBytes( + /// utf8.encode('hello world'), + /// iv, + /// additionalData: ad, + /// ); + /// + /// // Decrypt message (requires the same iv) + /// print(utf8.decode(await k.decryptBytes( + /// c, + /// iv, + /// additionalData: ad, + /// ))); // hello world + /// } /// ``` /// {@endtemplate} /// @@ -253,18 +261,21 @@ final class AesGcmSecretKey { /// **Example** /// ```dart /// import 'package:webcrypto/webcrypto.dart'; + /// import 'dart:convert' show base64; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesGcmSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesGcmSecretKey.generateKey(256); /// - /// // Extract the secret key. - /// final secretBytes = await key.exportRawKey(); + /// // Extract the secret key. + /// final secretBytes = await key.exportRawKey(); /// - /// // Print the key as base64 - /// print(base64.encode(secretBytes)); + /// // Print the key as base64 + /// print(base64.encode(secretBytes)); /// - /// // If we wanted to we could import the key as follows: - /// // key = await AesGcmSecretKey.importRawKey(secretBytes); + /// // If we wanted to we could import the key as follows: + /// // key = await AesGcmSecretKey.importRawKey(secretBytes); + /// } /// ``` Future exportRawKey() => _impl.exportRawKey(); @@ -277,16 +288,18 @@ final class AesGcmSecretKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a new random AES-256 secret key. - /// final key = await AesGcmSecretKey.generate(256); + /// Future main() async { + /// // Generate a new random AES-256 secret key. + /// final key = await AesGcmSecretKey.generateKey(256); /// - /// // Export the secret key. - /// final jwk = await key.exportJsonWebKey(); + /// // Export the secret key. + /// final jwk = await key.exportJsonWebKey(); /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "oct", "alg": "A256GCM", "k": ...} - /// print(jsonEncode(jwk)); + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "oct", "alg": "A256GCM", "k": ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.digest.dart b/lib/src/webcrypto/webcrypto.digest.dart index 54370e75..4e653db8 100644 --- a/lib/src/webcrypto/webcrypto.digest.dart +++ b/lib/src/webcrypto/webcrypto.digest.dart @@ -61,20 +61,23 @@ abstract final class Hash { /// import 'dart:convert' show base64; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Pick a file to hash. - /// String fileToHash = '/etc/passwd'; - /// - /// // Compute hash of fileToHash with sha-256 - /// List hash; - /// final stream = File(fileToHash).openRead(); - /// try { - /// hash = await Hash.sha256.digestStream(stream); - /// } finally { - /// await stream.close(); // always close the stream + /// Future main() async { + /// // Pick a file to hash. + /// final fileToHash = File('example.txt'); + /// await fileToHash.writeAsString('hello world'); + /// + /// // Compute hash of fileToHash with sha-256 + /// List hash; + /// final stream = fileToHash.openRead(); + /// try { + /// hash = await Hash.sha256.digestStream(stream); + /// } finally { + /// await stream.close(); // always close the stream + /// } + /// + /// // Print the base64 encoded hash + /// print(base64.encode(hash)); /// } - /// - /// // Print the base64 encoded hash - /// print(base64.encode(hash)); /// ``` Future digestStream(Stream> data) => _impl.digestStream(data); @@ -89,14 +92,16 @@ abstract final class Hash { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Convert 'hello world' to a byte array - /// final bytesToHash = utf8.encode('hello world'); + /// Future main() async { + /// // Convert 'hello world' to a byte array + /// final bytesToHash = utf8.encode('hello world'); /// - /// // Compute hash of bytesToHash with sha-256 - /// List hash = await Hash.sha256.digestBytes(bytesToHash); + /// // Compute hash of bytesToHash with sha-256 + /// List hash = await Hash.sha256.digestBytes(bytesToHash); /// - /// // Print the base64 encoded hash - /// print(base64.encode(hash)); + /// // Print the base64 encoded hash + /// print(base64.encode(hash)); + /// } /// ``` /// /// [1]: https://doi.org/10.6028/NIST.FIPS.180-4 @@ -109,14 +114,16 @@ abstract final class Hash { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Convert 'hello world' to a byte array - /// final bytesToHash = utf8.encode('hello world'); + /// Future main() async { + /// // Convert 'hello world' to a byte array + /// final bytesToHash = utf8.encode('hello world'); /// - /// // Compute hash of bytesToHash with sha-256 - /// List hash = await Hash.sha256.digestBytes(bytesToHash); + /// // Compute hash of bytesToHash with sha-256 + /// List hash = await Hash.sha256.digestBytes(bytesToHash); /// - /// // Print the base64 encoded hash - /// print(base64.encode(hash)); + /// // Print the base64 encoded hash + /// print(base64.encode(hash)); + /// } /// ``` /// /// [1]: https://doi.org/10.6028/NIST.FIPS.180-4 @@ -129,14 +136,16 @@ abstract final class Hash { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Convert 'hello world' to a byte array - /// final bytesToHash = utf8.encode('hello world'); + /// Future main() async { + /// // Convert 'hello world' to a byte array + /// final bytesToHash = utf8.encode('hello world'); /// - /// // Compute hash of bytesToHash with sha-384 - /// List hash = await Hash.sha384.digestBytes(bytesToHash); + /// // Compute hash of bytesToHash with sha-384 + /// List hash = await Hash.sha384.digestBytes(bytesToHash); /// - /// // Print the base64 encoded hash - /// print(base64.encode(hash)); + /// // Print the base64 encoded hash + /// print(base64.encode(hash)); + /// } /// ``` /// /// [1]: https://doi.org/10.6028/NIST.FIPS.180-4 @@ -149,14 +158,16 @@ abstract final class Hash { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Convert 'hello world' to a byte array - /// final bytesToHash = utf8.encode('hello world'); + /// Future main() async { + /// // Convert 'hello world' to a byte array + /// final bytesToHash = utf8.encode('hello world'); /// - /// // Compute hash of bytesToHash with sha-512 - /// List hash = await Hash.sha512.digestBytes(bytesToHash); + /// // Compute hash of bytesToHash with sha-512 + /// List hash = await Hash.sha512.digestBytes(bytesToHash); /// - /// // Print the base64 encoded hash - /// print(base64.encode(hash)); + /// // Print the base64 encoded hash + /// print(base64.encode(hash)); + /// } /// ``` /// /// [1]: https://doi.org/10.6028/NIST.FIPS.180-4 diff --git a/lib/src/webcrypto/webcrypto.ecdh.dart b/lib/src/webcrypto/webcrypto.ecdh.dart index 064dbb17..c7573f46 100644 --- a/lib/src/webcrypto/webcrypto.ecdh.dart +++ b/lib/src/webcrypto/webcrypto.ecdh.dart @@ -70,16 +70,18 @@ final class EcdhPrivateKey { /// import 'package:pem/pem.dart'; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Read key data from a PEM encoded block. This will remove the - /// // the padding, decode base64 and return the encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(''' - /// -----BEGIN PRIVATE KEY----- - /// MIGHAgEAMBMGByqGSM4..... - /// -----END PRIVATE KEY----- + /// Future main() async { + /// // Read key data from a PEM encoded block. This will remove the + /// // the padding, decode base64 and return the encoded bytes. + /// // Sample P-256 private key. + /// List keyData = PemCodec(PemLabel.privateKey).decode(''' + /// -----BEGIN PRIVATE KEY----- + /// MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYY/bM/GkKkXyT+rM + /// HuX+aC2s7n+L/F9X7M2I2/9Q8nehRANCAAQW7urj+lFz0F7cK5n8+C9q6v/9/6+1 + /// 9/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+0= + /// -----END PRIVATE KEY----- /// '''); /// - /// - /// Future main() async { /// // Import the Private Key from a Binary PEM decoded data. /// final privateKey = await EcdhPrivateKey.importPkcs8Key( /// keyData, diff --git a/lib/src/webcrypto/webcrypto.hkdf.dart b/lib/src/webcrypto/webcrypto.hkdf.dart index 288a0443..a6a3bdef 100644 --- a/lib/src/webcrypto/webcrypto.hkdf.dart +++ b/lib/src/webcrypto/webcrypto.hkdf.dart @@ -28,22 +28,24 @@ part of 'webcrypto.dart'; /// import 'dart:convert' show utf8, base64; /// import 'package:webcrypto/webcrypto.dart'; /// -/// // Provide a password to be used for key derivation -/// final key = await HkdfSecretKey.importRawKey(utf8.decode( -/// 'my-password-in-plain-text', -/// )); +/// Future main() async { +/// // Provide a password to be used for key derivation +/// final key = await HkdfSecretKey.importRawKey(utf8.decode( +/// 'my-password-in-plain-text', +/// )); /// -/// // Derive a key from password -/// final derivedKey = await HkdfSecretKey.deriveBits( -/// 256, // number of bits to derive. -/// Hash.sha256, -/// utf8.decode('unique salt'), -/// utf8.decode('creating derivedKey in example'), -/// ); +/// // Derive a key from password +/// final derivedKey = await HkdfSecretKey.deriveBits( +/// 256, // number of bits to derive. +/// Hash.sha256, +/// utf8.decode('unique salt'), +/// utf8.decode('creating derivedKey in example'), +/// ); /// -/// // Print the derived key, this could also be used as basis for other new -/// // symmetric cryptographic keys. -/// print(base64.encode(derivedKey)); +/// // Print the derived key, this could also be used as basis for other new +/// // symmetric cryptographic keys. +/// print(base64.encode(derivedKey)); +/// } /// ``` /// {@endtemplate} /// diff --git a/lib/src/webcrypto/webcrypto.hmac.dart b/lib/src/webcrypto/webcrypto.hmac.dart index 63f4dd9e..86b320fd 100644 --- a/lib/src/webcrypto/webcrypto.hmac.dart +++ b/lib/src/webcrypto/webcrypto.hmac.dart @@ -65,13 +65,15 @@ final class HmacSecretKey { /// /// **Example** /// ```dart - /// import 'dart:convert' show utf8; + /// import 'dart:convert' show utf8, base64; /// import 'package:webcrypto/webcrypto.dart'; /// - /// final key = await HmacSecretKey.importRawKey( - /// base64.decode('WzIxLDg0LDEwMCw5OSwxMCwxMDUsMjIsODAsMTkwLDExNiwyMDMsMjQ5XQ=='), - /// Hash.sha256, - /// ); + /// Future main() async { + /// final key = await HmacSecretKey.importRawKey( + /// base64.decode('WzIxLDg0LDEwMCw5OSwxMCwxMDUsMjIsODAsMTkwLDExNiwyMDMsMjQ5XQ=='), + /// Hash.sha256, + /// ); + /// } /// ``` static Future importRawKey( List keyData, @@ -129,18 +131,20 @@ final class HmacSecretKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "oct", "alg": "HS256", "k": ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "oct", "alg": "HS256", "k": "Y0ztPO2iDca0H0iM6y0_s0ztPO2iDca0H0iM6y0_s0w"}'; /// - /// // Import secret key from decoded JSON. - /// final key = await HmacSecretKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import secret key from decoded JSON. + /// final key = await HmacSecretKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await key.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await key.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -193,8 +197,10 @@ final class HmacSecretKey { /// ```dart /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a new random HMAC secret key. - /// final key = await HmacSecretKey.generate(Hash.sha256); + /// Future main() async { + /// // Generate a new random HMAC secret key. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); + /// } /// ``` static Future generateKey(Hash hash, {int? length}) async { if (length != null && length <= 0) { @@ -219,16 +225,18 @@ final class HmacSecretKey { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate an HmacSecretKey. - /// final key = await HmacSecretKey.generateKey(Hash.sha256); + /// Future main() async { + /// // Generate an HmacSecretKey. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// String stringToSign = 'example-string-to-signed'; + /// String stringToSign = 'example-string-to-signed'; /// - /// // Compute signature. - /// final signature = await key.signBytes(utf8.encode(stringToSign)); + /// // Compute signature. + /// final signature = await key.signBytes(utf8.encode(stringToSign)); /// - /// // Print as base64 - /// print(base64.encode(signature)); + /// // Print as base64 + /// print(base64.encode(signature)); + /// } /// ``` /// /// {@template HMAC-sign:do-not-validate-using-sign} @@ -252,18 +260,20 @@ final class HmacSecretKey { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate an HmacSecretKey. - /// final key = await HmacSecretKey.generateKey(Hash.sha256); + /// Future main() async { + /// // Generate an HmacSecretKey. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// String stringToSign = 'example-string-to-signed'; + /// String stringToSign = 'example-string-to-signed'; /// - /// // Compute signature. - /// final signature = await key.signStream(Stream.fromIterable([ - /// utf8.encode(stringToSign), - /// ])); + /// // Compute signature. + /// final signature = await key.signStream(Stream.fromIterable([ + /// utf8.encode(stringToSign), + /// ])); /// - /// // Print as base64 - /// print(base64.encode(signature)); + /// // Print as base64 + /// print(base64.encode(signature)); + /// } /// ``` /// /// {@macro HMAC-sign:do-not-validate-using-sign} @@ -289,20 +299,22 @@ final class HmacSecretKey { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate an HmacSecretKey. - /// final key = await HmacSecretKey.generateKey(Hash.sha256); + /// Future main() async { + /// // Generate an HmacSecretKey. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// String stringToSign = 'example-string-to-signed'; + /// String stringToSign = 'example-string-to-signed'; /// - /// // Compute signature. - /// final signature = await key.signBytes(utf8.encode(stringToSign)); + /// // Compute signature. + /// final signature = await key.signBytes(utf8.encode(stringToSign)); /// - /// // Verify signature. - /// final result = await key.verifyBytes( - /// signature, - /// utf8.encode(stringToSign), - /// ); - /// assert(result == true, 'this signature should be valid'); + /// // Verify signature. + /// final result = await key.verifyBytes( + /// signature, + /// utf8.encode(stringToSign), + /// ); + /// assert(result == true, 'this signature should be valid'); + /// } /// ``` Future verifyBytes(List signature, List data) => _impl.verifyBytes(signature, data); @@ -320,21 +332,23 @@ final class HmacSecretKey { /// import 'dart:convert' show base64, utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate an HmacSecretKey. - /// final key = await HmacSecretKey.generateKey(Hash.sha256); + /// Future main() async { + /// // Generate an HmacSecretKey. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// String stringToSign = 'example-string-to-signed'; + /// String stringToSign = 'example-string-to-signed'; /// - /// // Compute signature. - /// final signature = await key.signBytes(Stream.fromIterable([ - /// utf8.encode(stringToSign), - /// ])); + /// // Compute signature. + /// final signature = await key.signStream(Stream.fromIterable([ + /// utf8.encode(stringToSign), + /// ])); /// - /// // Verify signature. - /// final result = await key.verifyStream(signature, Stream.fromIterable([ - /// utf8.encode(stringToSign), - /// ])); - /// assert(result == true, 'this signature should be valid'); + /// // Verify signature. + /// final result = await key.verifyStream(signature, Stream.fromIterable([ + /// utf8.encode(stringToSign), + /// ])); + /// assert(result == true, 'this signature should be valid'); + /// } /// ``` Future verifyStream(List signature, Stream> data) => _impl.verifyStream(signature, data); @@ -347,18 +361,21 @@ final class HmacSecretKey { /// **Example** /// ```dart /// import 'package:webcrypto/webcrypto.dart'; + /// import 'dart:convert' show base64; /// - /// // Generate a new random HMAC secret key. - /// final key = await HmacSecretKey.generate(Hash.sha256); + /// Future main() async { + /// // Generate a new random HMAC secret key. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// // Extract the secret key. - /// final secretBytes = await key.exportRawKey(); + /// // Extract the secret key. + /// final secretBytes = await key.exportRawKey(); /// - /// // Print the key as base64 - /// print(base64.encode(secretBytes)); + /// // Print the key as base64 + /// print(base64.encode(secretBytes)); /// - /// // If we wanted to we could import the key as follows: - /// // key = await HmacSecretKey.importRawKey(secretBytes, Hash.sha256); + /// // If we wanted to we could import the key as follows: + /// // key = await HmacSecretKey.importRawKey(secretBytes, Hash.sha256); + /// } /// ``` Future exportRawKey() => _impl.exportRawKey(); @@ -371,16 +388,18 @@ final class HmacSecretKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a new random HMAC secret key. - /// final key = await HmacSecretKey.generate(Hash.sha256); + /// Future main() async { + /// // Generate a new random HMAC secret key. + /// final key = await HmacSecretKey.generateKey(Hash.sha256); /// - /// // Export the secret key. - /// final jwk = await key.exportJsonWebKey(); + /// // Export the secret key. + /// final jwk = await key.exportJsonWebKey(); /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "oct", "alg": "HS256", "k": ...} - /// print(jsonEncode(jwk)); + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "oct", "alg": "HS256", "k": ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.pbkdf2.dart b/lib/src/webcrypto/webcrypto.pbkdf2.dart index c82544b2..243227d2 100644 --- a/lib/src/webcrypto/webcrypto.pbkdf2.dart +++ b/lib/src/webcrypto/webcrypto.pbkdf2.dart @@ -29,22 +29,24 @@ part of 'webcrypto.dart'; /// import 'dart:convert' show utf8, base64; /// import 'package:webcrypto/webcrypto.dart'; /// -/// // Provide a password to be used for key derivation -/// final key = await Pbkdf2SecretKey.importRawKey(utf8.decode( -/// 'my-password-in-plain-text', -/// )); +/// Future main() async { +/// // Provide a password to be used for key derivation +/// final key = await Pbkdf2SecretKey.importRawKey(utf8.decode( +/// 'my-password-in-plain-text', +/// )); /// -/// // Derive a key from password -/// final derivedKey = await Pbkdf2SecretKey.deriveBits( -/// 256, // number of bits to derive. -/// Hash.sha256, -/// utf8.decode('unique salt'), -/// 100000, -/// ); +/// // Derive a key from password +/// final derivedKey = await Pbkdf2SecretKey.deriveBits( +/// 256, // number of bits to derive. +/// Hash.sha256, +/// utf8.decode('unique salt'), +/// 100000, +/// ); /// -/// // Print the derived key, this could also be used as basis for other new -/// // symmetric cryptographic keys. -/// print(base64.encode(derivedKey)); +/// // Print the derived key, this could also be used as basis for other new +/// // symmetric cryptographic keys. +/// print(base64.encode(derivedKey)); +/// } /// ``` /// {@endtemplate} /// diff --git a/lib/src/webcrypto/webcrypto.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index 155059f3..7a7cf5ef 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -33,30 +33,32 @@ part of 'webcrypto.dart'; /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// -/// // Generate a public / private key-pair. -/// final keyPair = await RsaOaepPrivateKey.generateKey( -/// 4096, -/// BigInt.from(65537), -/// Hash.sha256, -/// ); +/// Future main() async { +/// // Generate a public / private key-pair. +/// final keyPair = await RsaOaepPrivateKey.generateKey( +/// 4096, +/// BigInt.from(65537), +/// Hash.sha256, +/// ); /// -/// // Generate a 256 bit symmetric key -/// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); +/// // Generate a 256 bit symmetric key +/// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); /// -/// // Using publicKey Bob can encrypt secretKeyToBeShared, such that it can -/// // only be decrypted with the private key. -/// final encryptedRawKey = await keyPair.publicKey.encryptBytes( -/// await secretKeyToBeShared.exportRawKey(), -/// label: 'shared-key', -/// ); +/// // Using publicKey Bob can encrypt secretKeyToBeShared, such that it can +/// // only be decrypted with the private key. +/// final encryptedRawKey = await keyPair.publicKey.encryptBytes( +/// await secretKeyToBeShared.exportRawKey(), +/// label: utf8.encode('shared-key'), +/// ); /// -/// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. -/// final sharedRawSecretKey = await keypair.privateKey.decryptBytes( -/// encryptedRawKey, -/// label: 'shared-key', -/// ); -/// final sharedSecretKey = await AesGcmSecretKey.importRaw(sharedRawSecretKey); -/// // Now both Alice and Bob share a secret key. +/// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. +/// final sharedRawSecretKey = await keyPair.privateKey.decryptBytes( +/// encryptedRawKey, +/// label: utf8.encode('shared-key'), +/// ); +/// final sharedSecretKey = await AesGcmSecretKey.importRawKey(sharedRawSecretKey); +/// // Now both Alice and Bob share a secret key. +/// } /// ``` /// {@endtemplate} /// @@ -94,23 +96,25 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privateKey = await RsaOaepPrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await privateKey.exportPkcs8Key(); - /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsaOaepPrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await privateKey.exportPkcs8Key(); + /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5208 @@ -148,18 +152,20 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "RSA-OAEP-256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "RSA-OAEP-256", "n": "...", "e": "AQAB", "d": "...", "p": "...", "q": "...", "dp": "...", "dq": "...", "qi": "..."}'; /// - /// // Import private key from decoded JSON. - /// final privateKey = await RsaOaepPrivateKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import private key from decoded JSON. + /// final privateKey = await RsaOaepPrivateKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await privateKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await privateKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@macro RSA-importJsonWebKey:use-key_ops} @@ -186,43 +192,45 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export public, so Bob can use it - /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); - /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... - /// // Alice sends pemPublicKey to Bob - /// - /// // Bob can generate a 256 bit symmetric secret key - /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); - /// - /// // Using publicKey Bob can encrypt secretKeyToBeShared, such that it can - /// // only be decrypted with the private key. - /// final publicKey = RsaOaepPublicKey.importSpki( - /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), - /// Hash.sha256, - /// ) - /// final encryptedRawKey = await publicKey.encryptBytes( - /// await secretKeyToBeShared.exportRawKey(), - /// label: 'shared-key', - /// ); - /// // Bob sends Alice: encryptedRawKey - /// - /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. - /// final sharedRawSecretKey = await keypair.privateKey.decryptBytes( - /// encryptedRawKey, - /// label: 'shared-key', - /// ); - /// final sharedSecretKey = await AesGcmSecretKey.importRaw( - /// sharedRawSecretKey, - /// ); - /// // Now both Alice and Bob share a secret key. + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export public, so Bob can use it + /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); + /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... + /// // Alice sends pemPublicKey to Bob + /// + /// // Bob can generate a 256 bit symmetric secret key + /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); + /// + /// // Using publicKey Bob can encrypt secretKeyToBeShared, such that it can + /// // only be decrypted with the private key. + /// final publicKey = await RsaOaepPublicKey.importSpkiKey( + /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), + /// Hash.sha256, + /// ); + /// final encryptedRawKey = await publicKey.encryptBytes( + /// await secretKeyToBeShared.exportRawKey(), + /// label: utf8.encode('shared-key'), + /// ); + /// // Bob sends Alice: encryptedRawKey + /// + /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. + /// final sharedRawSecretKey = await keyPair.privateKey.decryptBytes( + /// encryptedRawKey, + /// label: utf8.encode('shared-key'), + /// ); + /// final sharedSecretKey = await AesGcmSecretKey.importRawKey( + /// sharedRawSecretKey, + /// ); + /// // Now both Alice and Bob share a secret key. + /// } /// ``` static Future> generateKey( int modulusLength, @@ -251,32 +259,34 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// // Alice sends keyPair.publicKey to Bob - /// - /// // Bob can generate a 256 bit symmetric secret key - /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); - /// - /// // Using the public key Bob can encrypt secretKeyToBeShared, such that it - /// // can only be decrypted with the private key. - /// final encryptedRawKey = await keyPair.publicKey.encryptBytes( - /// await secretKeyToBeShared.exportRawKey(), - /// ); - /// // Bob sends Alice: encryptedRawKey - /// - /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. - /// final sharedRawSecretKey = await keypair.privateKey.decryptBytes( - /// encryptedRawKey, - /// ); - /// final sharedSecretKey = await AesGcmSecretKey.importRaw( - /// sharedRawSecretKey, - /// ); - /// // Now both Alice and Bob share a secret key. + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// // Alice sends keyPair.publicKey to Bob + /// + /// // Bob can generate a 256 bit symmetric secret key + /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); + /// + /// // Using the public key Bob can encrypt secretKeyToBeShared, such that it + /// // can only be decrypted with the private key. + /// final encryptedRawKey = await keyPair.publicKey.encryptBytes( + /// await secretKeyToBeShared.exportRawKey(), + /// ); + /// // Bob sends Alice: encryptedRawKey + /// + /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. + /// final sharedRawSecretKey = await keyPair.privateKey.decryptBytes( + /// encryptedRawKey, + /// ); + /// final sharedSecretKey = await AesGcmSecretKey.importRawKey( + /// sharedRawSecretKey, + /// ); + /// // Now both Alice and Bob share a secret key. + /// } /// ``` Future decryptBytes(List data, {List? label}) => _impl.decryptBytes(data, label: label); @@ -291,20 +301,22 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the private key. - /// final rawPrivateKey = await keypair.privateKey.exportPkcs8Key(); - /// - /// // Private keys are often encoded as PEM. - /// // This encodes the key in base64 and wraps it with: - /// // '-----BEGIN PRIVATE KEY----'... - /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the private key. + /// final rawPrivateKey = await keyPair.privateKey.exportPkcs8Key(); + /// + /// // Private keys are often encoded as PEM. + /// // This encodes the key in base64 and wraps it with: + /// // '-----BEGIN PRIVATE KEY----'... + /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5208 @@ -319,20 +331,22 @@ final class RsaOaepPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the private key. - /// final jwk = await keypair.privateKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "RSA-OAEP-256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the private key. + /// final jwk = await keyPair.privateKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "RSA-OAEP-256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -380,23 +394,25 @@ final class RsaOaepPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.publicKey).decode(""" - /// -----BEGIN PUBLIC KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PUBLIC KEY----- - /// """); - /// - /// // Import public key from binary PEM decoded data. - /// final publicKey = await RsaOaepPublicKey.importSpkiKey( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await publicKey.exportSpkiKey(); - /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.publicKey).decode(""" + /// -----BEGIN PUBLIC KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PUBLIC KEY----- + /// """); + /// + /// // Import public key from binary PEM decoded data. + /// final publicKey = await RsaOaepPublicKey.importSpkiKey( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await publicKey.exportSpkiKey(); + /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -428,18 +444,20 @@ final class RsaOaepPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "RSA-OAEP-256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "RSA-OAEP-256", "n": "...", "e": "AQAB"}'; /// - /// // Import public key from decoded JSON. - /// final publicKey = await RsaOaepPublicKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import public key from decoded JSON. + /// final publicKey = await RsaOaepPublicKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await publicKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await publicKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@macro RSA-importJsonWebKey:use-key_ops} @@ -484,34 +502,36 @@ final class RsaOaepPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// // Alice sends keyPair.publicKey to Bob - /// - /// // Bob can generate a 256 bit symmetric secret key - /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); - /// - /// // Using the public key Bob can encrypt secretKeyToBeShared, such that it - /// // can only be decrypted with the private key. - /// final encryptedRawKey = await keyPair.publicKey.encryptBytes( - /// await secretKeyToBeShared.exportRawKey(), - /// label: 'shared-key-exchange', - /// ); - /// // Bob sends Alice: encryptedRawKey - /// - /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. - /// final sharedRawSecretKey = await keypair.privateKey.decryptBytes( - /// encryptedRawKey, - /// label: 'shared-key-exchange', - /// ); - /// final sharedSecretKey = await AesGcmSecretKey.importRaw( - /// sharedRawSecretKey, - /// ); - /// // Now both Alice and Bob share a secret key. + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// // Alice sends keyPair.publicKey to Bob + /// + /// // Bob can generate a 256 bit symmetric secret key + /// final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256); + /// + /// // Using the public key Bob can encrypt secretKeyToBeShared, such that it + /// // can only be decrypted with the private key. + /// final encryptedRawKey = await keyPair.publicKey.encryptBytes( + /// await secretKeyToBeShared.exportRawKey(), + /// label: utf8.encode('shared-key-exchange'), + /// ); + /// // Bob sends Alice: encryptedRawKey + /// + /// // Given privateKey and encryptedRawKey Alice can decrypt the shared key. + /// final sharedRawSecretKey = await keyPair.privateKey.decryptBytes( + /// encryptedRawKey, + /// label: utf8.encode('shared-key-exchange'), + /// ); + /// final sharedSecretKey = await AesGcmSecretKey.importRawKey( + /// sharedRawSecretKey, + /// ); + /// // Now both Alice and Bob share a secret key. + /// } /// ``` /// /// [1]: https://www.shoup.net/papers/iso-2_1.pdf @@ -535,20 +555,22 @@ final class RsaOaepPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// - /// // Public keys are often encoded as PEM. - /// // This encode the key in base64 and wraps it with: - /// // '-----BEGIN PUBLIC KEY-----'... - /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// + /// // Public keys are often encoded as PEM. + /// // This encode the key in base64 and wraps it with: + /// // '-----BEGIN PUBLIC KEY-----'... + /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -563,20 +585,22 @@ final class RsaOaepPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsaOaepPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final jwk = await keypair.publicKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "RSA-OAEP-256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaOaepPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final jwk = await keyPair.publicKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "RSA-OAEP-256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.rsapss.dart b/lib/src/webcrypto/webcrypto.rsapss.dart index 5fa3bc9c..1a9acd33 100644 --- a/lib/src/webcrypto/webcrypto.rsapss.dart +++ b/lib/src/webcrypto/webcrypto.rsapss.dart @@ -32,31 +32,33 @@ part of 'webcrypto.dart'; /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// -/// // Generate a key-pair. -/// final keyPair = await RsaPssPrivateKey.generateKey( -/// 4096, -/// BigInt.from(65537), -/// Hash.sha256, -/// ); +/// Future main() async { +/// // Generate a key-pair. +/// final keyPair = await RsaPssPrivateKey.generateKey( +/// 4096, +/// BigInt.from(65537), +/// Hash.sha256, +/// ); /// -/// // Use the same saltLength for signing and verifying -/// const saltLength = 256 / 8; +/// // Use the same saltLength for signing and verifying +/// const saltLength = 256 / 8; /// -/// // Using privateKey Bob can sign a message for Alice. -/// final message = 'Hi Alice'; -/// final signature = await keyPair.privateKey.signBytes( -/// utf8.encode(message), -/// saltLength, -/// ); +/// // Using privateKey Bob can sign a message for Alice. +/// final message = 'Hi Alice'; +/// final signature = await keyPair.privateKey.signBytes( +/// utf8.encode(message), +/// saltLength.round(), +/// ); /// -/// // Given publicKey and signature Alice can verify the message from Bob. -/// final isValid = await keypair.publicKey.verifyBytes( -/// signature, -/// utf8.encode(message), -/// saltLength, -/// ); -/// if (isValid) { -/// print('Authentic message from Bob: $message'); +/// // Given publicKey and signature Alice can verify the message from Bob. +/// final isValid = await keyPair.publicKey.verifyBytes( +/// signature, +/// utf8.encode(message), +/// saltLength.round(), +/// ); +/// if (isValid) { +/// print('Authentic message from Bob: \$message'); +/// } /// } /// ``` /// {@endtemplate} @@ -86,23 +88,25 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privateKey = await RsaPssPrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await privateKey.exportPkcs8Key(); - /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsaPssPrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await privateKey.exportPkcs8Key(); + /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5208 @@ -140,18 +144,20 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "PS256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "PS256", "n": "...", "e": "AQAB", "d": "...", "p": "...", "q": "...", "dp": "...", "dq": "...", "qi": "..."}'; /// - /// // Import private key from decoded JSON. - /// final privateKey = await RsaPssPrivateKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import private key from decoded JSON. + /// final privateKey = await RsaPssPrivateKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await privateKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await privateKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@macro RSA-importJsonWebKey:use-key_ops} @@ -178,42 +184,44 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export public, so Alice can use it later. - /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); - /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... - /// - /// // Use the same saltLength for signing and verifying - /// const saltLength = 256 / 8; - /// - /// // Sign a message for Alice. - /// final message = 'Hi Alice'; - /// final signature = await keyPair.privateKey.signBytes( - /// utf8.encode(message), - /// saltLength, - /// ); - /// - /// // On the other side of the world, Alice has written down the pemPublicKey - /// // on a trusted piece of paper, but receives the message and signature - /// // from an untrusted source (thus, desires to verify the signature). - /// final publicKey = await RsaPssPublicKey.importSpkiKey( - /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), - /// Hash.sha256, - /// ); - /// final isValid = await publicKey.verifyBytes( - /// signature, - /// utf8.encode(message), - /// saltLength, - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export public, so Alice can use it later. + /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); + /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... + /// + /// // Use the same saltLength for signing and verifying + /// const saltLength = 256 / 8; + /// + /// // Sign a message for Alice. + /// final message = 'Hi Alice'; + /// final signature = await keyPair.privateKey.signBytes( + /// utf8.encode(message), + /// saltLength.round(), + /// ); + /// + /// // On the other side of the world, Alice has written down the pemPublicKey + /// // on a trusted piece of paper, but receives the message and signature + /// // from an untrusted source (thus, desires to verify the signature). + /// final publicKey = await RsaPssPublicKey.importSpkiKey( + /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), + /// Hash.sha256, + /// ); + /// final isValid = await publicKey.verifyBytes( + /// signature, + /// utf8.encode(message), + /// saltLength.round(), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` static Future> generateKey( @@ -251,33 +259,35 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read prviate key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privatKey = await RsaPssPrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Use the same salt for signing and verifying. - /// // In this case we're using the length of the hash function, divided by 8 - /// // to as saltLength must be specified in bytes. - /// const saltLength = 256 / 8; - /// - /// // Create a signature for UTF-8 encoded message - /// final message = 'hello world'; - /// final signature = await privateKey.signBytes( - /// utf8.encode(message), - /// saltLength, - /// ); - /// - /// print('signature: ${base64.encode(signature)}'); + /// Future main() async { + /// // Read prviate key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsaPssPrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Use the same salt for signing and verifying. + /// // In this case we're using the length of the hash function, divided by 8 + /// // to as saltLength must be specified in bytes. + /// const saltLength = 256 / 8; + /// + /// // Create a signature for UTF-8 encoded message + /// final message = 'hello world'; + /// final signature = await privateKey.signBytes( + /// utf8.encode(message), + /// saltLength.round(), + /// ); + /// + /// print('signature: \${base64.encode(signature)}'); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc3447#section-9.1 @@ -326,32 +336,34 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read prviate key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privatKey = await RsaPssPrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Use the same salt for signing and verifying. - /// // In this case we're using the length of the hash function, divided by 8 - /// // to as saltLength must be specified in bytes. - /// const saltLength = 256 / 8; - /// - /// // Create a signature for message read directly from file. - /// final signature = await privateKey.signStream( - /// File('message.txt').openRead(), - /// saltLength, - /// ); - /// - /// print('signature: ${base64.encode(signature)}'); + /// Future main() async { + /// // Read private key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsaPssPrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Use the same salt for signing and verifying. + /// // In this case we're using the length of the hash function, divided by 8 + /// // to as saltLength must be specified in bytes. + /// const saltLength = 256 / 8; + /// + /// // Create a signature for message read directly from file. + /// final signature = await privateKey.signStream( + /// File('message.txt').openRead(), + /// saltLength.round(), + /// ); + /// + /// print('signature: \${base64.encode(signature)}'); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc3447#section-9.1 @@ -397,20 +409,22 @@ final class RsaPssPrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the private key. - /// final jwk = await keypair.privateKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "PS256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the private key. + /// final jwk = await keyPair.privateKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "PS256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -456,23 +470,25 @@ final class RsaPssPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.publicKey).decode(""" - /// -----BEGIN PUBLIC KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PUBLIC KEY----- - /// """); - /// - /// // Import public key from binary PEM decoded data. - /// final publicKey = await RsaPssPublicKey.importSpkiKey( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await publicKey.exportSpkiKey(); - /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.publicKey).decode(""" + /// -----BEGIN PUBLIC KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PUBLIC KEY----- + /// """); + /// + /// // Import public key from binary PEM decoded data. + /// final publicKey = await RsaPssPublicKey.importSpkiKey( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await publicKey.exportSpkiKey(); + /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -504,18 +520,20 @@ final class RsaPssPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "PS256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "PS256", "n": "...", "e": "AQAB"}'; /// - /// // Import public key from decoded JSON. - /// final publicKey = await RsaPssPublicKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import public key from decoded JSON. + /// final publicKey = await RsaPssPublicKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await publicKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await publicKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@macro RSA-importJsonWebKey:use-key_ops} @@ -546,31 +564,33 @@ final class RsaPssPublicKey { /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Use the same salt for signing and verifying. - /// const saltLength = 256 / 8; - /// - /// // Using privateKey Bob can sign a message for Alice. - /// final message = 'Hi Alice'; - /// final signature = await keyPair.privateKey.signBytes( - /// utf8.encode(message), - /// saltLength, - /// ); - /// - /// // Given publicKey and signature Alice can verify the message from Bob. - /// final isValid = await keypair.publicKey.verifyBytes( - /// signature, - /// utf8.encode(message), - /// saltLength, - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Use the same salt for signing and verifying. + /// const saltLength = 256 / 8; + /// + /// // Using privateKey Bob can sign a message for Alice. + /// final message = 'Hi Alice'; + /// final signature = await keyPair.privateKey.signBytes( + /// utf8.encode(message), + /// saltLength.round(), + /// ); + /// + /// // Given publicKey and signature Alice can verify the message from Bob. + /// final isValid = await keyPair.publicKey.verifyBytes( + /// signature, + /// utf8.encode(message), + /// saltLength.round(), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` Future verifyBytes( @@ -593,30 +613,32 @@ final class RsaPssPublicKey { /// import 'dart:io' show File; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Use the same salt for signing and verifying. - /// const saltLength = 256 / 8; - /// - /// // Using privateKey Bob can sign a message for Alice. - /// final signature = await keyPair.privateKey.signStream( - /// File('message.txt').openRead(), // read directly from file - /// saltLength, - /// ); - /// - /// // Given publicKey and signature Alice can verify the message from Bob. - /// final isValid = await keypair.publicKey.verifyStream( - /// signature, - /// File('message.txt').openRead(), // read directly from file - /// saltLength, - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Use the same salt for signing and verifying. + /// const saltLength = 256 / 8; + /// + /// // Using privateKey Bob can sign a message for Alice. + /// final signature = await keyPair.privateKey.signStream( + /// File('message.txt').openRead(), // read directly from file + /// saltLength.round(), + /// ); + /// + /// // Given publicKey and signature Alice can verify the message from Bob. + /// final isValid = await keyPair.publicKey.verifyStream( + /// signature, + /// File('message.txt').openRead(), // read directly from file + /// saltLength.round(), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` Future verifyStream( @@ -635,20 +657,22 @@ final class RsaPssPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// - /// // Public keys are often encoded as PEM. - /// // This encode the key in base64 and wraps it with: - /// // '-----BEGIN PUBLIC KEY-----'... - /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// + /// // Public keys are often encoded as PEM. + /// // This encode the key in base64 and wraps it with: + /// // '-----BEGIN PUBLIC KEY-----'... + /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -663,20 +687,22 @@ final class RsaPssPublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsaPssPrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final jwk = await keypair.publicKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "PS256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsaPssPrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final jwk = await keyPair.publicKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "PS256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 diff --git a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart index 6002b05d..03dbbb1d 100644 --- a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart +++ b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart @@ -33,24 +33,26 @@ part of 'webcrypto.dart'; /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// -/// // Generate a key-pair. -/// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( -/// 4096, -/// BigInt.from(65537), -/// Hash.sha256, -/// ); +/// Future main() async { +/// // Generate a key-pair. +/// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( +/// 4096, +/// BigInt.from(65537), +/// Hash.sha256, +/// ); /// -/// // Using privateKey Bob can sign a message for Alice. -/// final message = 'Hi Alice'; -/// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); +/// // Using privateKey Bob can sign a message for Alice. +/// final message = 'Hi Alice'; +/// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); /// -/// // Given publicKey and signature Alice can verify the message from Bob. -/// final isValid = await keypair.publicKey.verifyBytes( -/// signature, -/// utf8.encode(message), -/// ); -/// if (isValid) { -/// print('Authentic message from Bob: $message'); +/// // Given publicKey and signature Alice can verify the message from Bob. +/// final isValid = await keyPair.publicKey.verifyBytes( +/// signature, +/// utf8.encode(message), +/// ); +/// if (isValid) { +/// print('Authentic message from Bob: \$message'); +/// } /// } /// ``` /// {@endtemplate} @@ -82,23 +84,25 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privateKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await privateKey.exportPkcs8Key(); - /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await privateKey.exportPkcs8Key(); + /// print(PemCodec(PemLabel.privateKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5208 @@ -142,18 +146,20 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "RS256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "RS256", "n": "...", "e": "AQAB", "d": "...", "p": "...", "q": "...", "dp": "...", "dq": "...", "qi": "..."}'; /// - /// // Import private key from decoded JSON. - /// final privateKey = await RsassaPkcs1V15PrivateKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import private key from decoded JSON. + /// final privateKey = await RsassaPkcs1V15PrivateKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await privateKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await privateKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@template RSA-importJsonWebKey:use-key_ops} @@ -214,37 +220,39 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export public, so Alice can use it later. - /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); - /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... - /// - /// // Sign a message for Alice. - /// final message = 'Hi Alice'; - /// final signature = await keyPair.privateKey.signBytes( - /// utf8.encode(message), - /// ); - /// - /// // On the other side of the world, Alice has written down the pemPublicKey - /// // on a trusted piece of paper, but receives the message and signature - /// // from an untrusted source (thus, desires to verify the signature). - /// final publicKey = await RsassaPkcs1V15PublicKey.importSpkiKey( - /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), - /// Hash.sha256, - /// ); - /// final isValid = await publicKey.verifyBytes( - /// signature, - /// utf8.encode(message), - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export public, so Alice can use it later. + /// final spkiPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// final pemPublicKey = PemCodec(PemLabel.publicKey).encode(spkiPublicKey); + /// print(pemPublicKey); // print key in PEM format: -----BEGIN PUBLIC KEY.... + /// + /// // Sign a message for Alice. + /// final message = 'Hi Alice'; + /// final signature = await keyPair.privateKey.signBytes( + /// utf8.encode(message), + /// ); + /// + /// // On the other side of the world, Alice has written down the pemPublicKey + /// // on a trusted piece of paper, but receives the message and signature + /// // from an untrusted source (thus, desires to verify the signature). + /// final publicKey = await RsassaPkcs1V15PublicKey.importSpkiKey( + /// PemCodec(PemLabel.publicKey).decode(pemPublicKey), + /// Hash.sha256, + /// ); + /// final isValid = await publicKey.verifyBytes( + /// signature, + /// utf8.encode(message), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` static Future> @@ -270,25 +278,27 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read prviate key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privatKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Create a signature for UTF-8 encoded message - /// final message = 'hello world'; - /// final signature = await privateKey.signBytes(utf8.encode(message)); - /// - /// print('signature: ${base64.encode(signature)}'); + /// Future main() async { + /// // Read prviate key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Create a signature for UTF-8 encoded message + /// final message = 'hello world'; + /// final signature = await privateKey.signBytes(utf8.encode(message)); + /// + /// print('signature: \${base64.encode(signature)}'); + /// } /// ``` Future signBytes(List data) => _impl.signBytes(data); @@ -303,27 +313,29 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read prviate key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.privateKey).decode(""" - /// -----BEGIN PRIVATE KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PRIVATE KEY----- - /// """); - /// - /// // Import private key from binary PEM decoded data. - /// final privatKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Create a signature for UTF-8 encoded message - /// final message = 'hello world'; - /// final signature = await privateKey.signStream(Stream.fromIterable([ - /// utf8.encode(message), - /// ])); - /// - /// print('signature: ${base64.encode(signature)}'); + /// Future main() async { + /// // Read prviate key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.privateKey).decode(""" + /// -----BEGIN PRIVATE KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PRIVATE KEY----- + /// """); + /// + /// // Import private key from binary PEM decoded data. + /// final privateKey = await RsassaPkcs1V15PrivateKey.importPkcs8Key( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Create a signature for UTF-8 encoded message + /// final message = 'hello world'; + /// final signature = await privateKey.signStream(Stream.fromIterable([ + /// utf8.encode(message), + /// ])); + /// + /// print('signature: \${base64.encode(signature)}'); + /// } /// ``` Future signStream(Stream> data) => _impl.signStream(data); @@ -338,20 +350,22 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the private key. - /// final rawPrivateKey = await keypair.privateKey.exportPkcs8Key(); - /// - /// // Private keys are often encoded as PEM. - /// // This encodes the key in base64 and wraps it with: - /// // '-----BEGIN PRIVATE KEY----'... - /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the private key. + /// final rawPrivateKey = await keyPair.privateKey.exportPkcs8Key(); + /// + /// // Private keys are often encoded as PEM. + /// // This encodes the key in base64 and wraps it with: + /// // '-----BEGIN PRIVATE KEY----'... + /// print(PemCodec(PemLabel.privateKey).encode(rawPrivateKey)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5208 @@ -369,20 +383,22 @@ final class RsassaPkcs1V15PrivateKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the private key. - /// final jwk = await keypair.privateKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "RS256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the private key. + /// final jwk = await keyPair.privateKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "RS256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 @@ -430,23 +446,25 @@ final class RsassaPkcs1V15PublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Read key data from PEM encoded block. This will remove the - /// // '----BEGIN...' padding, decode base64 and return encoded bytes. - /// List keyData = PemCodec(PemLabel.publicKey).decode(""" - /// -----BEGIN PUBLIC KEY----- - /// MIGEAgEAMBAGByqG... - /// -----END PUBLIC KEY----- - /// """); - /// - /// // Import public key from binary PEM decoded data. - /// final publicKey = await RsassaPkcs1V15PublicKey.importSpkiKey( - /// keyData, - /// Hash.sha256, - /// ); - /// - /// // Export the key again (print it in same format as it was given). - /// List rawKeyData = await publicKey.exportSpkiKey(); - /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// Future main() async { + /// // Read key data from PEM encoded block. This will remove the + /// // '----BEGIN...' padding, decode base64 and return encoded bytes. + /// List keyData = PemCodec(PemLabel.publicKey).decode(""" + /// -----BEGIN PUBLIC KEY----- + /// MIGEAgEAMBAGByqG... + /// -----END PUBLIC KEY----- + /// """); + /// + /// // Import public key from binary PEM decoded data. + /// final publicKey = await RsassaPkcs1V15PublicKey.importSpkiKey( + /// keyData, + /// Hash.sha256, + /// ); + /// + /// // Export the key again (print it in same format as it was given). + /// List rawKeyData = await publicKey.exportSpkiKey(); + /// print(PemCodec(PemLabel.publicKey).encode(rawKeyData)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -478,18 +496,20 @@ final class RsassaPkcs1V15PublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode, jsonDecode; /// - /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "RSA", "alg": "RS256", ...}'; + /// Future main() async { + /// // JSON Web Key as a string containing JSON. + /// final jwk = '{"kty": "RSA", "alg": "RS256", "n": "...", "e": "AQAB"}'; /// - /// // Import public key from decoded JSON. - /// final publicKey = await RsassaPkcs1V15PublicKey.importJsonWebKey( - /// jsonDecode(jwk), - /// Hash.sha256, // Must match the hash used the JWK key "alg" - /// ); + /// // Import public key from decoded JSON. + /// final publicKey = await RsassaPkcs1V15PublicKey.importJsonWebKey( + /// jsonDecode(jwk), + /// Hash.sha256, // Must match the hash used the JWK key "alg" + /// ); /// - /// // Export the key (print it in same format as it was given). - /// Map keyData = await publicKey.exportJsonWebKey(); - /// print(jsonEncode(keyData)); + /// // Export the key (print it in same format as it was given). + /// Map keyData = await publicKey.exportJsonWebKey(); + /// print(jsonEncode(keyData)); + /// } /// ``` /// /// {@macro RSA-importJsonWebKey:use-key_ops} @@ -517,24 +537,26 @@ final class RsassaPkcs1V15PublicKey { /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Using privateKey Bob can sign a message for Alice. - /// final message = 'Hi Alice'; - /// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); - /// - /// // Given publicKey and signature Alice can verify the message from Bob. - /// final isValid = await keypair.publicKey.verifyBytes( - /// signature, - /// utf8.encode(message), - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Using privateKey Bob can sign a message for Alice. + /// final message = 'Hi Alice'; + /// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); + /// + /// // Given publicKey and signature Alice can verify the message from Bob. + /// final isValid = await keyPair.publicKey.verifyBytes( + /// signature, + /// utf8.encode(message), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` Future verifyBytes(List signature, List data) => @@ -551,24 +573,26 @@ final class RsassaPkcs1V15PublicKey { /// import 'dart:convert' show utf8; /// import 'package:webcrypto/webcrypto.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Using privateKey Bob can sign a message for Alice. - /// final message = 'Hi Alice'; - /// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); - /// - /// // Given publicKey and signature Alice can verify the message from Bob. - /// final isValid = await keypair.publicKey.verifyStream( - /// signature, - /// Stream.fromIterable([utf8.encode(message)]), - /// ); - /// if (isValid) { - /// print('Authentic message from Bob: $message'); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Using privateKey Bob can sign a message for Alice. + /// final message = 'Hi Alice'; + /// final signature = await keyPair.privateKey.signBytes(utf8.encode(message)); + /// + /// // Given publicKey and signature Alice can verify the message from Bob. + /// final isValid = await keyPair.publicKey.verifyStream( + /// signature, + /// Stream.fromIterable([utf8.encode(message)]), + /// ); + /// if (isValid) { + /// print('Authentic message from Bob: \$message'); + /// } /// } /// ``` Future verifyStream(List signature, Stream> data) => @@ -584,20 +608,22 @@ final class RsassaPkcs1V15PublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'package:pem/pem.dart'; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); - /// - /// // Public keys are often encoded as PEM. - /// // This encode the key in base64 and wraps it with: - /// // '-----BEGIN PUBLIC KEY-----'... - /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final rawPublicKey = await keyPair.publicKey.exportSpkiKey(); + /// + /// // Public keys are often encoded as PEM. + /// // This encode the key in base64 and wraps it with: + /// // '-----BEGIN PUBLIC KEY-----'... + /// print(PemCodec(PemLabel.publicKey).encode(rawPublicKey)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc5280 @@ -612,20 +638,22 @@ final class RsassaPkcs1V15PublicKey { /// import 'package:webcrypto/webcrypto.dart'; /// import 'dart:convert' show jsonEncode; /// - /// // Generate a key-pair. - /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( - /// 4096, - /// BigInt.from(65537), - /// Hash.sha256, - /// ); - /// - /// // Export the public key. - /// final jwk = await keypair.publicKey.exportJsonWebKey(); - /// - /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with - /// // `jsonEncode` from `dart:convert`, this will print something like: - /// // {"kty": "RSA", "alg": "RS256", ...} - /// print(jsonEncode(jwk)); + /// Future main() async { + /// // Generate a key-pair. + /// final keyPair = await RsassaPkcs1V15PrivateKey.generateKey( + /// 4096, + /// BigInt.from(65537), + /// Hash.sha256, + /// ); + /// + /// // Export the public key. + /// final jwk = await keyPair.publicKey.exportJsonWebKey(); + /// + /// // The Map returned by `exportJsonWebKey()` can be converted to JSON with + /// // `jsonEncode` from `dart:convert`, this will print something like: + /// // {"kty": "RSA", "alg": "RS256", ...} + /// print(jsonEncode(jwk)); + /// } /// ``` /// /// [1]: https://www.rfc-editor.org/rfc/rfc7517 From 2b34a85b2430bcce487e028d51df80bcab85531b Mon Sep 17 00:00:00 2001 From: YATIN JAMWAL Date: Sat, 24 Jan 2026 19:34:00 +0530 Subject: [PATCH 3/4] Hide the key for JSON Web Key in webcrypto.aesctr.dart --- lib/src/webcrypto/webcrypto.aesctr.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/webcrypto/webcrypto.aesctr.dart b/lib/src/webcrypto/webcrypto.aesctr.dart index cb3628c7..9a159eb9 100644 --- a/lib/src/webcrypto/webcrypto.aesctr.dart +++ b/lib/src/webcrypto/webcrypto.aesctr.dart @@ -96,7 +96,7 @@ final class AesCtrSecretKey { /// /// Future main() async { /// // JSON Web Key as a string containing JSON. - /// final jwk = '{"kty": "oct", "alg": "A256CTR", "k": "Y0ztPO2iDca0H0iM6y0_s0ztPO2iDca0H0iM6y0_s0w"}'; + /// final jwk = '{"kty": "oct", "alg": "A256CTR", "k": "..."}'; /// /// // Import secret key from decoded JSON. /// final key = await AesCtrSecretKey.importJsonWebKey(jsonDecode(jwk)); From fbf03f74a9ab366da097158f693125103b51cb73 Mon Sep 17 00:00:00 2001 From: YATIN JAMWAL Date: Sat, 24 Jan 2026 19:49:09 +0530 Subject: [PATCH 4/4] Replace sample private key with placeholder text Updated the private key placeholder in comments. --- lib/src/webcrypto/webcrypto.ecdh.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/src/webcrypto/webcrypto.ecdh.dart b/lib/src/webcrypto/webcrypto.ecdh.dart index c7573f46..86b07f5b 100644 --- a/lib/src/webcrypto/webcrypto.ecdh.dart +++ b/lib/src/webcrypto/webcrypto.ecdh.dart @@ -76,9 +76,7 @@ final class EcdhPrivateKey { /// // Sample P-256 private key. /// List keyData = PemCodec(PemLabel.privateKey).decode(''' /// -----BEGIN PRIVATE KEY----- - /// MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYY/bM/GkKkXyT+rM - /// HuX+aC2s7n+L/F9X7M2I2/9Q8nehRANCAAQW7urj+lFz0F7cK5n8+C9q6v/9/6+1 - /// 9/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+19/6+0= + /// /// -----END PRIVATE KEY----- /// '''); ///