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 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 25 | throw new IllegalArgumentException("No such private key " + spec); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | |
| 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | throw new IllegalArgumentException("No such public key " + spec); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | } |
| 35 | return null; |
| 36 | } |
| 37 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | public static String toString(Object key) { |
| 39 | if (key instanceof RSAPrivateKey) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | RSAPrivateKey pk = (RSAPrivateKey) key; |
| 41 | return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")"; |
| 42 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 43 | if (key instanceof RSAPublicKey) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | RSAPublicKey pk = (RSAPublicKey) key; |
| 45 | return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")"; |
| 46 | } |
| 47 | return null; |
| 48 | } |
| 49 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 50 | // 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | |
| 57 | public static Verifier verifier(PublicKey key, Digest digest) throws NoSuchAlgorithmException { |
| 58 | Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digest.getAlgorithm()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | return new Verifier(s, digest); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 62 | } |