1 17 18 19 20 package org.apache.lenya.ac; 21 22 import java.security.MessageDigest ; 23 24 import org.apache.log4j.Category; 25 26 30 public class Password { 31 private static Category log = Category.getInstance(Password.class); 32 33 38 public static void main(String [] args) { 39 if (args.length != 1) { 40 System.out.println("Usage: plain-text-password"); 41 42 return; 43 } 44 45 try { 46 System.out.println(Password.encrypt(args[0])); 47 } catch (Exception e) { 48 System.err.println(e); 49 } 50 } 51 52 58 public static String encrypt(String plain) { 59 return getMD5(plain); 60 } 61 62 67 public static String getMD5(String plain) { 68 MessageDigest md = null; 69 try { 70 md = MessageDigest.getInstance("MD5"); 71 } catch (java.security.NoSuchAlgorithmException e) { 72 log.error(e); 73 } 74 return stringify(md.digest(plain.getBytes())); 75 } 76 77 82 private static String stringify(byte[] buf) { 83 StringBuffer sb = new StringBuffer (2 * buf.length); 84 85 for (int i = 0; i < buf.length; i++) { 86 int h = (buf[i] & 0xf0) >> 4; 87 int l = (buf[i] & 0x0f); 88 sb.append(new Character ((char) ((h > 9) ? (('a' + h) - 10) : ('0' + h)))); 89 sb.append(new Character ((char) ((l > 9) ? (('a' + l) - 10) : ('0' + l)))); 90 } 91 92 return sb.toString(); 93 } 94 } 95 | Popular Tags |