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.authc.credential.HashingPasswordService;
20 
21 import hunt.shiro.authc.credential.PasswordService;
22 
23 import hunt.shiro.crypto.hash.Hash;
24 import hunt.shiro.util.ByteSource;
25 
26 /**
27  * A {@code HashingPasswordService} is a {@link PasswordService} that performs password encryption and comparisons
28  * based on cryptographic {@link Hash}es.
29  *
30  */
31 interface HashingPasswordService : PasswordService {
32 
33     /**
34      * Hashes the specified plaintext password using internal hashing configuration settings pertinent to password
35      * hashing.
36      * <p/>
37      * Note
38      * that this method is only likely to be used in more complex environments that wish to format and/or save the
39      * returned {@code Hash} object in a custom manner.  Most applications will find the
40      * {@link #encryptPassword(Object) encryptPassword} method suitable enough for safety
41      * and ease-of-use.
42      * <h3>Usage</h3>
43      * The input argument type can be any 'byte backed' {@code Object} - almost always either a
44      * string or character array representing passwords (character arrays are often a safer way to represent passwords
45      * as they can be cleared/nulled-out after use.  Any argument type supported by
46      * {@link ByteSourceUtil#isCompatible(Object)} is valid.
47      * <p/>
48      * Regardless of your choice of using Strings or character arrays to represent submitted passwords, you can wrap
49      * either as a {@code ByteSource} by using {@link ByteSourceUtil}, for example, when the passwords are captured as
50      * Strings:
51      * <pre>
52      * ByteSource passwordBytes = ByteSourceUtil.bytes(submittedPasswordString);
53      * Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
54      * </pre>
55      * or, identically, when captured as a character array:
56      * <pre>
57      * ByteSource passwordBytes = ByteSourceUtil.bytes(submittedPasswordCharacterArray);
58      * Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
59      * </pre>
60      *
61      * @param plaintext the raw password as 'byte-backed' object (string, character array, {@link ByteSource},
62      *                  etc) usually acquired from your application's 'new user' or 'password reset' workflow.
63      * @return the hashed password.
64      * @throws IllegalArgumentException if the argument cannot be easily converted to bytes as defined by
65      *                                  {@link ByteSourceUtil#isCompatible(Object)}.
66      * @see ByteSourceUtil#isCompatible(Object)
67      * @see #encryptPassword(Object)
68      */
69     Hash hashPassword(Object plaintext);
70 
71     /**
72      * Returns {@code true} if the {@code submittedPlaintext} password matches the existing {@code savedPasswordHash},
73      * {@code false} otherwise.  Note that this method is only likely to be used in more complex environments that
74      * save hashes in a custom manner.  Most applications will find the
75      * {@link #passwordsMatch(Object, string) passwordsMatch(plaintext,string)} method
76      * sufficient if {@link #encryptPassword(Object) encrypting passwords as Strings}.
77      * <h3>Usage</h3>
78      * The {@code submittedPlaintext} argument type can be any 'byte backed' {@code Object} - almost always either a
79      * string or character array representing passwords (character arrays are often a safer way to represent passwords
80      * as they can be cleared/nulled-out after use.  Any argument type supported by
81      * {@link ByteSourceUtil#isCompatible(Object)} is valid.
82      *
83      * @param plaintext a raw/plaintext password submitted by an end user/Subject.
84      * @param savedPasswordHash  the previously hashed password known to be associated with an account.
85      *                           This value is expected to have been previously generated from the
86      *                           {@link #hashPassword(Object) hashPassword} method (typically
87      *                           when the account is created or the account's password is reset).
88      * @return {@code true} if the {@code plaintext} password matches the existing {@code savedPasswordHash},
89      *         {@code false} otherwise.
90      */
91     bool passwordsMatch(Object plaintext, Hash savedPasswordHash);
92 }