|
From: <pe...@us...> - 2004-02-19 15:40:13
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16429/src/java/org/neuclear/commons/crypto Modified Files: Base64.java CryptoTools.java Log Message: Various cleanups and corrections Index: Base64.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/Base64.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Base64.java 19 Feb 2004 00:27:34 -0000 1.4 --- Base64.java 19 Feb 2004 15:29:10 -0000 1.5 *************** *** 1,4 **** --- 1,7 ---- /* $Id$ * $Log$ + * Revision 1.5 2004/02/19 15:29:10 pelle + * Various cleanups and corrections + * * Revision 1.4 2004/02/19 00:27:34 pelle * Discovered several incompatabilities with the xmlsig implementation. Have been working on getting it working. *************** *** 61,64 **** --- 64,68 ---- */ package org.neuclear.commons.crypto; + import java.math.BigInteger; *************** *** 79,90 **** public final class Base64 { ! /** Field LINE_SEPARATOR */ public static final String LINE_SEPARATOR = "\n"; ! /** Field BASE64DEFAULTLENGTH */ public static final int BASE64DEFAULTLENGTH = 76; - /** Field _base64length */ - static final int _base64length = Base64.BASE64DEFAULTLENGTH; private Base64() { --- 83,96 ---- public final class Base64 { ! /** ! * Field LINE_SEPARATOR ! */ public static final String LINE_SEPARATOR = "\n"; ! /** ! * Field BASE64DEFAULTLENGTH ! */ public static final int BASE64DEFAULTLENGTH = 76; private Base64() { *************** *** 99,103 **** */ public static int getBase64WrapLength() { ! return Base64._base64length; } --- 105,109 ---- */ public static int getBase64WrapLength() { ! return BASE64DEFAULTLENGTH; } *************** *** 105,109 **** * Returns a byte-array representation of a <code>{@link java.math.BigInteger}<code>. * No sign-bit is outputed. ! * * <p><b>N.B.:</B> <code>{@link java.math.BigInteger}<code>'s toByteArray * retunrs eventually longer arrays because of the leading sign-bit. --- 111,115 ---- * Returns a byte-array representation of a <code>{@link java.math.BigInteger}<code>. * No sign-bit is outputed. ! * <p/> * <p><b>N.B.:</B> <code>{@link java.math.BigInteger}<code>'s toByteArray * retunrs eventually longer arrays because of the leading sign-bit. *************** *** 114,118 **** public static byte[] getBytes(final BigInteger big) { ! int bitlen= big.bitLength(); //round bitlen bitlen = ((bitlen + 7) >> 3) << 3; --- 120,124 ---- public static byte[] getBytes(final BigInteger big) { ! int bitlen = big.bitLength(); //round bitlen bitlen = ((bitlen + 7) >> 3) << 3; *************** *** 162,166 **** public static String encodeClean(final byte[] bytes) { ! return LINE_SEPARATOR+ encode(bytes,76)+LINE_SEPARATOR; } --- 168,172 ---- public static String encodeClean(final byte[] bytes) { ! return LINE_SEPARATOR + encode(bytes, BASE64DEFAULTLENGTH) + LINE_SEPARATOR; } *************** *** 169,175 **** * Method decodeBase64Element * - * * @param base64 - * * @return */ --- 175,179 ---- *************** *** 193,202 **** * wrapped line</p> * ! * @param raw <code>byte[]</code> data to be encoded * @param wrap <code>int<code> length of wrapped lines; No wrapping if less than 4. * @return a <code>String</code> with encoded data */ public static String encode(final byte[] raw, final int wrap) { ! final byte[] b64=org.bouncycastle.util.encoders.Base64.encode(raw); //calculate length of encoded string --- 197,206 ---- * wrapped line</p> * ! * @param raw <code>byte[]</code> data to be encoded * @param wrap <code>int<code> length of wrapped lines; No wrapping if less than 4. * @return a <code>String</code> with encoded data */ public static String encode(final byte[] raw, final int wrap) { ! final byte[] b64 = org.bouncycastle.util.encoders.Base64.encode(raw); //calculate length of encoded string *************** *** 205,215 **** final int lines = (encLen / wrap); ! final byte[] encoded = new byte[encLen+lines]; ! int sx=0,dx=0; ! for (sx = 0; sx < (lines*wrap); sx+=wrap,dx+=(wrap+1)) { ! System.arraycopy(b64,sx,encoded, dx,wrap); ! encoded[dx+wrap]='\n'; } ! System.arraycopy(b64,sx,encoded, dx,encLen-sx); --- 209,219 ---- final int lines = (encLen / wrap); ! final byte[] encoded = new byte[encLen + lines]; ! int sx = 0, dx = 0; ! for (sx = 0; sx < (lines * wrap); sx += wrap, dx += (wrap + 1)) { ! System.arraycopy(b64, sx, encoded, dx, wrap); ! encoded[dx + wrap] = '\n'; } ! System.arraycopy(b64, sx, encoded, dx, encLen - sx); Index: CryptoTools.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/CryptoTools.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** CryptoTools.java 18 Feb 2004 00:13:41 -0000 1.13 --- CryptoTools.java 19 Feb 2004 15:29:10 -0000 1.14 *************** *** 2,5 **** --- 2,8 ---- * $Id$ * $Log$ + * Revision 1.14 2004/02/19 15:29:10 pelle + * Various cleanups and corrections + * * Revision 1.13 2004/02/18 00:13:41 pelle * Many, many clean ups. I've readded Targets in a new method. *************** *** 266,269 **** --- 269,274 ---- public final class CryptoTools { + private CryptoTools() { + } *************** *** 316,319 **** --- 321,325 ---- return null; } + public static PublicKey getPublicKeyFromBase64(final String b64) throws CryptoException { try { *************** *** 337,342 **** h = 2 * i; final byte src = barray[i]; ! hexarray[h] = hexTable[(src & 0xF0) >> 4]; ! hexarray[h + 1] = hexTable[src & 0x0F]; } --- 343,348 ---- h = 2 * i; final byte src = barray[i]; ! hexarray[h] = HEX_TABLE[(src & 0xF0) >> 4]; ! hexarray[h + 1] = HEX_TABLE[src & 0x0F]; } *************** *** 360,373 **** } - public static byte[] getHash(final String value) throws CryptoException { - try { - final MessageDigest dig = MessageDigest.getInstance("SHA1"); - dig.digest(value.getBytes()); - return dig.digest(); - } catch (NoSuchAlgorithmException e) { - rethrowException(e); - return null; - } - } //Quick Hack. Not very efficient I know. --- 366,369 ---- *************** *** 425,429 **** rethrowException(e); } ! return null; } --- 421,425 ---- rethrowException(e); } ! return new byte[0]; } *************** *** 466,470 **** rethrowException(e); } ! return null; } --- 462,466 ---- rethrowException(e); } ! return new byte[0]; } *************** *** 508,511 **** --- 504,508 ---- return digest(dig, value); } + public static byte[] digest(final String value) { return digest(value.getBytes()); *************** *** 542,548 **** --- 539,547 ---- return true; } + /** * Unpadded Base32 Encoding as defined in: * <a href="http://www.waterken.com/dev/Enc/base32/">http://www.waterken.com/dev/Enc/base32/</a> + * * @param val * @return *************** *** 607,618 **** * */ ! public static Cipher makePBECipher( ! final String algorithm, ! final int mode, ! final char[] password, ! final byte[] salt, ! final int iterationCount, ! final String provider ! ) throws GeneralSecurityException { final PBEKeySpec pbeSpec = new PBEKeySpec(password); final SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, provider); --- 606,615 ---- * */ ! public static Cipher makePBECipher(final String algorithm, ! final int mode, ! final char[] password, ! final byte[] salt, ! final int iterationCount, ! final String provider) throws GeneralSecurityException { final PBEKeySpec pbeSpec = new PBEKeySpec(password); final SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, provider); *************** *** 638,647 **** * */ ! public static Cipher makePBECipher( ! final int mode, ! final char[] password, ! final byte[] salt, ! final int iterationCount ! ) throws GeneralSecurityException { return makePBECipher(DEFAULT_PBE_ALGORITHM, mode, password, salt, iterationCount, DEFAULT_JCE_PROVIDER); } --- 635,642 ---- * */ ! public static Cipher makePBECipher(final int mode, ! final char[] password, ! final byte[] salt, ! final int iterationCount) throws GeneralSecurityException { return makePBECipher(DEFAULT_PBE_ALGORITHM, mode, password, salt, iterationCount, DEFAULT_JCE_PROVIDER); } *************** *** 657,664 **** * */ ! public static Cipher makePBECipher( ! final int mode, ! final char[] password ! ) throws GeneralSecurityException { return makePBECipher(DEFAULT_PBE_ALGORITHM, mode, password, DEFAULT_SALT, DEFAULT_ITERATION_COUNT, DEFAULT_JCE_PROVIDER); } --- 652,657 ---- * */ ! public static Cipher makePBECipher(final int mode, ! final char[] password) throws GeneralSecurityException { return makePBECipher(DEFAULT_PBE_ALGORITHM, mode, password, DEFAULT_SALT, DEFAULT_ITERATION_COUNT, DEFAULT_JCE_PROVIDER); } *************** *** 701,704 **** --- 694,698 ---- } + public static KeyPairGenerator getTinyKeyPairGenerator() throws NoSuchAlgorithmException { if (kg == null) { *************** *** 751,755 **** || (asn1Bytes[2] != 2) || (i > 20) || (asn1Bytes[4 + rLength] != 2) || (j > 20)) { ! throw new IOException("Invalid ASN.1 format of DSA signature"); } else { --- 745,749 ---- || (asn1Bytes[2] != 2) || (i > 20) || (asn1Bytes[4 + rLength] != 2) || (j > 20)) { ! throw new IOException("Invalid ASN.1 format of DSA signature"); } else { *************** *** 810,815 **** } ! public final static Certificate createCertificate(String name,KeyPair kp) throws SignatureException, InvalidKeyException { ! X509V3CertificateGenerator gen=new X509V3CertificateGenerator(); // Vector code=new Vector(1); // code.add(0,"CN"); --- 804,809 ---- } ! public final static Certificate createCertificate(String name, KeyPair kp) throws SignatureException, InvalidKeyException { ! X509V3CertificateGenerator gen = new X509V3CertificateGenerator(); // Vector code=new Vector(1); // code.add(0,"CN"); *************** *** 818,822 **** // final X509Name x509Name = new X509Name(code,names); // gen.setIssuerDN(x509Name); ! final X509Principal x509 = new X509Principal("CN="+name+", OU=NEU, O=NEU, L=NEU, ST=NEU, C=PA"); gen.setSubjectDN(x509); gen.setIssuerDN(x509); --- 812,816 ---- // final X509Name x509Name = new X509Name(code,names); // gen.setIssuerDN(x509Name); ! final X509Principal x509 = new X509Principal("CN=" + name + ", OU=NEU, O=NEU, L=NEU, ST=NEU, C=PA"); gen.setSubjectDN(x509); gen.setIssuerDN(x509); *************** *** 825,831 **** gen.setNotAfter(TimeTools.get2020()); gen.setSignatureAlgorithm("SHA1withRSA"); ! gen.setSerialNumber(new BigInteger( digest(kp.getPublic().getEncoded()))); return gen.generateX509Certificate(kp.getPrivate()); } { ensureProvider(); --- 819,826 ---- gen.setNotAfter(TimeTools.get2020()); gen.setSignatureAlgorithm("SHA1withRSA"); ! gen.setSerialNumber(new BigInteger(digest(kp.getPublic().getEncoded()))); return gen.generateX509Certificate(kp.getPrivate()); } + { ensureProvider(); *************** *** 839,843 **** private static final byte DEFAULT_SALT[] = "LiquidNightClubPanam".getBytes(); private static final int DEFAULT_ITERATION_COUNT = 2048; ! public static final byte[] hexTable = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static SecureRandom randSource; --- 834,838 ---- private static final byte DEFAULT_SALT[] = "LiquidNightClubPanam".getBytes(); private static final int DEFAULT_ITERATION_COUNT = 2048; ! private static final byte[] HEX_TABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static SecureRandom randSource; *************** *** 845,848 **** public final static String DEFAULT_KEYSTORE = System.getProperty("user.home") + "/.neuclear/keystore.jks"; public static final int RAND_BIT_LENGTH = 128; ! private static final long YPLUS20 = 20*365*24*60*60; } --- 840,843 ---- public final static String DEFAULT_KEYSTORE = System.getProperty("user.home") + "/.neuclear/keystore.jks"; public static final int RAND_BIT_LENGTH = 128; ! private static final long YPLUS20 = 20 * 365 * 24 * 60 * 60; } |