00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package Acme.Crypto;
00030
00031 import java.io.*;
00032
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 public class CryptoUtils implements Serializable
00045 {
00046
00048 public static void zeroBlock( byte[] block, int off, int len )
00049 {
00050 for ( int i = off; i < off + len; ++i )
00051 block[i] = 0;
00052 }
00053
00055 public static void zeroBlock( byte[] block )
00056 {
00057 zeroBlock( block, 0, block.length );
00058 }
00059
00061 public static void randomBlock( byte[] block, int off, int len )
00062 {
00063 for ( int i = off; i < off + len; ++i )
00064 block[i] = (byte) ( Math.random() * 256.0 );
00065 }
00066
00068 public static void randomBlock( byte[] block )
00069 {
00070 randomBlock( block, 0, block.length );
00071 }
00072
00074 public static void xorBlock( byte[] a, int aOff, byte[] b, int bOff, byte[] dst, int dstOff, int len )
00075 {
00076 for ( int i = 0; i < len; ++i )
00077 dst[dstOff + i] = (byte) ( a[aOff + i] ^ b[bOff + i] );
00078 }
00079
00081 public static void xorBlock( byte[] a, byte[] b, byte[] dst )
00082 {
00083 xorBlock( a, 0, b, 0, dst, 0, a.length );
00084 }
00085
00087 public static void copyBlock( byte[] src, int srcOff, byte[] dst, int dstOff, int len )
00088 {
00089 for ( int i = 0; i < len; ++i )
00090 dst[dstOff + i] = src[srcOff + i];
00091 }
00092
00094 public static void copyBlock( byte[] src, byte[] dst )
00095 {
00096 copyBlock( src, 0, dst, 0, src.length );
00097 }
00098
00100 public static boolean equalsBlock( byte[] a, int aOff, byte[] b, int bOff, int len )
00101 {
00102 for ( int i = 0; i < len; ++i )
00103 if ( a[aOff + i] != b[bOff + i] )
00104 return false;
00105 return true;
00106 }
00107
00109 public static boolean equalsBlock( byte[] a, byte[] b )
00110 {
00111 return equalsBlock( a, 0, b, 0, a.length );
00112 }
00113
00115 public static void fillBlock( byte[] block, int blockOff, byte b, int len )
00116 {
00117 for ( int i = blockOff; i < blockOff + len; ++i )
00118 block[i] = b;
00119 }
00120
00122 public static void fillBlock( byte[] block, byte b )
00123 {
00124 fillBlock( block, 0, b, block.length );
00125 }
00126
00128 public static void squashBytesToInts( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
00129 {
00130 for ( int i = 0; i < intLen; ++i )
00131 outInts[outOff + i] =
00132 ( ( inBytes[inOff + i * 4 ] & 0xff ) << 24 ) |
00133 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) |
00134 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 8 ) |
00135 ( ( inBytes[inOff + i * 4 + 3] & 0xff ) );
00136 }
00137
00139 public static void spreadIntsToBytes( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
00140 {
00141 for ( int i = 0; i < intLen; ++i )
00142 {
00143 outBytes[outOff + i * 4 ] =
00144 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
00145 outBytes[outOff + i * 4 + 1] =
00146 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
00147 outBytes[outOff + i * 4 + 2] =
00148 (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff );
00149 outBytes[outOff + i * 4 + 3] =
00150 (byte) ( ( inInts[inOff + i] ) & 0xff );
00151 }
00152 }
00153
00155 public static void squashBytesToIntsLittle( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
00156 {
00157 for ( int i = 0; i < intLen; ++i )
00158 outInts[outOff + i] =
00159 ( ( inBytes[inOff + i * 4 ] & 0xff ) ) |
00160 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 8 ) |
00161 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 16 ) |
00162 ( ( inBytes[inOff + i * 4 + 3] & 0xff ) << 24 );
00163 }
00164
00166 public static void spreadIntsToBytesLittle( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
00167 {
00168 for ( int i = 0; i < intLen; ++i )
00169 {
00170 outBytes[outOff + i * 4 ] =
00171 (byte) ( ( inInts[inOff + i] ) & 0xff );
00172 outBytes[outOff + i * 4 + 1] =
00173 (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff );
00174 outBytes[outOff + i * 4 + 2] =
00175 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
00176 outBytes[outOff + i * 4 + 3] =
00177 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
00178 }
00179 }
00180
00182 public static void squashBytesToShorts( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
00183 {
00184 for ( int i = 0; i < shortLen; ++i )
00185 outShorts[outOff + i] =
00186 ( ( inBytes[inOff + i * 2 ] & 0xff ) << 8 ) |
00187 ( ( inBytes[inOff + i * 2 + 1] & 0xff ) );
00188 }
00189
00191 public static void spreadShortsToBytes( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
00192 {
00193 for ( int i = 0; i < shortLen; ++i )
00194 {
00195 outBytes[outOff + i * 2 ] =
00196 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
00197 outBytes[outOff + i * 2 + 1] =
00198 (byte) ( ( inShorts[inOff + i] ) & 0xff );
00199 }
00200 }
00201
00203 public static void squashBytesToShortsLittle( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
00204 {
00205 for ( int i = 0; i < shortLen; ++i )
00206 outShorts[outOff + i] =
00207 ( ( inBytes[inOff + i * 2 ] & 0xff ) ) |
00208 ( ( inBytes[inOff + i * 2 + 1] & 0xff ) << 8 );
00209 }
00210
00212 public static void spreadShortsToBytesLittle( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
00213 {
00214 for ( int i = 0; i < shortLen; ++i )
00215 {
00216 outBytes[outOff + i * 2 ] =
00217 (byte) ( ( inShorts[inOff + i] ) & 0xff );
00218 outBytes[outOff + i * 2 + 1] =
00219 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
00220 }
00221 }
00222
00224 public static String toStringBlock( byte[] block, int off, int len )
00225 {
00226 String hexits = "0123456789abcdef";
00227 StringBuffer buf = new StringBuffer();
00228 for ( int i = off; i < off + len; ++i )
00229 {
00230 buf.append( hexits.charAt( ( block[i] >>> 4 ) & 0xf ) );
00231 buf.append( hexits.charAt( block[i] & 0xf ) );
00232 }
00233 return "[" + buf + "]";
00234 }
00235
00237 public static String toStringBlock( byte[] block )
00238 {
00239 return toStringBlock( block, 0, block.length );
00240 }
00241
00242 }