@@ -12,9 +12,9 @@ class RsaKeyHelper {
1212 ///
1313 /// Returns a [AsymmetricKeyPair] based on the [RSAKeyGenerator] with custom parameters,
1414 /// including a [SecureRandom]
15- Future <AsymmetricKeyPair <PublicKey , PrivateKey >> getRSAKeyPair (
15+ Future <AsymmetricKeyPair <PublicKey , PrivateKey >> computeRSAKeyPair (
1616 SecureRandom secureRandom) async {
17- return await compute (computeRSAKeyPair , secureRandom);
17+ return await compute (getRsaKeyPair , secureRandom);
1818 }
1919
2020 /// Generates a [SecureRandom]
@@ -77,6 +77,23 @@ class RsaKeyHelper {
7777 return rsaPublicKey;
7878 }
7979
80+ /// Sign plain text with Private Key
81+ ///
82+ /// Given a plain text [String] and a [RSAPrivateKey] , decrypt the text using
83+ /// a [RSAEngine] cipher
84+ String sign (String plainText, RSAPrivateKey privateKey) {
85+ var signer = RSASigner (SHA256Digest (), "0609608648016503040201" );
86+ signer.init (true , PrivateKeyParameter <RSAPrivateKey >(privateKey));
87+ return base64Encode (signer.generateSignature (createUint8ListFromString (plainText)).bytes);
88+ }
89+
90+
91+ /// Creates a [Uint8List] from a string to be signed
92+ Uint8List createUint8ListFromString (String s) {
93+ var codec = Utf8Codec (allowMalformed: true );
94+ return Uint8List .fromList (codec.encode (s));
95+ }
96+
8097 /// Decode Private key from PEM Format
8198 ///
8299 /// Given a base64 encoded PEM [String] with correct headers and footers, return a
@@ -172,7 +189,7 @@ class RsaKeyHelper {
172189
173190 var version = ASN1Integer (BigInt .from (0 ));
174191 var modulus = ASN1Integer (privateKey.n);
175- var publicExponent = ASN1Integer (BigInt . parse ( '65537' ) );
192+ var publicExponent = ASN1Integer (privateKey.exponent );
176193 var privateExponent = ASN1Integer (privateKey.d);
177194 var p = ASN1Integer (privateKey.p);
178195 var q = ASN1Integer (privateKey.q);
@@ -198,79 +215,6 @@ class RsaKeyHelper {
198215 return """-----BEGIN PRIVATE KEY-----\r\n $dataBase64 \r\n -----END PRIVATE KEY-----""" ;
199216 }
200217
201- /// Encode Private key to PEM Format
202- ///
203- /// Given [RSAPrivateKey] returns a base64 encoded [String] with standard PEM headers and footers
204- String encodePrivateKeyToPemPKCS8 (RSAPrivateKey privateKey) {
205- var version = ASN1Integer (BigInt .from (0 ));
206-
207- var algorithmSeq = new ASN1Sequence ();
208- var algorithmAsn1Obj = new ASN1Object .fromBytes (Uint8List .fromList (
209- [0x6 , 0x9 , 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0xd , 0x1 , 0x1 , 0x1 ]));
210- var paramsAsn1Obj =
211- new ASN1Object .fromBytes (Uint8List .fromList ([0x5 , 0x0 ]));
212- algorithmSeq.add (algorithmAsn1Obj);
213- algorithmSeq.add (paramsAsn1Obj);
214-
215- var privateKeySeq = new ASN1Sequence ();
216- var modulus = ASN1Integer (privateKey.n);
217- var publicExponent = ASN1Integer (BigInt .parse ('65537' ));
218- var privateExponent = ASN1Integer (privateKey.d);
219- var p = ASN1Integer (privateKey.p);
220- var q = ASN1Integer (privateKey.q);
221- var dP = privateKey.d % (privateKey.p - BigInt .from (1 ));
222- var exp1 = ASN1Integer (dP);
223- var dQ = privateKey.d % (privateKey.q - BigInt .from (1 ));
224- var exp2 = ASN1Integer (dQ);
225- var iQ = privateKey.q.modInverse (privateKey.p);
226- var co = ASN1Integer (iQ);
227-
228- privateKeySeq.add (version);
229- privateKeySeq.add (modulus);
230- privateKeySeq.add (publicExponent);
231- privateKeySeq.add (privateExponent);
232- privateKeySeq.add (p);
233- privateKeySeq.add (q);
234- privateKeySeq.add (exp1);
235- privateKeySeq.add (exp2);
236- privateKeySeq.add (co);
237- var publicKeySeqOctetString =
238- new ASN1OctetString (Uint8List .fromList (privateKeySeq.encodedBytes));
239-
240- var topLevelSeq = new ASN1Sequence ();
241- topLevelSeq.add (version);
242- topLevelSeq.add (algorithmSeq);
243- topLevelSeq.add (publicKeySeqOctetString);
244- var dataBase64 = base64.encode (topLevelSeq.encodedBytes);
245-
246- return """-----BEGIN PRIVATE KEY-----\r\n $dataBase64 \r\n -----END PRIVATE KEY-----""" ;
247- }
248-
249- /// Encode Public key to PEM Format
250- ///
251- /// Given [RSAPublicKey] returns a base64 encoded [String] with standard PEM headers and footers
252- String encodePublicKeyToPemPKCS8 (RSAPublicKey publicKey) {
253- var algorithmSeq = new ASN1Sequence ();
254- var algorithmAsn1Obj = new ASN1Object .fromBytes (Uint8List .fromList (
255- [0x6 , 0x9 , 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0xd , 0x1 , 0x1 , 0x1 ]));
256- var paramsAsn1Obj =
257- new ASN1Object .fromBytes (Uint8List .fromList ([0x5 , 0x0 ]));
258- algorithmSeq.add (algorithmAsn1Obj);
259- algorithmSeq.add (paramsAsn1Obj);
260-
261- var publicKeySeq = new ASN1Sequence ();
262- publicKeySeq.add (ASN1Integer (publicKey.modulus));
263- publicKeySeq.add (ASN1Integer (publicKey.exponent));
264- var publicKeySeqBitString =
265- new ASN1BitString (Uint8List .fromList (publicKeySeq.encodedBytes));
266-
267- var topLevelSeq = new ASN1Sequence ();
268- topLevelSeq.add (algorithmSeq);
269- topLevelSeq.add (publicKeySeqBitString);
270- var dataBase64 = base64.encode (topLevelSeq.encodedBytes);
271- return """-----BEGIN PUBLIC KEY-----\r\n $dataBase64 \r\n -----END PUBLIC KEY-----""" ;
272- }
273-
274218 /// Encode Public key to PEM Format
275219 ///
276220 /// Given [RSAPublicKey] returns a base64 encoded [String] with standard PEM headers and footers
@@ -289,9 +233,9 @@ class RsaKeyHelper {
289233///
290234/// Returns a [AsymmetricKeyPair] based on the [RSAKeyGenerator] with custom parameters,
291235/// including a [SecureRandom]
292- AsymmetricKeyPair <PublicKey , PrivateKey > computeRSAKeyPair (
236+ AsymmetricKeyPair <PublicKey , PrivateKey > getRsaKeyPair (
293237 SecureRandom secureRandom) {
294- var rsapars = new RSAKeyGeneratorParameters (BigInt .from (65537 ), 2048 , 12 );
238+ var rsapars = new RSAKeyGeneratorParameters (BigInt .from (65537 ), 2048 , 5 );
295239 var params = new ParametersWithRandom (rsapars, secureRandom);
296240 var keyGenerator = new RSAKeyGenerator ();
297241 keyGenerator.init (params);
0 commit comments