View Javadoc
1 2 package jsdsi.sexp; 3 4 import java.io.ByteArrayInputStream; 5 import java.io.IOException; 6 import java.security.InvalidKeyException; 7 import java.security.Key; 8 import java.security.KeyFactorySpi; 9 import java.security.spec.InvalidKeySpecException; 10 11 import jsdsi.Obj; 12 13 /*** 14 * Creates public keys from S-expressions. 15 * 16 * @author Sameer Ajmani 17 * @version $Revision: 1.1.6.1 $ $Date: 2005/11/08 03:12:52 $ 18 */ 19 public class KeyFactory extends KeyFactorySpi { 20 private Obj generateObj(java.security.spec.KeySpec spec) 21 throws InvalidKeySpecException { 22 if (!(spec instanceof jsdsi.sexp.KeySpec)) { 23 throw new InvalidKeySpecException( 24 "Not a " + jsdsi.sexp.KeySpec.class.getName()); 25 } 26 27 try { 28 return new ObjInputStream( 29 new ByteArrayInputStream( 30 ((jsdsi.sexp.KeySpec) spec).getEncoded())) 31 .readObj(); 32 } catch (SexpException e) { 33 throw new InvalidKeySpecException( 34 "Invalid S-expression: " + e.getMessage()); 35 } catch (SexpParseException e) { 36 throw new InvalidKeySpecException( 37 "Invalid SDSI object: " + e.getMessage()); 38 } catch (IOException e) { 39 throw (InvalidKeySpecException) new InvalidKeySpecException() 40 .initCause( 41 e); 42 } 43 } 44 45 protected java.security.PublicKey engineGeneratePublic( 46 java.security.spec.KeySpec spec) 47 throws InvalidKeySpecException { 48 try { 49 return (java.security.PublicKey) generateObj(spec); 50 } catch (ClassCastException e) { 51 throw (InvalidKeySpecException) new InvalidKeySpecException() 52 .initCause( 53 e); 54 } 55 } 56 57 protected java.security.PrivateKey engineGeneratePrivate( 58 java.security.spec.KeySpec spec) 59 throws InvalidKeySpecException { 60 try { 61 return (java.security.PrivateKey) generateObj(spec); 62 } catch (ClassCastException e) { 63 throw (InvalidKeySpecException) new InvalidKeySpecException() 64 .initCause( 65 e); 66 } 67 } 68 69 protected java.security.spec.KeySpec engineGetKeySpec(Key key, Class spec) 70 throws InvalidKeySpecException { 71 if (!jsdsi.sexp.KeySpec.class.isAssignableFrom(spec)) { 72 throw new InvalidKeySpecException( 73 "Not a " 74 + jsdsi.sexp.KeySpec.class.getName() 75 + ": " 76 + spec.getName()); 77 } 78 if (!(key instanceof Obj)) { 79 throw new InvalidKeySpecException( 80 "Not a " 81 + Obj.class.getName() 82 + ": " 83 + key.getClass().getName()); 84 } 85 return new jsdsi.sexp.KeySpec(((Obj) key).toByteArray()); 86 } 87 88 protected Key engineTranslateKey(Key key) throws InvalidKeyException { 89 if (!(key instanceof Obj)) { 90 throw new InvalidKeyException( 91 "Not a " 92 + Obj.class.getName() 93 + ": " 94 + key.getClass().getName()); 95 } 96 return key; 97 } 98 }

This page was automatically generated by Maven