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.impl;
1819import java.io.ByteArrayOutputStream;
20import java.io.OutputStream;
21import java.security.MessageDigest;
2223import javax.mail.internet.MimeUtility;
2425import org.apache.jetspeed.security.SecurityException;
26import org.apache.jetspeed.security.spi.CredentialPasswordEncoder;
2728publicclass Jetspeed1CredentialPasswordEncoder implements
29 CredentialPasswordEncoder {
3031protected String passwordsAlgorithm = "SHA";
32protected String encodingMethod = "base64";
3334// We don't need the constructors to do anything, but it crashes if we35// don't provide them.36/*37 public Jetspeed1CredentialPasswordEncoder() {}38 public Jetspeed1CredentialPasswordEncoder(boolean dummy) {}39 public Jetspeed1CredentialPasswordEncoder(String algorithm) 40 {41 this.passwordsAlgorithm = algorithm;42 }4344 public Jetspeed1CredentialPasswordEncoder(boolean dummy1, String dummy2) {}45 */4647public Jetspeed1CredentialPasswordEncoder()
48 {
49this("SHA", "base64");
50 }
5152public Jetspeed1CredentialPasswordEncoder( String algorithm )
53 {
54this(algorithm, "base64");
55 }
5657public Jetspeed1CredentialPasswordEncoder( String algorithm, String encoding )
58 {
59this.passwordsAlgorithm = algorithm;
60this.encodingMethod = encoding;
61 }
6263public String encode(String userName, String clearTextPassword)
64 throws SecurityException {
65try66 {
67 MessageDigest md = MessageDigest.getInstance(passwordsAlgorithm);
68// We need to use unicode here, to be independent of platform's69// default encoding. Thanks to SGawin for spotting this.70 byte[] digest = md.digest(clearTextPassword.getBytes("UTF-8"));
71 ByteArrayOutputStream bas = new ByteArrayOutputStream(digest.length + digest.length / 3 + 1);
72 OutputStream encodedStream = MimeUtility.encode(bas, "base64");
73 encodedStream.write(digest);
74 encodedStream.flush();
75 encodedStream.close();
76return bas.toString();
77 }
78catch( Exception e )
79 {
80//logger.error("Unable to encrypt password."+e.getMessage(), e);81returnnull;
82 }
83 }
8485 }