Use the BouncyCastle provider so we can be sure we won't get

NoSuchAlgorithmExceptions. Key generation is *slow* - I guess that's a
good sign. ;-)
This commit is contained in:
akwizgran
2011-07-25 10:38:46 +01:00
parent c98c968b87
commit fb95565880
13 changed files with 126 additions and 78 deletions

View File

@@ -0,0 +1,66 @@
package net.sf.briar.crypto;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.Signature;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.KeyParser;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class CryptoComponentImpl implements CryptoComponent {
private static final String PROVIDER = "BC";
private static final String DIGEST_ALGO = "SHA-256";
private static final String KEY_PAIR_ALGO = "RSA";
private static final String SIGNATURE_ALGO = "SHA256withRSA";
private final KeyParser keyParser;
private final KeyPairGenerator keyPairGenerator;
CryptoComponentImpl() {
Security.addProvider(new BouncyCastleProvider());
try {
keyParser = new KeyParserImpl(KEY_PAIR_ALGO, PROVIDER);
keyPairGenerator = KeyPairGenerator.getInstance(KEY_PAIR_ALGO,
PROVIDER);
} catch(NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchProviderException impossible) {
throw new RuntimeException(impossible);
}
}
public KeyPair generateKeyPair() {
return keyPairGenerator.generateKeyPair();
}
public KeyParser getKeyParser() {
return keyParser;
}
public MessageDigest getMessageDigest() {
try {
return MessageDigest.getInstance(DIGEST_ALGO, PROVIDER);
} catch(NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchProviderException impossible) {
throw new RuntimeException(impossible);
}
}
public Signature getSignature() {
try {
return Signature.getInstance(SIGNATURE_ALGO, PROVIDER);
} catch(NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchProviderException impossible) {
throw new RuntimeException(impossible);
}
}
}