1 18 package sync4j.framework.tools; 19 20 import java.security.SecureRandom ; 21 import java.security.MessageDigest ; 22 23 36 public class MD5 { 37 38 private static SecureRandom random = null; 40 private static MessageDigest md = null; 41 42 44 45 protected MD5() { 46 } 47 48 50 56 public static byte[] getNextNonce() { 57 byte[] nextNonce = new byte[16]; 58 random.nextBytes(nextNonce); 59 60 int i; 61 for (int j=0; j<nextNonce.length; ++j) { 62 i = nextNonce[j] & 0x000000ff; 63 if ((i<32) || (i>128)) { 64 nextNonce[j] = (byte)(32 + (i % 64)); 65 } 66 } 67 68 return nextNonce; 69 } 70 71 77 public static byte[] digest(byte[] data) { 78 md.reset(); 79 return md.digest(data); 80 } 81 82 84 88 private static void randomGeneratorInit() 89 throws java.security.NoSuchAlgorithmException { 90 random = SecureRandom.getInstance("SHA1PRNG"); 91 } 92 93 95 static { 96 try { 97 randomGeneratorInit(); 98 md = MessageDigest.getInstance("MD5"); 99 } catch(Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 } 104 | Popular Tags |