1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8*9* http://www.apache.org/licenses/LICENSE-2.010*11* Unless required by applicable law or agreed to in writing, software12* distributed under the License is distributed on an "AS IS" BASIS,13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14* See the License for the specific language governing permissions and15* limitations under the License.16*/17packageorg.apache.jetspeed.security.spi.impl;
1819import java.security.MessageDigest;
20import java.security.NoSuchAlgorithmException;
2122import org.apache.commons.codec.binary.Base64;
23import org.apache.jetspeed.security.SecurityException;
24import org.apache.jetspeed.security.spi.CredentialPasswordEncoder;
2526/***27 * <p>28 * MessageDigestCredentialPasswordEncoder29 * </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 */34publicclassMessageDigestCredentialPasswordEncoder implements CredentialPasswordEncoder
35 {
36// Allow copying of encoded passwords or salt the digest with the userName preventing that37boolean simpleEncryption = false;
38 MessageDigest digester;
3940publicMessageDigestCredentialPasswordEncoder() throws NoSuchAlgorithmException
41 {
42this("SHA-1", false);
43 }
4445publicMessageDigestCredentialPasswordEncoder(boolean simpleEncryption) throws NoSuchAlgorithmException
46 {
47this("SHA-1", simpleEncryption);
48 }
4950publicMessageDigestCredentialPasswordEncoder(String algorithm) throws NoSuchAlgorithmException
51 {
52this(algorithm, false);
53 }
5455publicMessageDigestCredentialPasswordEncoder(String algorithm, boolean simpleEncryption) throws NoSuchAlgorithmException
56 {
57this.digester = MessageDigest.getInstance(algorithm);
58this.simpleEncryption = simpleEncryption;
59 }
6061public String getAlgorithm()
62 {
63return digester.getAlgorithm();
64 }
6566/***67 * @see org.apache.jetspeed.security.spi.CredentialPasswordEncoder#encode(java.lang.String, java.lang.String)68 */69public String encode(String userName, String clearTextPassword)
70 throws SecurityException
71 {
72 byte[] value;
73synchronized(digester)
74 {
75 digester.reset();
76 value = digester.digest(clearTextPassword.getBytes());
77if (!simpleEncryption)
78 {
79// don't allow copying of encoded passwords80 digester.update(userName.getBytes());
81 }
82 value = digester.digest(value);
83 }
84returnnew String(Base64.encodeBase64(value));
85 }
86 }