@@ -261,9 +261,9 @@ class CryptoUtils {
261
261
}
262
262
263
263
///
264
- /// Enode the given [publicKey] to PEM format using the PKCS#8 standard.
264
+ /// Enode the given [publicKey] to DER bytes using the PKCS#8 standard.
265
265
///
266
- static String encodeRSAPublicKeyToPem (RSAPublicKey publicKey) {
266
+ static Uint8List encodeRSAPublicKeyToDERBytes (RSAPublicKey publicKey) {
267
267
var algorithmSeq = ASN1Sequence ();
268
268
var paramsAsn1Obj = ASN1Object .fromBytes (Uint8List .fromList ([0x5 , 0x0 ]));
269
269
algorithmSeq.add (ASN1ObjectIdentifier .fromName ('rsaEncryption' ));
@@ -278,7 +278,15 @@ class CryptoUtils {
278
278
var topLevelSeq = ASN1Sequence ();
279
279
topLevelSeq.add (algorithmSeq);
280
280
topLevelSeq.add (publicKeySeqBitString);
281
- var dataBase64 = base64.encode (topLevelSeq.encode ());
281
+
282
+ return topLevelSeq.encode ();
283
+ }
284
+
285
+ ///
286
+ /// Enode the given [publicKey] to PEM format using the PKCS#8 standard.
287
+ ///
288
+ static String encodeRSAPublicKeyToPem (RSAPublicKey publicKey) {
289
+ var dataBase64 = base64.encode (encodeRSAPublicKeyToDERBytes (publicKey));
282
290
var chunks = StringUtils .chunk (dataBase64, 64 );
283
291
284
292
return '$BEGIN_PUBLIC_KEY \n ${chunks .join ('\n ' )}\n $END_PUBLIC_KEY ' ;
@@ -359,7 +367,7 @@ class CryptoUtils {
359
367
}
360
368
361
369
///
362
- /// Enode the given [rsaPrivateKey] to PEM format using the PKCS#8 standard.
370
+ /// Enode the given [rsaPrivateKey] to DER Bytes using the PKCS#8 standard.
363
371
///
364
372
/// The ASN1 structure is decripted at <https://tools.ietf.org/html/rfc5208>.
365
373
/// ```
@@ -370,7 +378,7 @@ class CryptoUtils {
370
378
/// }
371
379
/// ```
372
380
///
373
- static String encodeRSAPrivateKeyToPem (RSAPrivateKey rsaPrivateKey) {
381
+ static Uint8List encodeRSAPrivateKeyToDERBytes (RSAPrivateKey rsaPrivateKey) {
374
382
var version = ASN1Integer (BigInt .from (0 ));
375
383
376
384
var algorithmSeq = ASN1Sequence ();
@@ -411,7 +419,25 @@ class CryptoUtils {
411
419
topLevelSeq.add (version);
412
420
topLevelSeq.add (algorithmSeq);
413
421
topLevelSeq.add (publicKeySeqOctetString);
414
- var dataBase64 = base64.encode (topLevelSeq.encode ());
422
+
423
+ return topLevelSeq.encode ();
424
+ }
425
+
426
+ ///
427
+ /// Enode the given [rsaPrivateKey] to PEM format using the PKCS#8 standard.
428
+ ///
429
+ /// The ASN1 structure is decripted at <https://tools.ietf.org/html/rfc5208>.
430
+ /// ```
431
+ /// PrivateKeyInfo ::= SEQUENCE {
432
+ /// version Version,
433
+ /// algorithm AlgorithmIdentifier,
434
+ /// PrivateKey BIT STRING
435
+ /// }
436
+ /// ```
437
+ ///
438
+ static String encodeRSAPrivateKeyToPem (RSAPrivateKey rsaPrivateKey) {
439
+ var dataBase64 =
440
+ base64.encode (encodeRSAPrivateKeyToDERBytes (rsaPrivateKey));
415
441
var chunks = StringUtils .chunk (dataBase64, 64 );
416
442
return '$BEGIN_PRIVATE_KEY \n ${chunks .join ('\n ' )}\n $END_PRIVATE_KEY ' ;
417
443
}
@@ -1179,8 +1205,10 @@ class CryptoUtils {
1179
1205
///
1180
1206
static String encodePrivateEcdsaKeyToPkcs8 (ECPrivateKey privateKey) {
1181
1207
final privateKeyPem = CryptoUtils .encodeEcPrivateKeyToPem (privateKey);
1182
- final base64Encoded = base64.encode (ASN1PrivateKeyInfo .fromEccPem (privateKeyPem).encode ());
1183
- final base64Formatted = base64Encoded.replaceAllMapped (RegExp (r".{64}" ), (match) => "${match .group (0 )}\n " );
1208
+ final base64Encoded =
1209
+ base64.encode (ASN1PrivateKeyInfo .fromEccPem (privateKeyPem).encode ());
1210
+ final base64Formatted = base64Encoded.replaceAllMapped (
1211
+ RegExp (r".{64}" ), (match) => "${match .group (0 )}\n " );
1184
1212
1185
1213
return '$BEGIN_PRIVATE_KEY \n $base64Formatted \n $END_PRIVATE_KEY ' ;
1186
1214
}
0 commit comments