1 29 package Acme.Crypto; 30 31 import java.io.*; 32 33 44 public class CryptoUtils 45 { 46 47 public static void zeroBlock( byte[] block, int off, int len ) 49 { 50 for ( int i = off; i < off + len; ++i ) 51 block[i] = 0; 52 } 53 54 public static void zeroBlock( byte[] block ) 56 { 57 zeroBlock( block, 0, block.length ); 58 } 59 60 public static void randomBlock( byte[] block, int off, int len ) 62 { 63 for ( int i = off; i < off + len; ++i ) 64 block[i] = (byte) ( Math.random() * 256.0 ); 65 } 66 67 public static void randomBlock( byte[] block ) 69 { 70 randomBlock( block, 0, block.length ); 71 } 72 73 public static void xorBlock( byte[] a, int aOff, byte[] b, int bOff, byte[] dst, int dstOff, int len ) 75 { 76 for ( int i = 0; i < len; ++i ) 77 dst[dstOff + i] = (byte) ( a[aOff + i] ^ b[bOff + i] ); 78 } 79 80 public static void xorBlock( byte[] a, byte[] b, byte[] dst ) 82 { 83 xorBlock( a, 0, b, 0, dst, 0, a.length ); 84 } 85 86 public static void copyBlock( byte[] src, int srcOff, byte[] dst, int dstOff, int len ) 88 { 89 for ( int i = 0; i < len; ++i ) 90 dst[dstOff + i] = src[srcOff + i]; 91 } 92 93 public static void copyBlock( byte[] src, byte[] dst ) 95 { 96 copyBlock( src, 0, dst, 0, src.length ); 97 } 98 99 public static boolean equalsBlock( byte[] a, int aOff, byte[] b, int bOff, int len ) 101 { 102 for ( int i = 0; i < len; ++i ) 103 if ( a[aOff + i] != b[bOff + i] ) 104 return false; 105 return true; 106 } 107 108 public static boolean equalsBlock( byte[] a, byte[] b ) 110 { 111 return equalsBlock( a, 0, b, 0, a.length ); 112 } 113 114 public static void fillBlock( byte[] block, int blockOff, byte b, int len ) 116 { 117 for ( int i = blockOff; i < blockOff + len; ++i ) 118 block[i] = b; 119 } 120 121 public static void fillBlock( byte[] block, byte b ) 123 { 124 fillBlock( block, 0, b, block.length ); 125 } 126 127 public static void squashBytesToInts( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen ) 129 { 130 for ( int i = 0; i < intLen; ++i ) 131 outInts[outOff + i] = 132 ( ( inBytes[inOff + i * 4 ] & 0xff ) << 24 ) | 133 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) | 134 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 8 ) | 135 ( ( inBytes[inOff + i * 4 + 3] & 0xff ) ); 136 } 137 138 public static void spreadIntsToBytes( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen ) 140 { 141 for ( int i = 0; i < intLen; ++i ) 142 { 143 outBytes[outOff + i * 4 ] = 144 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff ); 145 outBytes[outOff + i * 4 + 1] = 146 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff ); 147 outBytes[outOff + i * 4 + 2] = 148 (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff ); 149 outBytes[outOff + i * 4 + 3] = 150 (byte) ( ( inInts[inOff + i] ) & 0xff ); 151 } 152 } 153 154 public static void squashBytesToIntsLittle( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen ) 156 { 157 for ( int i = 0; i < intLen; ++i ) 158 outInts[outOff + i] = 159 ( ( inBytes[inOff + i * 4 ] & 0xff ) ) | 160 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 8 ) | 161 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 16 ) | 162 ( ( inBytes[inOff + i * 4 + 3] & 0xff ) << 24 ); 163 } 164 165 public static void spreadIntsToBytesLittle( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen ) 167 { 168 for ( int i = 0; i < intLen; ++i ) 169 { 170 outBytes[outOff + i * 4 ] = 171 (byte) ( ( inInts[inOff + i] ) & 0xff ); 172 outBytes[outOff + i * 4 + 1] = 173 (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff ); 174 outBytes[outOff + i * 4 + 2] = 175 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff ); 176 outBytes[outOff + i * 4 + 3] = 177 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff ); 178 } 179 } 180 181 public static void squashBytesToShorts( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen ) 183 { 184 for ( int i = 0; i < shortLen; ++i ) 185 outShorts[outOff + i] = 186 ( ( inBytes[inOff + i * 2 ] & 0xff ) << 8 ) | 187 ( ( inBytes[inOff + i * 2 + 1] & 0xff ) ); 188 } 189 190 public static void spreadShortsToBytes( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen ) 192 { 193 for ( int i = 0; i < shortLen; ++i ) 194 { 195 outBytes[outOff + i * 2 ] = 196 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff ); 197 outBytes[outOff + i * 2 + 1] = 198 (byte) ( ( inShorts[inOff + i] ) & 0xff ); 199 } 200 } 201 202 public static void squashBytesToShortsLittle( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen ) 204 { 205 for ( int i = 0; i < shortLen; ++i ) 206 outShorts[outOff + i] = 207 ( ( inBytes[inOff + i * 2 ] & 0xff ) ) | 208 ( ( inBytes[inOff + i * 2 + 1] & 0xff ) << 8 ); 209 } 210 211 public static void spreadShortsToBytesLittle( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen ) 213 { 214 for ( int i = 0; i < shortLen; ++i ) 215 { 216 outBytes[outOff + i * 2 ] = 217 (byte) ( ( inShorts[inOff + i] ) & 0xff ); 218 outBytes[outOff + i * 2 + 1] = 219 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff ); 220 } 221 } 222 223 public static String toStringBlock( byte[] block, int off, int len ) 225 { 226 String hexits = "0123456789abcdef"; 227 StringBuffer buf = new StringBuffer (); 228 for ( int i = off; i < off + len; ++i ) 229 { 230 buf.append( hexits.charAt( ( block[i] >>> 4 ) & 0xf ) ); 231 buf.append( hexits.charAt( block[i] & 0xf ) ); 232 } 233 return "[" + buf + "]"; 234 } 235 236 public static String toStringBlock( byte[] block ) 238 { 239 return toStringBlock( block, 0, block.length ); 240 } 241 242 } 243 | Popular Tags |