]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PasswordHash.java
d6b0b9066b7ab11ec72177e883f1156227fc3ce7
[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 import com.lambdaworks.crypto.SCryptUtil;
7
8 public class PasswordHash {
9
10     public static boolean verifyHash(String password, String hash) {
11         if (hash.contains("$")) {
12             return SCryptUtil.check(password, hash);
13         }
14         String newhash = sha1(password);
15         boolean match = true;
16         if (newhash.length() != hash.length()) {
17             match = false;
18         }
19         for (int i = 0; i < newhash.length(); i++) {
20             match &= newhash.charAt(i) == hash.charAt(i);
21         }
22         return match;
23     }
24
25     private static String sha1(String password) {
26         try {
27             MessageDigest md = MessageDigest.getInstance("SHA1");
28             byte[] digest = md.digest(password.getBytes());
29             StringBuffer res = new StringBuffer(digest.length * 2);
30             for (int i = 0; i < digest.length; i++) {
31                 res.append(Integer.toHexString((digest[i] & 0xF0) >> 4));
32                 res.append(Integer.toHexString(digest[i] & 0xF));
33             }
34             return res.toString();
35         } catch (NoSuchAlgorithmException e) {
36             throw new Error(e);
37         }
38     }
39
40     public static String hash(String password) {
41         return SCryptUtil.scrypt(password, 1 << 14, 8, 1);
42     }
43 }