blob: 22eac4521b56c900e550b9dafb4f7faef272ba54 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.cryptography;
2
3import java.math.*;
4import java.security.*;
5import java.security.interfaces.*;
6import java.security.spec.*;
7import java.util.regex.*;
8
9public class Crypto {
Stuart McCulloch2286f232012-06-15 13:27:53 +000010 static final Pattern RSA_PRIVATE = Pattern.compile("\\s*RSA\\.Private\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
11 static final Pattern RSA_PUBLIC = Pattern.compile("\\s*RSA\\.Public\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
Stuart McCullochbb014372012-06-07 21:57:32 +000012
13 /**
Stuart McCullochbb014372012-06-07 21:57:32 +000014 * @param <T>
15 * @param spec
16 * @return
17 * @throws Exception
18 */
Stuart McCulloch2286f232012-06-15 13:27:53 +000019 @SuppressWarnings("unchecked")
20 public static <T> T fromString(String spec, Class<T> c) throws Exception {
21 if (PrivateKey.class.isAssignableFrom(c)) {
22 Matcher m = RSA_PRIVATE.matcher(spec);
23 if (m.matches()) {
24 return (T) RSA.createPrivate(new BigInteger(m.group(1)), new BigInteger(m.group(2)));
Stuart McCullochbb014372012-06-07 21:57:32 +000025 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000026 throw new IllegalArgumentException("No such private key " + spec);
Stuart McCullochbb014372012-06-07 21:57:32 +000027 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000028
29 if (PublicKey.class.isAssignableFrom(c)) {
30 Matcher m = RSA_PUBLIC.matcher(spec);
31 if (m.matches()) {
32 return (T) RSA.create(new RSAPublicKeySpec(new BigInteger(m.group(1)), new BigInteger(m.group(2))));
Stuart McCullochbb014372012-06-07 21:57:32 +000033 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000034 throw new IllegalArgumentException("No such public key " + spec);
Stuart McCullochbb014372012-06-07 21:57:32 +000035 }
36 return null;
37 }
38
Stuart McCulloch2286f232012-06-15 13:27:53 +000039 public static String toString(Object key) {
40 if (key instanceof RSAPrivateKey) {
Stuart McCullochbb014372012-06-07 21:57:32 +000041 RSAPrivateKey pk = (RSAPrivateKey) key;
42 return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")";
43 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000044 if (key instanceof RSAPublicKey) {
Stuart McCullochbb014372012-06-07 21:57:32 +000045 RSAPublicKey pk = (RSAPublicKey) key;
46 return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")";
47 }
48 return null;
49 }
50
Stuart McCulloch2286f232012-06-15 13:27:53 +000051 // public static <T extends Digest> Signer<T> signer(PrivateKey key,
52 // Digester<T> digester) throws NoSuchAlgorithmException {
53 // Signature s = Signature.getInstance(key.getAlgorithm() + "with" +
54 // digester.getAlgorithm());
55 // return new Signer<T>(s,digester);
56 // }
Stuart McCullochbb014372012-06-07 21:57:32 +000057
58 public static Verifier verifier(PublicKey key, Digest digest) throws NoSuchAlgorithmException {
59 Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digest.getAlgorithm());
Stuart McCulloch2286f232012-06-15 13:27:53 +000060 return new Verifier(s, digest);
Stuart McCullochbb014372012-06-07 21:57:32 +000061 }
62
Stuart McCullochbb014372012-06-07 21:57:32 +000063}