|
From: <pe...@us...> - 2004-01-16 23:42:03
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto In directory sc8-pr-cvs1:/tmp/cvs-serv8157/src/java/org/neuclear/commons/crypto Modified Files: CryptoTools.java Added Files: Base32.java Log Message: Added Base32 class. The Base32 encoding used wasnt following the standards. Added user creatable Identity for Public Keys --- NEW FILE: Base32.java --- package org.neuclear.commons.crypto; import java.math.BigInteger; /* NeuClear Distributed Transaction Clearing Platform (C) 2003 Pelle Braendgaard This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: Base32.java,v 1.1 2004/01/16 23:41:59 pelle Exp $ $Log: Base32.java,v $ Revision 1.1 2004/01/16 23:41:59 pelle Added Base32 class. The Base32 encoding used wasnt following the standards. Added user creatable Identity for Public Keys */ /** * Utility class for doing URL safe Base32 encodeings as specified in * <a href="http://www.waterken.com/dev/Enc/base32/">Tyler Close's Base32 page</a> */ public class Base32 { //Disallow Instantiation private Base32(){ } /** * Encode in Base32 the given <code>{@link java.math.BigInteger}<code>. * * @param big * @return String with Base32 encoding */ public static String encode(final BigInteger big) throws CryptoException { // System.out.println("JDK toByteArray(): "+encode(big.toByteArray())); // System.out.println("getBytes(): "+encode(getBytes(big))); return encode(big.toByteArray()); } /** * Method decodeBase32Element * * * @param base32 * * @return */ public static byte[] decode(final byte[] base32) throws CryptoException { return null; } /** * <p>Decode a Base32-encoded string to a byte array</p> * * @param base32 <code>String</code> encoded string (single line only !!) * @return Decoded data in a byte array */ public static byte[] decode(final String base32) throws CryptoException { return decode(base32.getBytes()); } /** * * @param raw <code>byte[]<code> to be base32 encoded * @return the <code>String<code> with encoded data */ public static String encode(final byte[] raw) throws CryptoException { return new String(encodeToByteArray(raw)); } public static int getEncodedLength(int src){ final int baselength = (src*8); final int mod=baselength%5; if (mod==0) return baselength/5; else return baselength/5+1; } public static byte[] encodeToByteArray(final byte[]raw) throws CryptoException { final int baselength = (raw.length*8); final int mod=baselength%5; final int length=getEncodedLength(raw.length); final byte encoded[]=new byte[length]; final byte chunk[]=new byte[5]; long chs=0; //ri= src index, ei= encoded index int ci=8; for (int ri=0,ei=0;ri<raw.length;ri=ri+5,ei=ei+8){ // System.out.print(", "+ri); if (ri==(raw.length-1)&&mod>0) { System.arraycopy(raw,ri,chunk,0,mod); for(int j=mod;j<5;j++) chunk[j]=0; ci=(8*mod)/5+(((8*mod)%5==0)?0:1); } else System.arraycopy(raw,ri,chunk,0,5); chs=new BigInteger(chunk).longValue(); for (int j=0;j<ci;j++){ encoded[ei+j]=encodeVal(getPart(chs,j)); } } return encoded; } private static byte getPart(long chunk,int num){ return (byte) ((chunk>>((7-num)*5))&31); } /** * Encode a String as base32 * * @param raw <code>byte[]<code> to be base32 encoded * @return the <code>String<code> with encoded data */ public static String encode(final String raw) throws CryptoException { return encode(raw.getBytes()); } private static byte encodeVal(byte val) throws CryptoException { if (val>31) throw new CryptoException("Encode Overflow"); return CROSS[val]; } private static final byte[] CROSS = new byte[]{ 'a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','p', 'q','r','s','t','u','v','w','x', 'y','z','2','3','4','5','6','7' }; } Index: CryptoTools.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/CryptoTools.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** CryptoTools.java 9 Jan 2004 16:34:32 -0000 1.10 --- CryptoTools.java 16 Jan 2004 23:41:59 -0000 1.11 *************** *** 2,5 **** --- 2,9 ---- * $Id$ * $Log$ + * Revision 1.11 2004/01/16 23:41:59 pelle + * Added Base32 class. The Base32 encoding used wasnt following the standards. + * Added user creatable Identity for Public Keys + * * Revision 1.10 2004/01/09 16:34:32 pelle * changed use of base36 encoding to base32 to ensure compatibility with other schemes. *************** *** 225,233 **** import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.jce.interfaces.ECPrivateKey; import org.bouncycastle.jce.provider.BouncyCastleProvider; - import org.bouncycastle.jce.X509V3CertificateGenerator; - import org.bouncycastle.jce.X509Principal; - import org.bouncycastle.asn1.x509.X509Name; import org.neuclear.commons.time.TimeTools; --- 229,236 ---- import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; + import org.bouncycastle.jce.X509Principal; + import org.bouncycastle.jce.X509V3CertificateGenerator; import org.bouncycastle.jce.interfaces.ECPrivateKey; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.neuclear.commons.time.TimeTools; *************** *** 250,257 **** import java.security.spec.RSAPublicKeySpec; import java.security.spec.X509EncodedKeySpec; - import java.util.Random; - import java.util.HashMap; - import java.util.Vector; import java.util.Date; // TODO Implement some code to automatically BC Provider if not installed --- 253,258 ---- import java.security.spec.RSAPublicKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Date; + import java.util.Random; // TODO Implement some code to automatically BC Provider if not installed *************** *** 500,503 **** --- 501,507 ---- return digest(dig, value); } + public static byte[] digest(final String value) { + return digest(value.getBytes()); + } public static byte[] digest256(final byte[] value) { *************** *** 673,676 **** --- 677,684 ---- } + public static KeyPair createTinyKeyPair() throws NoSuchAlgorithmException { + return getTinyKeyPairGenerator().generateKeyPair(); + } + public static KeyPair createKeyPair(final String algorithm) throws NoSuchAlgorithmException { *************** *** 687,690 **** --- 695,707 ---- } + public static KeyPairGenerator getTinyKeyPairGenerator() throws NoSuchAlgorithmException { + if (kg == null) { + kg = KeyPairGenerator.getInstance("RSA"); + + kg.initialize(512, new SecureRandom("Bear it all with NeuDist".getBytes())); + } + return kg; + + } public static KeyPairGenerator getKeyPairGenerator(final String algorithm) |