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.lang.util.ByteSource;
22
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import static java.util.Collections.unmodifiableMap;
28 import static java.util.Objects.requireNonNull;
29
30 /**
31 * Simple implementation of {@link HashRequest} that can be used when interacting with a {@link HashService}.
32 *
33 * @since 1.2
34 */
35 public class SimpleHashRequest implements HashRequest {
36
37 /**
38 * cannot be null - this is the source to hash.
39 */
40 private final ByteSource source;
41 /**
42 * can be null = no salt specified
43 */
44 private final ByteSource salt;
45 /**
46 * can be null = let the HashService decide.
47 */
48 private final String algorithmName;
49 private final Map<String, Object> parameters = new ConcurrentHashMap<>();
50
51 /**
52 * Creates a new SimpleHashRequest instance.
53 *
54 * @param algorithmName the name of the hash algorithm to use. This is often null as the
55 * {@link HashService} implementation is usually configured with an
56 * appropriate algorithm name, but this can be non-null
57 * if the hash service's algorithm should be overridden with a
58 * specific one for the duration of the request.
59 * @param source the source to be hashed
60 * @param salt any public salt which should be used when computing the hash
61 * @param parameters e.g. the number of hash iterations to execute or other parameters.
62 * @throws NullPointerException if {@code source} is null or empty or {@code parameters} is {@code null}.
63 */
64 public SimpleHashRequest(String algorithmName, ByteSource source, ByteSource salt, Map<String, Object> parameters) {
65 this.source = requireNonNull(source);
66 this.salt = salt;
67 this.algorithmName = algorithmName;
68 this.parameters.putAll(requireNonNull(parameters));
69 }
70
71 @Override
72 public ByteSource getSource() {
73 return this.source;
74 }
75
76 @Override
77 public Optional<ByteSource> getSalt() {
78 return Optional.ofNullable(this.salt);
79 }
80
81
82 @Override
83 public Optional<String> getAlgorithmName() {
84 return Optional.ofNullable(algorithmName);
85 }
86
87 @Override
88 public Map<String, Object> getParameters() {
89 return unmodifiableMap(this.parameters);
90 }
91 }