1 25 package org.snipsnap.user; 26 27 import java.security.MessageDigest ; 28 import java.security.NoSuchAlgorithmException ; 29 30 35 public class Digest { 36 private static MessageDigest digest; 37 38 static { 39 try { 40 digest = MessageDigest.getInstance("SHA1"); 41 } catch (NoSuchAlgorithmException e) { 42 System.err.println("UserManager: unable to load digest algorithm: " + e); 43 digest = null; 44 } 45 } 46 47 public static String getDigest(String s) { 48 if (digest != null && s != null) { 49 return digestToHexString(digest.digest(s.getBytes())); 50 } 51 return ""; 52 } 53 54 57 public static boolean authenticate(String password, String encrypted) { 58 return encrypted.equals(getDigest(password)); 59 } 60 61 64 public static String digestToHexString(byte[] digest) { 65 byte b = 0; 66 StringBuffer buffer = new StringBuffer (); 67 68 for (int i = 0; i < digest.length; ++i) { 69 b = digest[i]; 70 int value = (b & 0x7F) + (b < 0 ? 128 : 0); 71 buffer.append(value < 16 ? "0" : ""); 72 buffer.append(Integer.toHexString(value).toUpperCase()); 73 } 74 return buffer.toString(); 75 } 76 } 77 | Popular Tags |