1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 module hunt.shiro.crypto.hash.Sha256Hash;
20 
21 import hunt.shiro.crypto.hash.SimpleHash;
22 
23 import hunt.shiro.codec.Base64;
24 import hunt.shiro.codec.Hex;
25 
26 /**
27  * Generates an SHA-256 Hash from a given input <tt>source</tt> with an optional <tt>salt</tt> and hash iterations.
28  * <p/>
29  * See the {@link SimpleHash SimpleHash} parent class JavaDoc for a detailed explanation of Hashing
30  * techniques and how the overloaded constructors function.
31  * <p/>
32  * <b>JDK Version Note</b> - Attempting to instantiate this class on JREs prior to version 1.4.0 will throw
33  * an {@link IllegalStateException IllegalStateException}
34  *
35  */
36 class Sha256Hash : SimpleHash {
37 
38     //TODO - complete JavaDoc
39 
40     enum string ALGORITHM_NAME = "SHA-256";
41 
42     this() {
43         super(ALGORITHM_NAME);
44     }
45 
46     this(Object source) {
47         super(ALGORITHM_NAME, source);
48     }
49 
50     this(Object source, Object salt) {
51         super(ALGORITHM_NAME, source, salt);
52     }
53 
54     this(Object source, Object salt, int hashIterations) {
55         super(ALGORITHM_NAME, source, salt, hashIterations);
56     }
57 
58     static Sha256Hash fromHexString(string hex) {
59         Sha256Hash hash = new Sha256Hash();
60         hash.setBytes(Hex.decode(hex));
61         return hash;
62     }
63 
64     static Sha256Hash fromBase64String(string base64) {
65         Sha256Hash hash = new Sha256Hash();
66         hash.setBytes(Base64.decode(base64));
67         return hash;
68     }
69 }