001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.crypto.hash;
020    
021    import org.apache.shiro.codec.Base64;
022    import org.apache.shiro.codec.Hex;
023    
024    /**
025     * Generates an MD5 Hash (RFC 1321) from a given input <tt>source</tt> with an optional <tt>salt</tt> and
026     * hash iterations.
027     *
028     * <p>See the {@link AbstractHash AbstractHash} parent class JavaDoc for a detailed explanation of Hashing
029     * techniques and how the overloaded constructors function.
030     *
031     * @author Les Hazlewood
032     * @since 0.9
033     */
034    public class Md5Hash extends AbstractHash {
035    
036        //TODO - complete JavaDoc
037    
038        public static final String ALGORITHM_NAME = "MD5";
039    
040        public Md5Hash() {
041        }
042    
043        public Md5Hash(Object source) {
044            super(source);
045        }
046    
047        public Md5Hash(Object source, Object salt) {
048            super(source, salt);
049        }
050    
051        public Md5Hash(Object source, Object salt, int hashIterations) {
052            super(source, salt, hashIterations);
053        }
054    
055        protected String getAlgorithmName() {
056            return ALGORITHM_NAME;
057        }
058    
059        public static Md5Hash fromHexString(String hex) {
060            Md5Hash hash = new Md5Hash();
061            hash.setBytes(Hex.decode(hex));
062            return hash;
063        }
064    
065        public static Md5Hash fromBase64String(String base64) {
066            Md5Hash hash = new Md5Hash();
067            hash.setBytes(Base64.decode(base64));
068            return hash;
069        }
070    }