import java.io.BufferedReader; import java.io.InputStreamReader; import java.security.MessageDigest; import java.util.Random;
public class PasswordHash { private static Random random = new Random();
/** * Default constructor. */ public PasswordHash() { }
public static String encrypt(String inString) { try { MessageDigest md; // MD5, SHA, SHA-1 md = MessageDigest.getInstance("MD5"); byte[] output = md.digest(inString.getBytes()); StringBuffer sb = new StringBuffer(2 * output.length); for (int i = 0; i < output.length; ++i) { int k = output[i] & 0xFF; if (k < 0x10) { sb.append('0'); } sb.append(Integer.toHexString(k)); } return sb.toString(); } catch (java.security.NoSuchAlgorithmException e) { } return null; }
public static void main(String args[]) { PasswordHash hasher = new PasswordHash(); try { String text; if (args.length == 0) { System.out.print("Password: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); text = in.readLine(); System.out.println("Hash: " + hasher.encrypt(text)); } else { text = args[0]; System.out.println(hasher.encrypt(text)); } } catch (Exception ex) { System.out.println(ex); } }
public static String getRandomString(int lo, int hi) { int n = rand(lo, hi); byte b[] = new byte[n]; for (int i = 0; i < n; i++) { b[i] = (byte) rand('a', 'z'); } return new String(b); }
public static int rand(int lo, int hi) { int n = hi - lo + 1; int i = random.nextInt() % n; if (i < 0) { i = -i; } return lo + i; } }