allowed for mock testing

This commit is contained in:
Adrian Cole 2012-06-16 16:33:48 -04:00
parent db8f869f5b
commit 5dd4f04ddf
1 changed files with 7 additions and 6 deletions

View File

@ -124,13 +124,14 @@ public class SshKeys {
/** /**
* *
* @param used * @param generator
* to generate RSA key pairs * to generate RSA key pairs
* @param rand
* for initializing {@code generator}
* @return new 2048 bit keyPair * @return new 2048 bit keyPair
* @see Crypto#rsaKeyPairGenerator() * @see Crypto#rsaKeyPairGenerator()
*/ */
public static KeyPair generateRsaKeyPair(KeyPairGenerator generator) { public static KeyPair generateRsaKeyPair(KeyPairGenerator generator, SecureRandom rand) {
SecureRandom rand = new SecureRandom();
generator.initialize(2048, rand); generator.initialize(2048, rand);
return generator.genKeyPair(); return generator.genKeyPair();
} }
@ -140,15 +141,15 @@ public class SshKeys {
*/ */
public static Map<String, String> generate() { public static Map<String, String> generate() {
try { try {
return generate(KeyPairGenerator.getInstance("RSA")); return generate(KeyPairGenerator.getInstance("RSA"), new SecureRandom());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
propagate(e); propagate(e);
return null; return null;
} }
} }
public static Map<String, String> generate(KeyPairGenerator generator) { public static Map<String, String> generate(KeyPairGenerator generator, SecureRandom rand) {
KeyPair pair = generateRsaKeyPair(generator); KeyPair pair = generateRsaKeyPair(generator, rand);
Builder<String, String> builder = ImmutableMap.builder(); Builder<String, String> builder = ImmutableMap.builder();
builder.put("public", encodeAsOpenSSH(RSAPublicKey.class.cast(pair.getPublic()))); builder.put("public", encodeAsOpenSSH(RSAPublicKey.class.cast(pair.getPublic())));
builder.put("private", encodeAsPem(RSAPrivateKey.class.cast(pair.getPrivate()))); builder.put("private", encodeAsPem(RSAPrivateKey.class.cast(pair.getPrivate())));