1 package SnowMailClient.crypto; 2 3 import snow.crypto.*; 4 import sun.misc.BASE64Encoder; 5 import sun.misc.BASE64Decoder; 6 7 import java.io.*; 8 import java.math.*; 9 import java.net.*; 10 11 import java.security.*; 12 import java.security.spec.*; 13 import javax.crypto.spec.*; 14 import javax.crypto.*; 15 16 18 public final class Utilities 19 { 20 public static final SecureRandom secureRandom = new SecureRandom(); 21 22 25 public static String hashPassword( String thePassword ) 26 { 27 return encodeSingleLineBase64( CryptoUtilities.SHA1Hash(thePassword.getBytes())); 28 } 29 30 37 public static boolean isValidHashPasswordFormat(String hash) 38 { 39 if(hash.length()!=28) return false; 40 if(hash.charAt(27)!='=') return false; 41 return true; 42 } 43 44 45 50 public static String encodeSingleLineBase64(byte bytes[]) 51 { 52 BASE64Encoder enc = new BASE64Encoder(); 53 String base64 = enc.encode(bytes); 55 return suppressLineFeeds(base64); 56 } 57 58 59 63 public static String encodeBase64(byte bytes[]) 64 { 65 BASE64Encoder enc = new BASE64Encoder(); 66 String base64 = enc.encode(bytes); 68 return base64; 69 } 70 71 72 public static String suppressLineFeeds(String in) 73 { 74 StringBuffer sb = new StringBuffer (); 75 for(int i=0; i<in.length(); i++) 76 { 77 char c = in.charAt(i); 78 if(c !='\n' && c != '\r') sb.append(c); 79 } 80 return sb.toString(); 81 } 82 83 public static byte[] decodeBase64(String coded) 84 throws IOException 85 { 86 BASE64Decoder dec = new BASE64Decoder(); 87 byte original[] = null; 88 original = dec.decodeBuffer(new ByteArrayInputStream(coded.getBytes())); 89 return original; 90 } 91 92 93 public static String stringReplace(String s, String replace, String n) 94 { 95 StringBuffer str = new StringBuffer (s); 96 int pos = -1; 97 while( (pos=s.indexOf(replace,pos+1)) >= 0 ) 98 { str.replace(pos, pos+replace.length(), n); 100 s = str.toString(); 101 } 102 return s; 103 } 104 105 106 114 public static String encryptSingleLineBlowfishFormat64(String mess, byte[] pass) throws Exception 115 { 116 121 122 Cipher fc = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); 124 SecretKeySpec skeySpec = new SecretKeySpec(pass, "Blowfish"); 125 fc.init(Cipher.ENCRYPT_MODE, skeySpec); 126 127 return encodeSingleLineBase64(fc.doFinal(mess.getBytes())); 128 } 129 130 133 public static String decryptSingleLineBlowfishFormat64(String mess, byte[] pass) throws Exception 134 { 135 139 140 Cipher fc = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); 142 SecretKeySpec skeySpec = new SecretKeySpec(pass, "Blowfish"); 143 fc.init(Cipher.DECRYPT_MODE, skeySpec); 144 145 return new String (fc.doFinal(decodeBase64(mess))); 146 } 147 148 149 153 156 public static byte[] concatenate(byte[] a, byte[] b) 157 { 158 byte[] c = new byte[a.length+b.length]; 159 for(int i=0; i<a.length; i++) 161 { 162 c[i]=a[i]; 163 } 164 int lena = a.length; 166 for(int i=0; i<b.length; i++) 167 { 168 c[i+lena] = b[i]; 169 } 170 return c; 171 } 172 173 public static String printBytes(byte[] b) 174 { 175 StringBuffer sb = new StringBuffer (); 176 for(int i=0; i<b.length; i++) 177 { 178 sb.append(" "); 179 int val = (int)b[i]; 180 if(Math.abs(val)<10) sb.append(" "); 181 if(Math.abs(val)<100) sb.append(" "); 182 if(val>=0) sb.append(" "); 183 sb.append((int)b[i]); 184 if(i%20==19) sb.append("\n"); 185 } 186 return sb.toString(); 187 } 188 189 public static byte[] extract(byte[] b, int off, int len) 190 { 191 byte[] rep = new byte[len]; 192 for(int i=0; i<len; i++) 193 { 194 rep[i] = b[i+off]; 195 } 196 return rep; 197 } 198 199 200 201 205 public static BigInteger generateRandomBlowfishSecretKey(int bitlen) 206 { 207 return new BigInteger(bitlen, secureRandom); 208 } 209 210 211 217 public static String HMAC_KEYED_CRAM_MD5(byte[] _key, byte[] b, String username) 218 { 219 byte[] key = _key; 220 if(key.length>64) 221 { 222 key = CryptoUtilities.hashMD5(key); 223 } 224 225 byte[] ipad = new byte[64]; 226 byte[] opad = new byte[64]; 227 228 for(int i=0; i<64; i++) 229 { 230 ipad[i] = 0x36; 231 opad[i] = 0x5C; 232 } 233 byte[] hash = CryptoUtilities.hashMD5( 234 concatenate( XOR(opad,key), 235 CryptoUtilities.hashMD5( concatenate( XOR(ipad,key), b ) ) 236 )); 237 238 return encodeSingleLineBase64((username+" "+asHex(hash)).getBytes()); 239 } 240 241 242 243 245 public static String asHex (byte[] bytes) 246 { 247 StringBuffer buf = new StringBuffer (2*bytes.length); 248 249 for(int i = 0; i < bytes.length; i++) 250 { 251 if (((int) bytes[i] & 0xff) < 0x10) 252 { 253 buf.append("0"); 254 } 255 buf.append(Long.toString((int) bytes[i] & 0xff, 16)); 256 } 257 return buf.toString(); 258 } 259 260 261 265 public static byte[] XOR(byte[] a, byte[] b) 266 { 267 if(a.length < b.length) 268 { 269 throw new RuntimeException ("Bad lengths la<lb, a="+a.length+", b="+b.length); 270 } 271 272 byte[] rep = a.clone(); 273 for(int i=0; i<b.length; i++) 274 { 275 rep[i] = (byte) (a[i] ^ b[i]); 276 } 277 return rep; 278 } 279 280 281 282 } | Popular Tags |