1 19 package org.lucane.common.crypto; 20 21 import java.io.UnsupportedEncodingException ; 22 import java.security.MessageDigest ; 23 import java.security.NoSuchAlgorithmException ; 24 25 26 public class MD5 27 { 28 private static final char[] hex = { 29 '0', '1', '2', '3', '4', '5', '6', '7', 30 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 31 }; 32 33 40 private static final String toHex(byte hash[]) 41 { 42 StringBuffer buf = new StringBuffer (hash.length * 2); 43 44 for (int idx=0; idx<hash.length; idx++) 45 buf.append(hex[(hash[idx] >> 4) & 0x0f]).append(hex[hash[idx] & 0x0f]); 46 47 return buf.toString(); 48 } 49 50 56 private static final byte[] digest(byte[] input) 57 { 58 try 59 { 60 MessageDigest md5 = MessageDigest.getInstance("MD5"); 61 return md5.digest(input); 62 } 63 catch (NoSuchAlgorithmException nsae) 64 { 65 throw new Error (nsae.toString()); 66 } 67 } 68 69 76 private static final byte[] digest(byte[] input1, byte[] input2) 77 { 78 try 79 { 80 MessageDigest md5 = MessageDigest.getInstance("MD5"); 81 md5.update(input1); 82 return md5.digest(input2); 83 } 84 catch (NoSuchAlgorithmException nsae) 85 { 86 throw new Error (nsae.toString()); 87 } 88 } 89 90 96 public static final String encode(String input) 97 { 98 try { 99 return toHex(digest(input.getBytes("8859_1"))); 100 } catch(UnsupportedEncodingException uee) { 101 throw new Error (uee.toString()); 102 } 103 } 104 } 105 | Popular Tags |