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.lang.codec.Base64; 022import org.apache.shiro.lang.codec.Hex; 023 024/** 025 * Generates an SHA-512 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 Sha512Hash extends SimpleHash { 036 037 /** 038 * Sha512 algorithm name. 039 */ 040 public static final String ALGORITHM_NAME = "SHA-512"; 041 042 public Sha512Hash() { 043 super(ALGORITHM_NAME); 044 } 045 046 public Sha512Hash(Object source) { 047 super(ALGORITHM_NAME, source); 048 } 049 050 public Sha512Hash(Object source, Object salt) { 051 super(ALGORITHM_NAME, source, salt); 052 } 053 054 public Sha512Hash(Object source, Object salt, int hashIterations) { 055 super(ALGORITHM_NAME, source, salt, hashIterations); 056 } 057 058 public static Sha512Hash fromHexString(String hex) { 059 Sha512Hash hash = new Sha512Hash(); 060 hash.setBytes(Hex.decode(hex)); 061 return hash; 062 } 063 064 public static Sha512Hash fromBase64String(String base64) { 065 Sha512Hash hash = new Sha512Hash(); 066 hash.setBytes(Base64.decode(base64)); 067 return hash; 068 } 069} 070