001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: MessageDigestCredentialPasswordEncoder.java,v 1.3 2007/03/27 21:59:44 mlipp Exp $
021: *
022: * $Log: MessageDigestCredentialPasswordEncoder.java,v $
023: * Revision 1.3 2007/03/27 21:59:44 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.2 2006/09/29 12:32:13 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.1 2006/09/19 14:12:08 drmlipp
030: * Separated portal app and modules.
031: *
032: * Revision 1.1 2006/06/26 20:55:25 mlipp
033: * Added JBoss compatible password encryption.
034: *
035: */
036: package de.danet.an.wfdemo.jsportalexts.security;
037:
038: import java.security.MessageDigest;
039: import java.security.NoSuchAlgorithmException;
040:
041: import org.apache.commons.codec.binary.Base64;
042: import org.apache.jetspeed.security.SecurityException;
043: import org.apache.jetspeed.security.spi.CredentialPasswordEncoder;
044:
045: /**
046: * A simpler password encoder matching the digesting method used by JBoss.
047: *
048: * @author mnl
049: *
050: */
051: public class MessageDigestCredentialPasswordEncoder implements
052: CredentialPasswordEncoder {
053:
054: /** The digester used. */
055: private MessageDigest digester;
056:
057: /**
058: * Create a new instance with all attributes initialized
059: * to defaults or the given values.
060: *
061: * @throws NoSuchAlgorithmException if the default algorithm
062: * cannot be initialized
063: */
064: public MessageDigestCredentialPasswordEncoder()
065: throws NoSuchAlgorithmException {
066: this ("SHA-1");
067: }
068:
069: /**
070: * Create a new instance with all attributes initialized
071: * to defaults or the given values.
072: *
073: * @param algorithm the algorithm to use
074: * @throws NoSuchAlgorithmException if the given algorithm
075: * cannot be initialized
076: */
077: public MessageDigestCredentialPasswordEncoder(String algorithm)
078: throws NoSuchAlgorithmException {
079: this .digester = MessageDigest.getInstance(algorithm);
080: }
081:
082: /**
083: * @return the algorithm
084: */
085: public String getAlgorithm() {
086: return digester.getAlgorithm();
087: }
088:
089: /**
090: * Encode the user name.
091: * @param userName the user name
092: * @param clearTextPassword the password
093: * @throws SecurityException if an error occurs
094: * @return the encoded password
095: * @see org.apache.jetspeed.security.spi.CredentialPasswordEncoder#encode
096: */
097: public String encode(String userName, String clearTextPassword)
098: throws SecurityException {
099: byte[] value;
100: synchronized (digester) {
101: digester.reset();
102: value = digester.digest(clearTextPassword.getBytes());
103: }
104: return new String(Base64.encodeBase64(value));
105: }
106: }
|