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 SHA-256 Hash from a given input <tt>source</tt> with an optional <tt>salt</tt> and hash iterations.
026     *
027     * <p>See the {@link AbstractHash AbstractHash} parent class JavaDoc for a detailed explanation of Hashing
028     * techniques and how the overloaded constructors function.
029     *
030     * <p><b>JDK Version Note</b> - Attempting to instantiate this class on JREs prior to version 1.4.0 will throw
031     * an {@link IllegalStateException IllegalStateException}
032     *
033     * @author Les Hazlewood
034     * @since 0.9
035     */
036    public class Sha256Hash extends AbstractHash {
037    
038        //TODO - complete JavaDoc
039    
040        public static final String ALGORITHM_NAME = "SHA-256";
041    
042        public Sha256Hash() {
043        }
044    
045        public Sha256Hash(Object source) {
046            super(source);
047        }
048    
049        public Sha256Hash(Object source, Object salt) {
050            super(source, salt);
051        }
052    
053        public Sha256Hash(Object source, Object salt, int hashIterations) {
054            super(source, salt, hashIterations);
055        }
056    
057        protected String getAlgorithmName() {
058            return ALGORITHM_NAME;
059        }
060    
061        public static Sha256Hash fromHexString(String hex) {
062            Sha256Hash hash = new Sha256Hash();
063            hash.setBytes(Hex.decode(hex));
064            return hash;
065        }
066    
067        public static Sha256Hash fromBase64String(String base64) {
068            Sha256Hash hash = new Sha256Hash();
069            hash.setBytes(Base64.decode(base64));
070            return hash;
071        }
072    
073    
074    }