diff --git a/lib/rsa_encrypt.dart b/lib/rsa_encrypt.dart index 05303d7..b0d6b06 100644 --- a/lib/rsa_encrypt.dart +++ b/lib/rsa_encrypt.dart @@ -14,9 +14,33 @@ class RsaKeyHelper { /// /// Returns a [AsymmetricKeyPair] based on the [RSAKeyGenerator] with custom parameters, /// including a [SecureRandom] + /// + /// The parameter [secureRandomAndBitStrength] is an array of length 2 that + /// uses a [SecureRandom] as first value and an [int] as second value for the BitStrength + /// + /// Recommended BitStrength [1024, 2048 or 4096] - default BitStrength [2048] + static AsymmetricKeyPair getRsaKeyPair( + List secureRandomAndBitStrength) { + var rsapars = new RSAKeyGeneratorParameters( + BigInt.from(65537), secureRandomAndBitStrength.last, 5); + var params = + new ParametersWithRandom(rsapars, secureRandomAndBitStrength.first); + var keyGenerator = new RSAKeyGenerator(); + keyGenerator.init(params); + return keyGenerator.generateKeyPair(); + } + + /// Generate a [PublicKey] and [PrivateKey] pair + /// + /// Returns a [AsymmetricKeyPair] based on the [RSAKeyGenerator] with custom parameters, + /// including a [SecureRandom] + /// + /// Accepts a custom [int] as BitStrength. Recommended BitStrength [1024, 2048 or 4096] + /// - default BitStrength [2048] Future> computeRSAKeyPair( - SecureRandom secureRandom) async { - return await compute(getRsaKeyPair, secureRandom); + SecureRandom secureRandom, + {int bitStrength = 2048}) async { + return await compute(getRsaKeyPair, [secureRandom, bitStrength]); } /// Generates a [SecureRandom] to use in computing RSA key pair @@ -247,17 +271,3 @@ String decrypt(String ciphertext, RSAPrivateKey privateKey) { return new String.fromCharCodes(decrypted); } - -/// Generate a [PublicKey] and [PrivateKey] pair -/// -/// Returns a [AsymmetricKeyPair] based on the [RSAKeyGenerator] with custom parameters, -/// including a [SecureRandom] -AsymmetricKeyPair getRsaKeyPair( - SecureRandom secureRandom) { - /// Set BitStrength to [1024, 2048 or 4096] - var rsapars = new RSAKeyGeneratorParameters(BigInt.from(65537), 2048, 5); - var params = new ParametersWithRandom(rsapars, secureRandom); - var keyGenerator = new RSAKeyGenerator(); - keyGenerator.init(params); - return keyGenerator.generateKeyPair(); -}