]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PasswordHash.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / util / PasswordHash.java
1 package org.cacert.gigi.util;
2
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
5
6 public class PasswordHash {
7         public static boolean verifyHash(String password, String hash) {
8                 String newhash = sha1(password);
9                 return newhash.equals(hash);
10         }
11
12         private static String sha1(String password) {
13                 try {
14                         MessageDigest md = MessageDigest.getInstance("SHA1");
15                         byte[] digest = md.digest(password.getBytes());
16                         StringBuffer res = new StringBuffer(digest.length * 2);
17                         for (int i = 0; i < digest.length; i++) {
18                                 res.append(Integer.toHexString((digest[i] & 0xF0) >> 4));
19                                 res.append(Integer.toHexString(digest[i] & 0xF));
20                         }
21                         return res.toString();
22                 } catch (NoSuchAlgorithmException e) {
23                         throw new Error(e);
24                 }
25         }
26
27         public static String hash(String password) {
28                 return sha1(password);
29         }
30 }