View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.logic;
20  
21  import java.security.MessageDigest;
22  import java.security.NoSuchAlgorithmException;
23  
24  import org.apache.commons.codec.binary.Base64;
25  import org.promotego.exceptions.NestedException;
26  import org.promotego.interfaces.PasswordHashTool;
27  
28  public class Sha1Hash implements PasswordHashTool
29  {
30      public boolean match(String cryptedPassword, String password)
31      {
32          return hash(password).equals(cryptedPassword);
33      }
34  
35  	public String hash(String password)
36  	{
37  		MessageDigest sha1Digest;
38  		
39  		try
40  		{
41  			sha1Digest = MessageDigest.getInstance("SHA-1");
42  		}
43  		catch (NoSuchAlgorithmException e)
44  		{
45  			// This should never happen, but it's best to propagate it
46  			throw NestedException.wrap(e);
47  		}
48  		
49  		sha1Digest.update(password.getBytes());
50  		
51  		StringBuilder retval = new StringBuilder("sha1:");
52  		retval.append(new String(Base64.encodeBase64(sha1Digest.digest())));
53  		return retval.toString();
54  	}
55  }