1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.spi.impl;
18
19 import java.security.MessageDigest;
20 import java.security.NoSuchAlgorithmException;
21
22 import org.apache.commons.codec.binary.Base64;
23 import org.apache.jetspeed.security.SecurityException;
24 import org.apache.jetspeed.security.spi.CredentialPasswordEncoder;
25
26 /***
27 * <p>
28 * MessageDigestCredentialPasswordEncoder
29 * </p>
30 *
31 * @author <a href="mailto:ate@apache.org">Ate Douma</a>
32 * @version $Id: MessageDigestCredentialPasswordEncoder.java 516448 2007-03-09 16:25:47Z ate $
33 */
34 public class MessageDigestCredentialPasswordEncoder implements CredentialPasswordEncoder
35 {
36
37 boolean simpleEncryption = false;
38 MessageDigest digester;
39
40 public MessageDigestCredentialPasswordEncoder() throws NoSuchAlgorithmException
41 {
42 this("SHA-1", false);
43 }
44
45 public MessageDigestCredentialPasswordEncoder(boolean simpleEncryption) throws NoSuchAlgorithmException
46 {
47 this("SHA-1", simpleEncryption);
48 }
49
50 public MessageDigestCredentialPasswordEncoder(String algorithm) throws NoSuchAlgorithmException
51 {
52 this(algorithm, false);
53 }
54
55 public MessageDigestCredentialPasswordEncoder(String algorithm, boolean simpleEncryption) throws NoSuchAlgorithmException
56 {
57 this.digester = MessageDigest.getInstance(algorithm);
58 this.simpleEncryption = simpleEncryption;
59 }
60
61 public String getAlgorithm()
62 {
63 return digester.getAlgorithm();
64 }
65
66 /***
67 * @see org.apache.jetspeed.security.spi.CredentialPasswordEncoder#encode(java.lang.String, java.lang.String)
68 */
69 public String encode(String userName, String clearTextPassword)
70 throws SecurityException
71 {
72 byte[] value;
73 synchronized(digester)
74 {
75 digester.reset();
76 value = digester.digest(clearTextPassword.getBytes());
77 if (!simpleEncryption)
78 {
79
80 digester.update(userName.getBytes());
81 }
82 value = digester.digest(value);
83 }
84 return new String(Base64.encodeBase64(value));
85 }
86 }