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