001    package org.apache.fulcrum.crypto.provider;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.security.MessageDigest;
025    
026    import org.apache.fulcrum.crypto.CryptoAlgorithm;
027    import org.apache.fulcrum.crypto.impl.Base64;
028    
029    /**
030     * Implements the normal java.security.MessageDigest stream cipers.
031     * Base64 strings returned by this provider are correctly padded to
032     * multiples of four bytes. If you run into interoperability problems
033     * with other languages, especially perl and the Digest::MD5 module,
034     * note that the md5_base64 function from this package incorrectly drops
035     * the pad bytes. Use the MIME::Base64 package instead.
036     *
037     * If you upgrade from Turbine 2.1 and suddently your old stored passwords
038     * no longer work, please take a look at the OldJavaCrypt provider for
039     * bug-to-bug compatibility.
040     *
041     * This provider can be used as the default crypto algorithm provider.
042     *
043     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
044     * @version $Id: JavaCrypt.java 581797 2007-10-04 08:26:18Z sgoeschl $
045     */
046    
047    public class JavaCrypt
048        implements CryptoAlgorithm
049    {
050    
051        /** The default cipher */
052        public static final String DEFAULT_CIPHER = "SHA";
053    
054        /** The cipher to use for encryption */
055        private String cipher = null;
056    
057    
058        /**
059         * C'tor
060         *
061         */
062    
063        public JavaCrypt()
064        {
065            this.cipher = DEFAULT_CIPHER;
066        }
067    
068        /**
069         * Setting the actual cipher requested. If not
070         * called, then the default cipher (SHA) is used.
071         *
072         * This will never throw an error even if there is no
073         * provider for this cipher. The error will be thrown
074         * by encrypt() (Fixme?)
075         *
076         * @param cipher     The cipher to use.
077         *
078         */
079    
080        public void setCipher(String cipher)
081        {
082            this.cipher = cipher;
083        }
084    
085        /**
086         * This class never uses a seed, so this is
087         * just a dummy.
088         *
089         * @param seed        Seed (ignored)
090         *
091         */
092    
093        public void setSeed(String seed)
094        {
095            /* dummy */
096        }
097    
098        /**
099         * encrypt the supplied string with the requested cipher
100         *
101         * @param value       The value to be encrypted
102         *
103         * @return The encrypted value
104         *
105         * @throws Exception An Exception of the underlying implementation.
106         */
107    
108        public String encrypt(String value)
109            throws Exception
110        {
111            MessageDigest md = MessageDigest.getInstance(cipher);
112    
113            // We need to use unicode here, to be independent of platform's
114            // default encoding. Thanks to SGawin for spotting this.
115            byte[] digest = md.digest(value.getBytes("UTF-8"));
116    
117            // Base64-encode the digest.
118            byte[] encodedDigest = Base64.encodeBase64(digest);
119            return (encodedDigest == null ? null :
120                    new String(encodedDigest));
121        }
122    }