Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2013 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
package edu.ucsb.nceas.metacat.authentication;
22

    
23
import org.mindrot.jbcrypt.BCrypt;
24

    
25

    
26
/**
27
 * A class to use the BCryptHash algorithm to generate the hash. This is a recommended way
28
 * to protect password.
29
 * @author tao
30
 *
31
 */
32
public class AuthFileBCryptHash implements AuthFileHashInterface {
33
    
34
    /**
35
     * Default Constructor
36
     */
37
    public AuthFileBCryptHash() {
38
        
39
    }
40
    
41
    @Override
42
    public boolean match(String plain, String hashed) throws Exception {
43
        if(plain == null || plain.trim().equals("")) {
44
            throw new IllegalArgumentException("AuthFileBrryptHash.match - the password parameter can't be null or blank");   
45
        }
46
        if(hashed == null || hashed.trim().equals("")) {
47
            throw new IllegalArgumentException("AuthFileBrryptHash.match - the hashed value of password parameter can't be null or blank");
48
        }
49
        return BCrypt.checkpw(plain, hashed);
50
    }
51
    
52
    @Override
53
    public String hash(String plain) {
54
        if(plain == null || plain.trim().equals("")) {
55
            throw new IllegalArgumentException("AuthFileBrryptHash.hash - the password parameter can't be null or blank");   
56
        }
57
        return BCrypt.hashpw(plain, BCrypt.gensalt());
58
    }
59
    
60
}
(2-2/4)