blob: 3d271889fcbc43bd8aacca86b4b42a94f1cd6abd [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 public static <T> T fromString(String spec, Class<T> c) throws Exception {
20 if (PrivateKey.class.isAssignableFrom(c)) {
21 Matcher m = RSA_PRIVATE.matcher(spec);
22 if (m.matches()) {
23 return (T) RSA.createPrivate(new BigInteger(m.group(1)), new BigInteger(m.group(2)));
Stuart McCullochbb014372012-06-07 21:57:32 +000024 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000025 throw new IllegalArgumentException("No such private key " + spec);
Stuart McCullochbb014372012-06-07 21:57:32 +000026 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000027
28 if (PublicKey.class.isAssignableFrom(c)) {
29 Matcher m = RSA_PUBLIC.matcher(spec);
30 if (m.matches()) {
31 return (T) RSA.create(new RSAPublicKeySpec(new BigInteger(m.group(1)), new BigInteger(m.group(2))));
Stuart McCullochbb014372012-06-07 21:57:32 +000032 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000033 throw new IllegalArgumentException("No such public key " + spec);
Stuart McCullochbb014372012-06-07 21:57:32 +000034 }
35 return null;
36 }
37
Stuart McCulloch2286f232012-06-15 13:27:53 +000038 public static String toString(Object key) {
39 if (key instanceof RSAPrivateKey) {
Stuart McCullochbb014372012-06-07 21:57:32 +000040 RSAPrivateKey pk = (RSAPrivateKey) key;
41 return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")";
42 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000043 if (key instanceof RSAPublicKey) {
Stuart McCullochbb014372012-06-07 21:57:32 +000044 RSAPublicKey pk = (RSAPublicKey) key;
45 return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")";
46 }
47 return null;
48 }
49
Stuart McCulloch2286f232012-06-15 13:27:53 +000050 // public static <T extends Digest> Signer<T> signer(PrivateKey key,
51 // Digester<T> digester) throws NoSuchAlgorithmException {
52 // Signature s = Signature.getInstance(key.getAlgorithm() + "with" +
53 // digester.getAlgorithm());
54 // return new Signer<T>(s,digester);
55 // }
Stuart McCullochbb014372012-06-07 21:57:32 +000056
57 public static Verifier verifier(PublicKey key, Digest digest) throws NoSuchAlgorithmException {
58 Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digest.getAlgorithm());
Stuart McCulloch2286f232012-06-15 13:27:53 +000059 return new Verifier(s, digest);
Stuart McCullochbb014372012-06-07 21:57:32 +000060 }
61
Stuart McCullochbb014372012-06-07 21:57:32 +000062}