1 package org.cacert.gigi.util;
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
6 public class PasswordHash {
8 public static boolean verifyHash(String password, String hash) {
9 String newhash = sha1(password);
11 if (newhash.length() != hash.length()) {
14 for (int i = 0; i < newhash.length(); i++) {
15 match &= newhash.charAt(i) == hash.charAt(i);
20 private static String sha1(String password) {
22 MessageDigest md = MessageDigest.getInstance("SHA1");
23 byte[] digest = md.digest(password.getBytes());
24 StringBuffer res = new StringBuffer(digest.length * 2);
25 for (int i = 0; i < digest.length; i++) {
26 res.append(Integer.toHexString((digest[i] & 0xF0) >> 4));
27 res.append(Integer.toHexString(digest[i] & 0xF));
29 return res.toString();
30 } catch (NoSuchAlgorithmException e) {
35 public static String hash(String password) {
36 return sha1(password);