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.SimpleHashRequest;
20 
21 import hunt.shiro.crypto.hash.HashRequest;
22 
23 import hunt.shiro.util.ByteSource;
24 import hunt.Exceptions;
25 
26 import std.algorithm;
27 
28 /**
29  * Simple implementation of {@link HashRequest} that can be used when interacting with a {@link HashService}.
30  *
31  */
32 class SimpleHashRequest : HashRequest {
33 
34     private ByteSource source; //cannot be null - this is the source to hash.
35     private ByteSource salt; //null = no salt specified
36     private int iterations; //0 = not specified by the requestor; let the HashService decide.
37     private string algorithmName; //null = let the HashService decide.
38 
39     /**
40      * Creates a new SimpleHashRequest instance.
41      *
42      * @param algorithmName the name of the hash algorithm to use.  This is often null as the
43      * {@link HashService} implementation is usually configured with an appropriate algorithm name, but this
44      * can be non-null if the hash service's algorithm should be overridden with a specific one for the duration
45      * of the request.
46      *
47      * @param source the source to be hashed
48      * @param salt any public salt which should be used when computing the hash
49      * @param iterations the number of hash iterations to execute.  Zero (0) indicates no iterations were specified
50      * for the request, at which point the number of iterations is decided by the {@code HashService}
51      * @throws NullPointerException if {@code source} is null or empty.
52      */
53     this(string algorithmName, ByteSource source, ByteSource salt, int iterations) {
54         if (source  is null) {
55             throw new NullPointerException("source argument cannot be null");
56         }
57         this.source = source;
58         this.salt = salt;
59         this.algorithmName = algorithmName;
60         this.iterations = max(0, iterations);
61     }
62 
63     ByteSource getSource() {
64         return this.source;
65     }
66 
67     ByteSource getSalt() {
68         return this.salt;
69     }
70 
71     int getIterations() {
72         return iterations;
73     }
74 
75     string getAlgorithmName() {
76         return algorithmName;
77     }
78 }