]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PasswordHash.java
[DB]: Add orgAdmin management code
[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
8     public static boolean verifyHash(String password, String hash) {
9         String newhash = sha1(password);
10         boolean match = true;
11         if (newhash.length() != hash.length()) {
12             match = false;
13         }
14         for (int i = 0; i < newhash.length(); i++) {
15             match &= newhash.charAt(i) == hash.charAt(i);
16         }
17         return match;
18     }
19
20     private static String sha1(String password) {
21         try {
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));
28             }
29             return res.toString();
30         } catch (NoSuchAlgorithmException e) {
31             throw new Error(e);
32         }
33     }
34
35     public static String hash(String password) {
36         return sha1(password);
37     }
38 }