Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.cryptography; |
| 2 | |
| 3 | import java.math.*; |
| 4 | import java.security.*; |
| 5 | import java.security.interfaces.*; |
| 6 | import java.security.spec.*; |
| 7 | import java.util.regex.*; |
| 8 | |
| 9 | public class Crypto { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 10 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | |
| 13 | /** |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | * @param <T> |
| 15 | * @param spec |
| 16 | * @return |
| 17 | * @throws Exception |
| 18 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 19 | @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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 26 | throw new IllegalArgumentException("No such private key " + spec); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 28 | |
| 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 34 | throw new IllegalArgumentException("No such public key " + spec); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | } |
| 36 | return null; |
| 37 | } |
| 38 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 39 | public static String toString(Object key) { |
| 40 | if (key instanceof RSAPrivateKey) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | RSAPrivateKey pk = (RSAPrivateKey) key; |
| 42 | return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")"; |
| 43 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 44 | if (key instanceof RSAPublicKey) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 45 | RSAPublicKey pk = (RSAPublicKey) key; |
| 46 | return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")"; |
| 47 | } |
| 48 | return null; |
| 49 | } |
| 50 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 51 | // 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | |
| 58 | public static Verifier verifier(PublicKey key, Digest digest) throws NoSuchAlgorithmException { |
| 59 | Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digest.getAlgorithm()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 60 | return new Verifier(s, digest); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | } |