|
From: <pe...@us...> - 2004-01-18 21:20:23
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto In directory sc8-pr-cvs1:/tmp/cvs-serv28561/src/java/org/neuclear/commons/crypto Modified Files: Base32.java CryptoTools.java Log Message: Created Base32 encoder that now fully complies with Tyler's spec. Index: Base32.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/Base32.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Base32.java 16 Jan 2004 23:41:59 -0000 1.1 --- Base32.java 18 Jan 2004 21:20:20 -0000 1.2 *************** *** 2,6 **** import java.math.BigInteger; - /* NeuClear Distributed Transaction Clearing Platform --- 2,5 ---- *************** *** 23,26 **** --- 22,28 ---- $Id$ $Log$ + Revision 1.2 2004/01/18 21:20:20 pelle + Created Base32 encoder that now fully complies with Tyler's spec. + Revision 1.1 2004/01/16 23:41:59 pelle Added Base32 class. The Base32 encoding used wasnt following the standards. *************** *** 33,39 **** * <a href="http://www.waterken.com/dev/Enc/base32/">Tyler Close's Base32 page</a> */ ! public class Base32 { //Disallow Instantiation private Base32(){ } /** --- 35,42 ---- * <a href="http://www.waterken.com/dev/Enc/base32/">Tyler Close's Base32 page</a> */ ! public final class Base32 { //Disallow Instantiation private Base32(){ + } /** *************** *** 51,65 **** - /** * Method decodeBase32Element * * ! * @param base32 * * @return */ ! public static byte[] decode(final byte[] base32) throws CryptoException { ! return null; } --- 54,113 ---- /** * Method decodeBase32Element * * ! * @param raw * * @return */ ! public static byte[] decode(final byte[] raw) throws CryptoException { ! final int baselength = (raw.length*5); ! final int mod=baselength%8; ! final int length=getDecodedLength(raw.length); ! final byte decoded[]=new byte[length]; ! final byte chunk[]=new byte[5]; ! long chs=0; ! ! for (int ri=0,di=0;ri<(raw.length);ri=ri+8,di=di+5){ ! int rchsize=(raw.length-ri)>=8?8:(raw.length-ri); ! for (int j=0;j<rchsize;j++){ ! chs=(chs<<5)|decodeVal(raw[ri+j]); ! } ! switch(rchsize){ ! case 0: ! break; ! case 8: ! break; ! case 2: ! if ((chs&0x03)!=0) ! throw new CryptoException("Encoding Error"); ! chs>>=2; ! break; ! case 4: ! if ((chs&0x0F)!=0) ! throw new CryptoException("Encoding Error"); ! chs>>=4; ! break; ! case 5: ! if ((chs&0x01)!=0) ! throw new CryptoException("Encoding Error"); ! chs>>=1; ! break; ! case 7: ! if ((chs&0x07)!=0) ! throw new CryptoException("Encoding Error"); ! chs>>=3; ! break; ! default: ! throw new CryptoException("Encoding Error"); ! } ! int chsize=(length-di)>=5?5:(length-di); ! for (int j=0;j<chsize;j++){ ! decoded[di+(chsize-1-j)]=(byte) ((chs>>(8*j))&0xFF); ! } ! ! } ! return decoded; } *************** *** 80,116 **** * @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)); } } --- 128,182 ---- * @return the <code>String<code> with encoded data */ ! public static String encode(final byte[] raw) { 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+1; ! return baselength/5; } ! public static int getDecodedLength(int src) { ! final int baselength = src*5; ! return baselength/8; ! } ! ! public static byte[] encodeToByteArray(final byte[]raw) { 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[8]; long chs=0; //ri= src index, ei= encoded index ! int ci=5; for (int ri=0,ei=0;ri<raw.length;ri=ri+5,ei=ei+8){ ! chs=0; ! int rcsize=(raw.length-ri)>=5?5:(raw.length-ri); ! for (int j=ri;j<(ri+rcsize);j++){ ! chs|=raw[j]; ! chs<<=8; ! } ! chs>>=8; ! int ecsize=(length-ei)>=8?8:(length-ei); ! switch((ecsize)){ ! case 2: ! chs<<=2; ! break; ! case 4: ! chs<<=4; ! break; ! case 5: ! chs<<=1; ! break; ! case 7: ! chs<<=3; ! break; ! } ! for (int j=0;j<ecsize;j++){ ! encoded[ei+ecsize-1-j]=encodeVal((byte) (chs&31)); ! chs>>=5; } } *************** *** 130,138 **** } ! 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[]{ --- 196,214 ---- } ! private static byte encodeVal(byte val) { if (val>31) ! throw new RuntimeException("Base32 Encoding Overflow"); return CROSS[val]; } + private static byte decodeVal(byte val) throws CryptoException { + if(val >= 'a' && val <= 'z') + return (byte) (val - 'a'); + else if(val >= '2' && val <= '7') + return (byte) (val - '2' + 26); + else if(val>= 'A' && val <= 'Z') + return (byte)(val - 'A'); + else + throw new CryptoException("Encode Overflow"); + } private static final byte[] CROSS = new byte[]{ Index: CryptoTools.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/CryptoTools.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** CryptoTools.java 16 Jan 2004 23:41:59 -0000 1.11 --- CryptoTools.java 18 Jan 2004 21:20:20 -0000 1.12 *************** *** 2,5 **** --- 2,8 ---- * $Id$ * $Log$ + * Revision 1.12 2004/01/18 21:20:20 pelle + * Created Base32 encoder that now fully complies with Tyler's spec. + * * Revision 1.11 2004/01/16 23:41:59 pelle * Added Base32 class. The Base32 encoding used wasnt following the standards. *************** *** 542,547 **** */ public static String encodeBase32(final byte[] val) { ! final BigInteger big = new BigInteger(val); ! return big.toString(32); } --- 545,549 ---- */ public static String encodeBase32(final byte[] val) { ! return Base32.encode(val); } |