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 /**
24 * A Cryptographic {@code Hash} represents a one-way conversion algorithm that transforms an input source to an
25 * underlying byte array. Hex and Base64-encoding output of the hashed bytes are automatically supported by the
26 * inherited {@link #toHex() toHex()} and {@link #toBase64() toBase64()} methods.
27 * <p/>
28 * The bytes returned by the parent interface's {@link #getBytes() getBytes()} are the hashed value of the
29 * original input source, also known as the 'checksum' or 'digest'.
30 *
31 * @see Sha256Hash
32 * @see Sha384Hash
33 * @see Sha512Hash
34 * @since 0.9
35 */
36 public interface Hash extends ByteSource {
37
38 /**
39 * Returns the name of the algorithm used to hash the input source, for example, {@code SHA-256}, {@code MD5}, etc.
40 * <p/>
41 * The name is expected to be a {@link java.security.MessageDigest MessageDigest} algorithm name.
42 *
43 * @return the the name of the algorithm used to hash the input source, for example, {@code SHA-256}, {@code MD5}, etc.
44 * @since 1.1
45 */
46 String getAlgorithmName();
47
48 /**
49 * Returns a salt used to compute the hash or {@code null} if no salt was used.
50 *
51 * @return a salt used to compute the hash or {@code null} if no salt was used.
52 * @since 1.2
53 */
54 ByteSource getSalt();
55
56 /**
57 * Returns the number of hash iterations used to compute the hash.
58 *
59 * @return the number of hash iterations used to compute the hash.
60 * @since 1.2
61 */
62 int getIterations();
63
64 /**
65 * Tests if a given passwords matches with this instance.
66 *
67 * <p>Usually implementations will re-create {@code this} but with the given plaintext bytes as secret.</p>
68 *
69 * @param plaintextBytes the plaintext bytes from a user.
70 * @return {@code true} if the given plaintext generates an equal hash with the same parameters as from this hash.
71 */
72 boolean matchesPassword(ByteSource plaintextBytes);
73 }