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 */
019package org.apache.shiro.crypto.hash;
020
021import org.apache.shiro.codec.Base64;
022import 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 * <p/>
027 * See the {@link SimpleHash SimpleHash} parent class JavaDoc for a detailed explanation of Hashing
028 * techniques and how the overloaded constructors function.
029 * <p/>
030 * <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 * @since 0.9
034 */
035public class Sha256Hash extends SimpleHash {
036
037    //TODO - complete JavaDoc
038
039    public static final String ALGORITHM_NAME = "SHA-256";
040
041    public Sha256Hash() {
042        super(ALGORITHM_NAME);
043    }
044
045    public Sha256Hash(Object source) {
046        super(ALGORITHM_NAME, source);
047    }
048
049    public Sha256Hash(Object source, Object salt) {
050        super(ALGORITHM_NAME, source, salt);
051    }
052
053    public Sha256Hash(Object source, Object salt, int hashIterations) {
054        super(ALGORITHM_NAME, source, salt, hashIterations);
055    }
056
057    public static Sha256Hash fromHexString(String hex) {
058        Sha256Hash hash = new Sha256Hash();
059        hash.setBytes(Hex.decode(hex));
060        return hash;
061    }
062
063    public static Sha256Hash fromBase64String(String base64) {
064        Sha256Hash hash = new Sha256Hash();
065        hash.setBytes(Base64.decode(base64));
066        return hash;
067    }
068}