blob: b59bab43318269dd904d1897c3e68591d5dae3d7 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one</a>
<span class="sourceLineNo">003</span><a id="line.3"> * or more contributor license agreements. See the NOTICE file</a>
<span class="sourceLineNo">004</span><a id="line.4"> * distributed with this work for additional information</a>
<span class="sourceLineNo">005</span><a id="line.5"> * regarding copyright ownership. The ASF licenses this file</a>
<span class="sourceLineNo">006</span><a id="line.6"> * to you under the Apache License, Version 2.0 (the</a>
<span class="sourceLineNo">007</span><a id="line.7"> * "License"); you may not use this file except in compliance</a>
<span class="sourceLineNo">008</span><a id="line.8"> * with the License. You may obtain a copy of the License at</a>
<span class="sourceLineNo">009</span><a id="line.9"> *</a>
<span class="sourceLineNo">010</span><a id="line.10"> * http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">011</span><a id="line.11"> *</a>
<span class="sourceLineNo">012</span><a id="line.12"> * Unless required by applicable law or agreed to in writing,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * software distributed under the License is distributed on an</a>
<span class="sourceLineNo">014</span><a id="line.14"> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</a>
<span class="sourceLineNo">015</span><a id="line.15"> * KIND, either express or implied. See the License for the</a>
<span class="sourceLineNo">016</span><a id="line.16"> * specific language governing permissions and limitations</a>
<span class="sourceLineNo">017</span><a id="line.17"> * under the License.</a>
<span class="sourceLineNo">018</span><a id="line.18"> */</a>
<span class="sourceLineNo">019</span><a id="line.19">package org.apache.shiro.authc.credential;</a>
<span class="sourceLineNo">020</span><a id="line.20"></a>
<span class="sourceLineNo">021</span><a id="line.21">import org.apache.shiro.authc.AuthenticationInfo;</a>
<span class="sourceLineNo">022</span><a id="line.22">import org.apache.shiro.authc.AuthenticationToken;</a>
<span class="sourceLineNo">023</span><a id="line.23">import org.apache.shiro.authc.SaltedAuthenticationInfo;</a>
<span class="sourceLineNo">024</span><a id="line.24">import org.apache.shiro.codec.Base64;</a>
<span class="sourceLineNo">025</span><a id="line.25">import org.apache.shiro.codec.Hex;</a>
<span class="sourceLineNo">026</span><a id="line.26">import org.apache.shiro.crypto.hash.AbstractHash;</a>
<span class="sourceLineNo">027</span><a id="line.27">import org.apache.shiro.crypto.hash.Hash;</a>
<span class="sourceLineNo">028</span><a id="line.28">import org.apache.shiro.crypto.hash.SimpleHash;</a>
<span class="sourceLineNo">029</span><a id="line.29">import org.apache.shiro.util.StringUtils;</a>
<span class="sourceLineNo">030</span><a id="line.30"></a>
<span class="sourceLineNo">031</span><a id="line.31">/**</a>
<span class="sourceLineNo">032</span><a id="line.32"> * A {@code HashedCredentialMatcher} provides support for hashing of supplied {@code AuthenticationToken} credentials</a>
<span class="sourceLineNo">033</span><a id="line.33"> * before being compared to those in the {@code AuthenticationInfo} from the data store.</a>
<span class="sourceLineNo">034</span><a id="line.34"> * &lt;p/&gt;</a>
<span class="sourceLineNo">035</span><a id="line.35"> * Credential hashing is one of the most common security techniques when safeguarding a user's private credentials</a>
<span class="sourceLineNo">036</span><a id="line.36"> * (passwords, keys, etc). Most developers never want to store their users' credentials in plain form, viewable by</a>
<span class="sourceLineNo">037</span><a id="line.37"> * anyone, so they often hash the users' credentials before they are saved in the data store.</a>
<span class="sourceLineNo">038</span><a id="line.38"> * &lt;p/&gt;</a>
<span class="sourceLineNo">039</span><a id="line.39"> * This class (and its subclasses) function as follows:</a>
<span class="sourceLineNo">040</span><a id="line.40"> * &lt;ol&gt;</a>
<span class="sourceLineNo">041</span><a id="line.41"> * &lt;li&gt;Hash the {@code AuthenticationToken} credentials supplied by the user during their login.&lt;/li&gt;</a>
<span class="sourceLineNo">042</span><a id="line.42"> * &lt;li&gt;Compare this hashed value directly with the {@code AuthenticationInfo} credentials stored in the system</a>
<span class="sourceLineNo">043</span><a id="line.43"> * (the stored account credentials are expected to already be in hashed form).&lt;/li&gt;</a>
<span class="sourceLineNo">044</span><a id="line.44"> * &lt;li&gt;If these two values are {@link #equals(Object, Object) equal}, the submitted credentials match, otherwise</a>
<span class="sourceLineNo">045</span><a id="line.45"> * they do not.&lt;/li&gt;</a>
<span class="sourceLineNo">046</span><a id="line.46"> * &lt;/ol&gt;</a>
<span class="sourceLineNo">047</span><a id="line.47"> * &lt;h2&gt;Salting and Multiple Hash Iterations&lt;/h2&gt;</a>
<span class="sourceLineNo">048</span><a id="line.48"> * Because simple hashing is usually not good enough for secure applications, this class also supports 'salting'</a>
<span class="sourceLineNo">049</span><a id="line.49"> * and multiple hash iterations. Please read this excellent</a>
<span class="sourceLineNo">050</span><a id="line.50"> * &lt;a href="http://www.owasp.org/index.php/Hashing_Java" _target="blank"&gt;Hashing Java article&lt;/a&gt; to learn about</a>
<span class="sourceLineNo">051</span><a id="line.51"> * salting and multiple iterations and why you might want to use them. (Note of sections 5</a>
<span class="sourceLineNo">052</span><a id="line.52"> * &amp;quot;Why add salt?&amp;quot; and 6 "Hardening against the attacker's attack"). We should also note here that all of</a>
<span class="sourceLineNo">053</span><a id="line.53"> * Shiro's Hash implementations (for example, {@link org.apache.shiro.crypto.hash.Md5Hash Md5Hash},</a>
<span class="sourceLineNo">054</span><a id="line.54"> * {@link org.apache.shiro.crypto.hash.Sha1Hash Sha1Hash}, etc) support salting and multiple hash iterations via</a>
<span class="sourceLineNo">055</span><a id="line.55"> * overloaded constructors.</a>
<span class="sourceLineNo">056</span><a id="line.56"> * &lt;h4&gt;Real World Case Study&lt;/h4&gt;</a>
<span class="sourceLineNo">057</span><a id="line.57"> * In April 2010, some public Atlassian Jira and Confluence</a>
<span class="sourceLineNo">058</span><a id="line.58"> * installations (Apache Software Foundation, Codehaus, etc) were the target of account attacks and user accounts</a>
<span class="sourceLineNo">059</span><a id="line.59"> * were compromised. The reason? Jira and Confluence at the time did not salt user passwords and attackers were</a>
<span class="sourceLineNo">060</span><a id="line.60"> * able to use dictionary attacks to compromise user accounts (Atlassian has since</a>
<span class="sourceLineNo">061</span><a id="line.61"> * &lt;a href="http://blogs.atlassian.com/news/2010/04/oh_man_what_a_day_an_update_on_our_security_breach.html"&gt;</a>
<span class="sourceLineNo">062</span><a id="line.62"> * fixed the problem&lt;/a&gt; of course).</a>
<span class="sourceLineNo">063</span><a id="line.63"> * &lt;p/&gt;</a>
<span class="sourceLineNo">064</span><a id="line.64"> * The lesson?</a>
<span class="sourceLineNo">065</span><a id="line.65"> * &lt;p/&gt;</a>
<span class="sourceLineNo">066</span><a id="line.66"> * &lt;b&gt;ALWAYS, ALWAYS, ALWAYS SALT USER PASSWORDS!&lt;/b&gt;</a>
<span class="sourceLineNo">067</span><a id="line.67"> * &lt;p/&gt;</a>
<span class="sourceLineNo">068</span><a id="line.68"> * &lt;h3&gt;Salting&lt;/h3&gt;</a>
<span class="sourceLineNo">069</span><a id="line.69"> * Prior to Shiro 1.1, salts could be obtained based on the end-user submitted</a>
<span class="sourceLineNo">070</span><a id="line.70"> * {@link AuthenticationToken AuthenticationToken} via the now-deprecated</a>
<span class="sourceLineNo">071</span><a id="line.71"> * {@link #getSalt(org.apache.shiro.authc.AuthenticationToken) getSalt(AuthenticationToken)} method. This however</a>
<span class="sourceLineNo">072</span><a id="line.72"> * could constitute a security hole since ideally salts should never be obtained based on what a user can submit.</a>
<span class="sourceLineNo">073</span><a id="line.73"> * User-submitted salt mechanisms are &lt;em&gt;much&lt;/em&gt; more susceptible to dictionary attacks and &lt;b&gt;SHOULD NOT&lt;/b&gt; be</a>
<span class="sourceLineNo">074</span><a id="line.74"> * used in secure systems. Instead salts should ideally be a secure randomly-generated number that is generated when</a>
<span class="sourceLineNo">075</span><a id="line.75"> * the user account is created. The secure number should never be disseminated to the user and always kept private</a>
<span class="sourceLineNo">076</span><a id="line.76"> * by the application.</a>
<span class="sourceLineNo">077</span><a id="line.77"> * &lt;h4&gt;Shiro 1.1&lt;/h4&gt;</a>
<span class="sourceLineNo">078</span><a id="line.78"> * As of Shiro 1.1, it is expected that any salt used to hash the submitted credentials will be obtained from the</a>
<span class="sourceLineNo">079</span><a id="line.79"> * stored account information (represented as an {@link AuthenticationInfo AuthenticationInfo} instance). This is much</a>
<span class="sourceLineNo">080</span><a id="line.80"> * more secure because the salt value remains private to the application (Shiro will never store this value).</a>
<span class="sourceLineNo">081</span><a id="line.81"> * &lt;p/&gt;</a>
<span class="sourceLineNo">082</span><a id="line.82"> * To enable this, {@code Realm}s should return {@link SaltedAuthenticationInfo SaltedAuthenticationInfo} instances</a>
<span class="sourceLineNo">083</span><a id="line.83"> * during authentication. {@code HashedCredentialsMatcher} implementations will then use the provided</a>
<span class="sourceLineNo">084</span><a id="line.84"> * {@link org.apache.shiro.authc.SaltedAuthenticationInfo#getCredentialsSalt credentialsSalt} for hashing. To avoid</a>
<span class="sourceLineNo">085</span><a id="line.85"> * security risks,</a>
<span class="sourceLineNo">086</span><a id="line.86"> * &lt;b&gt;it is highly recommended that any existing {@code Realm} implementations that support hashed credentials are</a>
<span class="sourceLineNo">087</span><a id="line.87"> * updated to return {@link SaltedAuthenticationInfo SaltedAuthenticationInfo} instances as soon as possible&lt;/b&gt;.</a>
<span class="sourceLineNo">088</span><a id="line.88"> * &lt;h4&gt;Shiro 1.0 Backwards Compatibility&lt;/h4&gt;</a>
<span class="sourceLineNo">089</span><a id="line.89"> * Because of the identified security risk, {@code Realm} implementations that support credentials hashing should</a>
<span class="sourceLineNo">090</span><a id="line.90"> * be updated to return {@link SaltedAuthenticationInfo SaltedAuthenticationInfo} instances as</a>
<span class="sourceLineNo">091</span><a id="line.91"> * soon as possible.</a>
<span class="sourceLineNo">092</span><a id="line.92"> * &lt;p/&gt;</a>
<span class="sourceLineNo">093</span><a id="line.93"> * If this is not possible for some reason, this class will retain 1.0 backwards-compatible behavior of obtaining</a>
<span class="sourceLineNo">094</span><a id="line.94"> * the salt via the now-deprecated {@link #getSalt(AuthenticationToken) getSalt(AuthenticationToken)} method. This</a>
<span class="sourceLineNo">095</span><a id="line.95"> * method will only be invoked if a {@code Realm} &lt;em&gt;does not&lt;/em&gt; return</a>
<span class="sourceLineNo">096</span><a id="line.96"> * {@link SaltedAuthenticationInfo SaltedAutenticationInfo} instances and {@link #isHashSalted() hashSalted} is</a>
<span class="sourceLineNo">097</span><a id="line.97"> * {@code true}.</a>
<span class="sourceLineNo">098</span><a id="line.98"> * But please note that the {@link #isHashSalted() hashSalted} property and the</a>
<span class="sourceLineNo">099</span><a id="line.99"> * {@link #getSalt(AuthenticationToken) getSalt(AuthenticationToken)} methods will be removed before the Shiro 2.0</a>
<span class="sourceLineNo">100</span><a id="line.100"> * release.</a>
<span class="sourceLineNo">101</span><a id="line.101"> * &lt;h3&gt;Multiple Hash Iterations&lt;/h3&gt;</a>
<span class="sourceLineNo">102</span><a id="line.102"> * If you hash your users' credentials multiple times before persisting to the data store, you will also need to</a>
<span class="sourceLineNo">103</span><a id="line.103"> * set this class's {@link #setHashIterations(int) hashIterations} property. See the</a>
<span class="sourceLineNo">104</span><a id="line.104"> * &lt;a href="http://www.owasp.org/index.php/Hashing_Java" _target="blank"&gt;Hashing Java article&lt;/a&gt;'s</a>
<span class="sourceLineNo">105</span><a id="line.105"> * &lt;a href="http://www.owasp.org/index.php/Hashing_Java#Hardening_against_the_attacker.27s_attack"&gt;</a>
<span class="sourceLineNo">106</span><a id="line.106"> * &amp;quot;Hardening against the attacker's attack&amp;quot;&lt;/a&gt; section to learn more about why you might want to use</a>
<span class="sourceLineNo">107</span><a id="line.107"> * multiple hash iterations.</a>
<span class="sourceLineNo">108</span><a id="line.108"> * &lt;h2&gt;MD5 &amp;amp; SHA-1 Notice&lt;/h2&gt;</a>
<span class="sourceLineNo">109</span><a id="line.109"> * &lt;a href="http://en.wikipedia.org/wiki/MD5"&gt;MD5&lt;/a&gt; and</a>
<span class="sourceLineNo">110</span><a id="line.110"> * &lt;a href="http://en.wikipedia.org/wiki/SHA_hash_functions"&gt;SHA-1&lt;/a&gt; algorithms are now known to be vulnerable to</a>
<span class="sourceLineNo">111</span><a id="line.111"> * compromise and/or collisions (read the linked pages for more). While most applications are ok with either of these</a>
<span class="sourceLineNo">112</span><a id="line.112"> * two, if your application mandates high security, use the SHA-256 (or higher) hashing algorithms and their</a>
<span class="sourceLineNo">113</span><a id="line.113"> * supporting {@code CredentialsMatcher} implementations.</a>
<span class="sourceLineNo">114</span><a id="line.114"> *</a>
<span class="sourceLineNo">115</span><a id="line.115"> * @see org.apache.shiro.crypto.hash.Md5Hash</a>
<span class="sourceLineNo">116</span><a id="line.116"> * @see org.apache.shiro.crypto.hash.Sha1Hash</a>
<span class="sourceLineNo">117</span><a id="line.117"> * @see org.apache.shiro.crypto.hash.Sha256Hash</a>
<span class="sourceLineNo">118</span><a id="line.118"> * @since 0.9</a>
<span class="sourceLineNo">119</span><a id="line.119"> */</a>
<span class="sourceLineNo">120</span><a id="line.120">public class HashedCredentialsMatcher extends SimpleCredentialsMatcher {</a>
<span class="sourceLineNo">121</span><a id="line.121"></a>
<span class="sourceLineNo">122</span><a id="line.122"> /**</a>
<span class="sourceLineNo">123</span><a id="line.123"> * @since 1.1</a>
<span class="sourceLineNo">124</span><a id="line.124"> */</a>
<span class="sourceLineNo">125</span><a id="line.125"> private String hashAlgorithm;</a>
<span class="sourceLineNo">126</span><a id="line.126"> private int hashIterations;</a>
<span class="sourceLineNo">127</span><a id="line.127"> private boolean hashSalted;</a>
<span class="sourceLineNo">128</span><a id="line.128"> private boolean storedCredentialsHexEncoded;</a>
<span class="sourceLineNo">129</span><a id="line.129"></a>
<span class="sourceLineNo">130</span><a id="line.130"> /**</a>
<span class="sourceLineNo">131</span><a id="line.131"> * JavaBeans-compatible no-arg constructor intended for use in IoC/Dependency Injection environments. If you</a>
<span class="sourceLineNo">132</span><a id="line.132"> * use this constructor, you &lt;em&gt;MUST&lt;/em&gt; also additionally set the</a>
<span class="sourceLineNo">133</span><a id="line.133"> * {@link #setHashAlgorithmName(String) hashAlgorithmName} property.</a>
<span class="sourceLineNo">134</span><a id="line.134"> */</a>
<span class="sourceLineNo">135</span><a id="line.135"> public HashedCredentialsMatcher() {</a>
<span class="sourceLineNo">136</span><a id="line.136"> this.hashAlgorithm = null;</a>
<span class="sourceLineNo">137</span><a id="line.137"> this.hashSalted = false;</a>
<span class="sourceLineNo">138</span><a id="line.138"> this.hashIterations = 1;</a>
<span class="sourceLineNo">139</span><a id="line.139"> this.storedCredentialsHexEncoded = true; //false means Base64-encoded</a>
<span class="sourceLineNo">140</span><a id="line.140"> }</a>
<span class="sourceLineNo">141</span><a id="line.141"></a>
<span class="sourceLineNo">142</span><a id="line.142"> /**</a>
<span class="sourceLineNo">143</span><a id="line.143"> * Creates an instance using the specified {@link #getHashAlgorithmName() hashAlgorithmName} to hash submitted</a>
<span class="sourceLineNo">144</span><a id="line.144"> * credentials.</a>
<span class="sourceLineNo">145</span><a id="line.145"> * @param hashAlgorithmName the {@code Hash} {@link org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName}</a>
<span class="sourceLineNo">146</span><a id="line.146"> * to use when performing hashes for credentials matching.</a>
<span class="sourceLineNo">147</span><a id="line.147"> * @since 1.1</a>
<span class="sourceLineNo">148</span><a id="line.148"> */</a>
<span class="sourceLineNo">149</span><a id="line.149"> public HashedCredentialsMatcher(String hashAlgorithmName) {</a>
<span class="sourceLineNo">150</span><a id="line.150"> this();</a>
<span class="sourceLineNo">151</span><a id="line.151"> if (!StringUtils.hasText(hashAlgorithmName) ) {</a>
<span class="sourceLineNo">152</span><a id="line.152"> throw new IllegalArgumentException("hashAlgorithmName cannot be null or empty.");</a>
<span class="sourceLineNo">153</span><a id="line.153"> }</a>
<span class="sourceLineNo">154</span><a id="line.154"> this.hashAlgorithm = hashAlgorithmName;</a>
<span class="sourceLineNo">155</span><a id="line.155"> }</a>
<span class="sourceLineNo">156</span><a id="line.156"></a>
<span class="sourceLineNo">157</span><a id="line.157"> /**</a>
<span class="sourceLineNo">158</span><a id="line.158"> * Returns the {@code Hash} {@link org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName} to use</a>
<span class="sourceLineNo">159</span><a id="line.159"> * when performing hashes for credentials matching.</a>
<span class="sourceLineNo">160</span><a id="line.160"> *</a>
<span class="sourceLineNo">161</span><a id="line.161"> * @return the {@code Hash} {@link org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName} to use</a>
<span class="sourceLineNo">162</span><a id="line.162"> * when performing hashes for credentials matching.</a>
<span class="sourceLineNo">163</span><a id="line.163"> * @since 1.1</a>
<span class="sourceLineNo">164</span><a id="line.164"> */</a>
<span class="sourceLineNo">165</span><a id="line.165"> public String getHashAlgorithmName() {</a>
<span class="sourceLineNo">166</span><a id="line.166"> return hashAlgorithm;</a>
<span class="sourceLineNo">167</span><a id="line.167"> }</a>
<span class="sourceLineNo">168</span><a id="line.168"></a>
<span class="sourceLineNo">169</span><a id="line.169"> /**</a>
<span class="sourceLineNo">170</span><a id="line.170"> * Sets the {@code Hash} {@link org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName} to use</a>
<span class="sourceLineNo">171</span><a id="line.171"> * when performing hashes for credentials matching.</a>
<span class="sourceLineNo">172</span><a id="line.172"> *</a>
<span class="sourceLineNo">173</span><a id="line.173"> * @param hashAlgorithmName the {@code Hash} {@link org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName}</a>
<span class="sourceLineNo">174</span><a id="line.174"> * to use when performing hashes for credentials matching.</a>
<span class="sourceLineNo">175</span><a id="line.175"> * @since 1.1</a>
<span class="sourceLineNo">176</span><a id="line.176"> */</a>
<span class="sourceLineNo">177</span><a id="line.177"> public void setHashAlgorithmName(String hashAlgorithmName) {</a>
<span class="sourceLineNo">178</span><a id="line.178"> this.hashAlgorithm = hashAlgorithmName;</a>
<span class="sourceLineNo">179</span><a id="line.179"> }</a>
<span class="sourceLineNo">180</span><a id="line.180"></a>
<span class="sourceLineNo">181</span><a id="line.181"> /**</a>
<span class="sourceLineNo">182</span><a id="line.182"> * Returns {@code true} if the system's stored credential hash is Hex encoded, {@code false} if it</a>
<span class="sourceLineNo">183</span><a id="line.183"> * is Base64 encoded.</a>
<span class="sourceLineNo">184</span><a id="line.184"> * &lt;p/&gt;</a>
<span class="sourceLineNo">185</span><a id="line.185"> * Default value is {@code true} for convenience - all of Shiro's {@link Hash Hash#toString()}</a>
<span class="sourceLineNo">186</span><a id="line.186"> * implementations return Hex encoded values by default, making this class's use with those implementations</a>
<span class="sourceLineNo">187</span><a id="line.187"> * easier.</a>
<span class="sourceLineNo">188</span><a id="line.188"> *</a>
<span class="sourceLineNo">189</span><a id="line.189"> * @return {@code true} if the system's stored credential hash is Hex encoded, {@code false} if it</a>
<span class="sourceLineNo">190</span><a id="line.190"> * is Base64 encoded. Default is {@code true}</a>
<span class="sourceLineNo">191</span><a id="line.191"> */</a>
<span class="sourceLineNo">192</span><a id="line.192"> public boolean isStoredCredentialsHexEncoded() {</a>
<span class="sourceLineNo">193</span><a id="line.193"> return storedCredentialsHexEncoded;</a>
<span class="sourceLineNo">194</span><a id="line.194"> }</a>
<span class="sourceLineNo">195</span><a id="line.195"></a>
<span class="sourceLineNo">196</span><a id="line.196"> /**</a>
<span class="sourceLineNo">197</span><a id="line.197"> * Sets the indicator if this system's stored credential hash is Hex encoded or not.</a>
<span class="sourceLineNo">198</span><a id="line.198"> * &lt;p/&gt;</a>
<span class="sourceLineNo">199</span><a id="line.199"> * A value of {@code true} will cause this class to decode the system credential from Hex, a</a>
<span class="sourceLineNo">200</span><a id="line.200"> * value of {@code false} will cause this class to decode the system credential from Base64.</a>
<span class="sourceLineNo">201</span><a id="line.201"> * &lt;p/&gt;</a>
<span class="sourceLineNo">202</span><a id="line.202"> * Unless overridden via this method, the default value is {@code true} for convenience - all of Shiro's</a>
<span class="sourceLineNo">203</span><a id="line.203"> * {@link Hash Hash#toString()} implementations return Hex encoded values by default, making this class's use with</a>
<span class="sourceLineNo">204</span><a id="line.204"> * those implementations easier.</a>
<span class="sourceLineNo">205</span><a id="line.205"> *</a>
<span class="sourceLineNo">206</span><a id="line.206"> * @param storedCredentialsHexEncoded the indicator if this system's stored credential hash is Hex</a>
<span class="sourceLineNo">207</span><a id="line.207"> * encoded or not ('not' automatically implying it is Base64 encoded).</a>
<span class="sourceLineNo">208</span><a id="line.208"> */</a>
<span class="sourceLineNo">209</span><a id="line.209"> public void setStoredCredentialsHexEncoded(boolean storedCredentialsHexEncoded) {</a>
<span class="sourceLineNo">210</span><a id="line.210"> this.storedCredentialsHexEncoded = storedCredentialsHexEncoded;</a>
<span class="sourceLineNo">211</span><a id="line.211"> }</a>
<span class="sourceLineNo">212</span><a id="line.212"></a>
<span class="sourceLineNo">213</span><a id="line.213"> /**</a>
<span class="sourceLineNo">214</span><a id="line.214"> * Returns {@code true} if a submitted {@code AuthenticationToken}'s credentials should be salted when hashing,</a>
<span class="sourceLineNo">215</span><a id="line.215"> * {@code false} if it should not be salted.</a>
<span class="sourceLineNo">216</span><a id="line.216"> * &lt;p/&gt;</a>
<span class="sourceLineNo">217</span><a id="line.217"> * If enabled, the salt used will be obtained via the {@link #getSalt(AuthenticationToken) getSalt} method.</a>
<span class="sourceLineNo">218</span><a id="line.218"> * &lt;p/&gt;</a>
<span class="sourceLineNo">219</span><a id="line.219"> * The default value is {@code false}.</a>
<span class="sourceLineNo">220</span><a id="line.220"> *</a>
<span class="sourceLineNo">221</span><a id="line.221"> * @return {@code true} if a submitted {@code AuthenticationToken}'s credentials should be salted when hashing,</a>
<span class="sourceLineNo">222</span><a id="line.222"> * {@code false} if it should not be salted.</a>
<span class="sourceLineNo">223</span><a id="line.223"> * @deprecated since Shiro 1.1. Hash salting is now expected to be based on if the {@link AuthenticationInfo}</a>
<span class="sourceLineNo">224</span><a id="line.224"> * returned from the {@code Realm} is a {@link SaltedAuthenticationInfo} instance and its</a>
<span class="sourceLineNo">225</span><a id="line.225"> * {@link org.apache.shiro.authc.SaltedAuthenticationInfo#getCredentialsSalt() getCredentialsSalt()} method returns a non-null value.</a>
<span class="sourceLineNo">226</span><a id="line.226"> * This method and the 1.0 behavior still exists for backwards compatibility if the {@code Realm} does not return</a>
<span class="sourceLineNo">227</span><a id="line.227"> * {@code SaltedAuthenticationInfo} instances, but &lt;b&gt;it is highly recommended that {@code Realm} implementations</a>
<span class="sourceLineNo">228</span><a id="line.228"> * that support hashed credentials start returning {@link SaltedAuthenticationInfo SaltedAuthenticationInfo}</a>
<span class="sourceLineNo">229</span><a id="line.229"> * instances as soon as possible&lt;/b&gt;.</a>
<span class="sourceLineNo">230</span><a id="line.230"> * &lt;p/&gt;</a>
<span class="sourceLineNo">231</span><a id="line.231"> * This is because salts should always be obtained from the stored account information and</a>
<span class="sourceLineNo">232</span><a id="line.232"> * never be interpreted based on user/Subject-entered data. User-entered data is easier to compromise for</a>
<span class="sourceLineNo">233</span><a id="line.233"> * attackers, whereas account-unique (and secure randomly-generated) salts never disseminated to the end-user</a>
<span class="sourceLineNo">234</span><a id="line.234"> * are almost impossible to break. This method will be removed in Shiro 2.0.</a>
<span class="sourceLineNo">235</span><a id="line.235"> */</a>
<span class="sourceLineNo">236</span><a id="line.236"> @Deprecated</a>
<span class="sourceLineNo">237</span><a id="line.237"> public boolean isHashSalted() {</a>
<span class="sourceLineNo">238</span><a id="line.238"> return hashSalted;</a>
<span class="sourceLineNo">239</span><a id="line.239"> }</a>
<span class="sourceLineNo">240</span><a id="line.240"></a>
<span class="sourceLineNo">241</span><a id="line.241"> /**</a>
<span class="sourceLineNo">242</span><a id="line.242"> * Sets whether or not to salt a submitted {@code AuthenticationToken}'s credentials when hashing.</a>
<span class="sourceLineNo">243</span><a id="line.243"> * &lt;p/&gt;</a>
<span class="sourceLineNo">244</span><a id="line.244"> * If enabled, the salt used will be obtained via the {@link #getSalt(org.apache.shiro.authc.AuthenticationToken) getCredentialsSalt} method.</a>
<span class="sourceLineNo">245</span><a id="line.245"> * &lt;/p&gt;</a>
<span class="sourceLineNo">246</span><a id="line.246"> * The default value is {@code false}.</a>
<span class="sourceLineNo">247</span><a id="line.247"> *</a>
<span class="sourceLineNo">248</span><a id="line.248"> * @param hashSalted whether or not to salt a submitted {@code AuthenticationToken}'s credentials when hashing.</a>
<span class="sourceLineNo">249</span><a id="line.249"> * @deprecated since Shiro 1.1. Hash salting is now expected to be based on if the {@link AuthenticationInfo}</a>
<span class="sourceLineNo">250</span><a id="line.250"> * returned from the {@code Realm} is a {@link SaltedAuthenticationInfo} instance and its</a>
<span class="sourceLineNo">251</span><a id="line.251"> * {@link org.apache.shiro.authc.SaltedAuthenticationInfo#getCredentialsSalt() getCredentialsSalt()} method returns a non-null value.</a>
<span class="sourceLineNo">252</span><a id="line.252"> * This method and the 1.0 behavior still exists for backwards compatibility if the {@code Realm} does not return</a>
<span class="sourceLineNo">253</span><a id="line.253"> * {@code SaltedAuthenticationInfo} instances, but &lt;b&gt;it is highly recommended that {@code Realm} implementations</a>
<span class="sourceLineNo">254</span><a id="line.254"> * that support hashed credentials start returning {@link SaltedAuthenticationInfo SaltedAuthenticationInfo}</a>
<span class="sourceLineNo">255</span><a id="line.255"> * instances as soon as possible&lt;/b&gt;.</a>
<span class="sourceLineNo">256</span><a id="line.256"> * &lt;p/&gt;</a>
<span class="sourceLineNo">257</span><a id="line.257"> * This is because salts should always be obtained from the stored account information and</a>
<span class="sourceLineNo">258</span><a id="line.258"> * never be interpreted based on user/Subject-entered data. User-entered data is easier to compromise for</a>
<span class="sourceLineNo">259</span><a id="line.259"> * attackers, whereas account-unique (and secure randomly-generated) salts never disseminated to the end-user</a>
<span class="sourceLineNo">260</span><a id="line.260"> * are almost impossible to break. This method will be removed in Shiro 2.0.</a>
<span class="sourceLineNo">261</span><a id="line.261"> */</a>
<span class="sourceLineNo">262</span><a id="line.262"> @Deprecated</a>
<span class="sourceLineNo">263</span><a id="line.263"> public void setHashSalted(boolean hashSalted) {</a>
<span class="sourceLineNo">264</span><a id="line.264"> this.hashSalted = hashSalted;</a>
<span class="sourceLineNo">265</span><a id="line.265"> }</a>
<span class="sourceLineNo">266</span><a id="line.266"></a>
<span class="sourceLineNo">267</span><a id="line.267"> /**</a>
<span class="sourceLineNo">268</span><a id="line.268"> * Returns the number of times a submitted {@code AuthenticationToken}'s credentials will be hashed before</a>
<span class="sourceLineNo">269</span><a id="line.269"> * comparing to the credentials stored in the system.</a>
<span class="sourceLineNo">270</span><a id="line.270"> * &lt;p/&gt;</a>
<span class="sourceLineNo">271</span><a id="line.271"> * Unless overridden, the default value is {@code 1}, meaning a normal hash execution will occur.</a>
<span class="sourceLineNo">272</span><a id="line.272"> *</a>
<span class="sourceLineNo">273</span><a id="line.273"> * @return the number of times a submitted {@code AuthenticationToken}'s credentials will be hashed before</a>
<span class="sourceLineNo">274</span><a id="line.274"> * comparing to the credentials stored in the system.</a>
<span class="sourceLineNo">275</span><a id="line.275"> */</a>
<span class="sourceLineNo">276</span><a id="line.276"> public int getHashIterations() {</a>
<span class="sourceLineNo">277</span><a id="line.277"> return hashIterations;</a>
<span class="sourceLineNo">278</span><a id="line.278"> }</a>
<span class="sourceLineNo">279</span><a id="line.279"></a>
<span class="sourceLineNo">280</span><a id="line.280"> /**</a>
<span class="sourceLineNo">281</span><a id="line.281"> * Sets the number of times a submitted {@code AuthenticationToken}'s credentials will be hashed before comparing</a>
<span class="sourceLineNo">282</span><a id="line.282"> * to the credentials stored in the system.</a>
<span class="sourceLineNo">283</span><a id="line.283"> * &lt;p/&gt;</a>
<span class="sourceLineNo">284</span><a id="line.284"> * Unless overridden, the default value is {@code 1}, meaning a normal single hash execution will occur.</a>
<span class="sourceLineNo">285</span><a id="line.285"> * &lt;p/&gt;</a>
<span class="sourceLineNo">286</span><a id="line.286"> * If this argument is less than 1 (i.e. 0 or negative), the default value of 1 is applied. There must always be</a>
<span class="sourceLineNo">287</span><a id="line.287"> * at least 1 hash iteration (otherwise there would be no hash).</a>
<span class="sourceLineNo">288</span><a id="line.288"> *</a>
<span class="sourceLineNo">289</span><a id="line.289"> * @param hashIterations the number of times to hash a submitted {@code AuthenticationToken}'s credentials.</a>
<span class="sourceLineNo">290</span><a id="line.290"> */</a>
<span class="sourceLineNo">291</span><a id="line.291"> public void setHashIterations(int hashIterations) {</a>
<span class="sourceLineNo">292</span><a id="line.292"> if (hashIterations &lt; 1) {</a>
<span class="sourceLineNo">293</span><a id="line.293"> this.hashIterations = 1;</a>
<span class="sourceLineNo">294</span><a id="line.294"> } else {</a>
<span class="sourceLineNo">295</span><a id="line.295"> this.hashIterations = hashIterations;</a>
<span class="sourceLineNo">296</span><a id="line.296"> }</a>
<span class="sourceLineNo">297</span><a id="line.297"> }</a>
<span class="sourceLineNo">298</span><a id="line.298"></a>
<span class="sourceLineNo">299</span><a id="line.299"> /**</a>
<span class="sourceLineNo">300</span><a id="line.300"> * Returns a salt value used to hash the token's credentials.</a>
<span class="sourceLineNo">301</span><a id="line.301"> * &lt;p/&gt;</a>
<span class="sourceLineNo">302</span><a id="line.302"> * This default implementation merely returns {@code token.getPrincipal()}, effectively using the user's</a>
<span class="sourceLineNo">303</span><a id="line.303"> * identity (username, user id, etc) as the salt, a most common technique. If you wish to provide the</a>
<span class="sourceLineNo">304</span><a id="line.304"> * authentication token's salt another way, you may override this method.</a>
<span class="sourceLineNo">305</span><a id="line.305"> *</a>
<span class="sourceLineNo">306</span><a id="line.306"> * @param token the AuthenticationToken submitted during the authentication attempt.</a>
<span class="sourceLineNo">307</span><a id="line.307"> * @return a salt value to use to hash the authentication token's credentials.</a>
<span class="sourceLineNo">308</span><a id="line.308"> * @deprecated since Shiro 1.1. Hash salting is now expected to be based on if the {@link AuthenticationInfo}</a>
<span class="sourceLineNo">309</span><a id="line.309"> * returned from the {@code Realm} is a {@link SaltedAuthenticationInfo} instance and its</a>
<span class="sourceLineNo">310</span><a id="line.310"> * {@link org.apache.shiro.authc.SaltedAuthenticationInfo#getCredentialsSalt() getCredentialsSalt()} method returns a non-null value.</a>
<span class="sourceLineNo">311</span><a id="line.311"> * This method and the 1.0 behavior still exists for backwards compatibility if the {@code Realm} does not return</a>
<span class="sourceLineNo">312</span><a id="line.312"> * {@code SaltedAuthenticationInfo} instances, but &lt;b&gt;it is highly recommended that {@code Realm} implementations</a>
<span class="sourceLineNo">313</span><a id="line.313"> * that support hashed credentials start returning {@link SaltedAuthenticationInfo SaltedAuthenticationInfo}</a>
<span class="sourceLineNo">314</span><a id="line.314"> * instances as soon as possible&lt;/b&gt;.&lt;p/&gt;</a>
<span class="sourceLineNo">315</span><a id="line.315"> * This is because salts should always be obtained from the stored account information and</a>
<span class="sourceLineNo">316</span><a id="line.316"> * never be interpreted based on user/Subject-entered data. User-entered data is easier to compromise for</a>
<span class="sourceLineNo">317</span><a id="line.317"> * attackers, whereas account-unique (and secure randomly-generated) salts never disseminated to the end-user</a>
<span class="sourceLineNo">318</span><a id="line.318"> * are almost impossible to break. This method will be removed in Shiro 2.0.</a>
<span class="sourceLineNo">319</span><a id="line.319"> */</a>
<span class="sourceLineNo">320</span><a id="line.320"> @Deprecated</a>
<span class="sourceLineNo">321</span><a id="line.321"> protected Object getSalt(AuthenticationToken token) {</a>
<span class="sourceLineNo">322</span><a id="line.322"> return token.getPrincipal();</a>
<span class="sourceLineNo">323</span><a id="line.323"> }</a>
<span class="sourceLineNo">324</span><a id="line.324"></a>
<span class="sourceLineNo">325</span><a id="line.325"> /**</a>
<span class="sourceLineNo">326</span><a id="line.326"> * Returns a {@link Hash Hash} instance representing the already-hashed AuthenticationInfo credentials stored in the system.</a>
<span class="sourceLineNo">327</span><a id="line.327"> * &lt;p/&gt;</a>
<span class="sourceLineNo">328</span><a id="line.328"> * This method reconstructs a {@link Hash Hash} instance based on a {@code info.getCredentials} call,</a>
<span class="sourceLineNo">329</span><a id="line.329"> * but it does &lt;em&gt;not&lt;/em&gt; hash that value - it is expected that method call will return an already-hashed value.</a>
<span class="sourceLineNo">330</span><a id="line.330"> * &lt;p/&gt;</a>
<span class="sourceLineNo">331</span><a id="line.331"> * This implementation's reconstruction effort functions as follows:</a>
<span class="sourceLineNo">332</span><a id="line.332"> * &lt;ol&gt;</a>
<span class="sourceLineNo">333</span><a id="line.333"> * &lt;li&gt;Convert {@code account.getCredentials()} to a byte array via the {@link #toBytes toBytes} method.</a>
<span class="sourceLineNo">334</span><a id="line.334"> * &lt;li&gt;If {@code account.getCredentials()} was originally a String or char[] before {@code toBytes} was</a>
<span class="sourceLineNo">335</span><a id="line.335"> * called, check for encoding:</a>
<span class="sourceLineNo">336</span><a id="line.336"> * &lt;li&gt;If {@link #storedCredentialsHexEncoded storedCredentialsHexEncoded}, Hex decode that byte array, otherwise</a>
<span class="sourceLineNo">337</span><a id="line.337"> * Base64 decode the byte array&lt;/li&gt;</a>
<span class="sourceLineNo">338</span><a id="line.338"> * &lt;li&gt;Set the byte[] array directly on the {@code Hash} implementation and return it.&lt;/li&gt;</a>
<span class="sourceLineNo">339</span><a id="line.339"> * &lt;/ol&gt;</a>
<span class="sourceLineNo">340</span><a id="line.340"> *</a>
<span class="sourceLineNo">341</span><a id="line.341"> * @param info the AuthenticationInfo from which to retrieve the credentials which assumed to be in already-hashed form.</a>
<span class="sourceLineNo">342</span><a id="line.342"> * @return a {@link Hash Hash} instance representing the given AuthenticationInfo's stored credentials.</a>
<span class="sourceLineNo">343</span><a id="line.343"> */</a>
<span class="sourceLineNo">344</span><a id="line.344"> protected Object getCredentials(AuthenticationInfo info) {</a>
<span class="sourceLineNo">345</span><a id="line.345"> Object credentials = info.getCredentials();</a>
<span class="sourceLineNo">346</span><a id="line.346"></a>
<span class="sourceLineNo">347</span><a id="line.347"> byte[] storedBytes = toBytes(credentials);</a>
<span class="sourceLineNo">348</span><a id="line.348"></a>
<span class="sourceLineNo">349</span><a id="line.349"> if (credentials instanceof String || credentials instanceof char[]) {</a>
<span class="sourceLineNo">350</span><a id="line.350"> //account.credentials were a char[] or String, so</a>
<span class="sourceLineNo">351</span><a id="line.351"> //we need to do text decoding first:</a>
<span class="sourceLineNo">352</span><a id="line.352"> if (isStoredCredentialsHexEncoded()) {</a>
<span class="sourceLineNo">353</span><a id="line.353"> storedBytes = Hex.decode(storedBytes);</a>
<span class="sourceLineNo">354</span><a id="line.354"> } else {</a>
<span class="sourceLineNo">355</span><a id="line.355"> storedBytes = Base64.decode(storedBytes);</a>
<span class="sourceLineNo">356</span><a id="line.356"> }</a>
<span class="sourceLineNo">357</span><a id="line.357"> }</a>
<span class="sourceLineNo">358</span><a id="line.358"> AbstractHash hash = newHashInstance();</a>
<span class="sourceLineNo">359</span><a id="line.359"> hash.setBytes(storedBytes);</a>
<span class="sourceLineNo">360</span><a id="line.360"> return hash;</a>
<span class="sourceLineNo">361</span><a id="line.361"> }</a>
<span class="sourceLineNo">362</span><a id="line.362"></a>
<span class="sourceLineNo">363</span><a id="line.363"> /**</a>
<span class="sourceLineNo">364</span><a id="line.364"> * This implementation first hashes the {@code token}'s credentials, potentially using a</a>
<span class="sourceLineNo">365</span><a id="line.365"> * {@code salt} if the {@code info} argument is a</a>
<span class="sourceLineNo">366</span><a id="line.366"> * {@link org.apache.shiro.authc.SaltedAuthenticationInfo SaltedAuthenticationInfo}. It then compares the hash</a>
<span class="sourceLineNo">367</span><a id="line.367"> * against the {@code AuthenticationInfo}'s</a>
<span class="sourceLineNo">368</span><a id="line.368"> * {@link #getCredentials(org.apache.shiro.authc.AuthenticationInfo) already-hashed credentials}. This method</a>
<span class="sourceLineNo">369</span><a id="line.369"> * returns {@code true} if those two values are {@link #equals(Object, Object) equal}, {@code false} otherwise.</a>
<span class="sourceLineNo">370</span><a id="line.370"> *</a>
<span class="sourceLineNo">371</span><a id="line.371"> * @param token the {@code AuthenticationToken} submitted during the authentication attempt.</a>
<span class="sourceLineNo">372</span><a id="line.372"> * @param info the {@code AuthenticationInfo} stored in the system matching the token principal</a>
<span class="sourceLineNo">373</span><a id="line.373"> * @return {@code true} if the provided token credentials hash match to the stored account credentials hash,</a>
<span class="sourceLineNo">374</span><a id="line.374"> * {@code false} otherwise</a>
<span class="sourceLineNo">375</span><a id="line.375"> * @since 1.1</a>
<span class="sourceLineNo">376</span><a id="line.376"> */</a>
<span class="sourceLineNo">377</span><a id="line.377"> @Override</a>
<span class="sourceLineNo">378</span><a id="line.378"> public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {</a>
<span class="sourceLineNo">379</span><a id="line.379"> Object tokenHashedCredentials = hashProvidedCredentials(token, info);</a>
<span class="sourceLineNo">380</span><a id="line.380"> Object accountCredentials = getCredentials(info);</a>
<span class="sourceLineNo">381</span><a id="line.381"> return equals(tokenHashedCredentials, accountCredentials);</a>
<span class="sourceLineNo">382</span><a id="line.382"> }</a>
<span class="sourceLineNo">383</span><a id="line.383"></a>
<span class="sourceLineNo">384</span><a id="line.384"> /**</a>
<span class="sourceLineNo">385</span><a id="line.385"> * Hash the provided {@code token}'s credentials using the salt stored with the account if the</a>
<span class="sourceLineNo">386</span><a id="line.386"> * {@code info} instance is an {@code instanceof} {@link SaltedAuthenticationInfo SaltedAuthenticationInfo} (see</a>
<span class="sourceLineNo">387</span><a id="line.387"> * the class-level JavaDoc for why this is the preferred approach).</a>
<span class="sourceLineNo">388</span><a id="line.388"> * &lt;p/&gt;</a>
<span class="sourceLineNo">389</span><a id="line.389"> * If the {@code info} instance is &lt;em&gt;not&lt;/em&gt;</a>
<span class="sourceLineNo">390</span><a id="line.390"> * an {@code instanceof} {@code SaltedAuthenticationInfo}, the logic will fall back to Shiro 1.0</a>
<span class="sourceLineNo">391</span><a id="line.391"> * backwards-compatible logic: it will first check to see {@link #isHashSalted() isHashSalted} and if so, will try</a>
<span class="sourceLineNo">392</span><a id="line.392"> * to acquire the salt from {@link #getSalt(AuthenticationToken) getSalt(AuthenticationToken)}. See the class-level</a>
<span class="sourceLineNo">393</span><a id="line.393"> * JavaDoc for why this is not recommended. This 'fallback' logic exists only for backwards-compatibility.</a>
<span class="sourceLineNo">394</span><a id="line.394"> * {@code Realm}s should be updated as soon as possible to return {@code SaltedAuthenticationInfo} instances</a>
<span class="sourceLineNo">395</span><a id="line.395"> * if account credentials salting is enabled (highly recommended for password-based systems).</a>
<span class="sourceLineNo">396</span><a id="line.396"> *</a>
<span class="sourceLineNo">397</span><a id="line.397"> * @param token the submitted authentication token from which its credentials will be hashed</a>
<span class="sourceLineNo">398</span><a id="line.398"> * @param info the stored account data, potentially used to acquire a salt</a>
<span class="sourceLineNo">399</span><a id="line.399"> * @return the token credentials hash</a>
<span class="sourceLineNo">400</span><a id="line.400"> * @since 1.1</a>
<span class="sourceLineNo">401</span><a id="line.401"> */</a>
<span class="sourceLineNo">402</span><a id="line.402"> protected Object hashProvidedCredentials(AuthenticationToken token, AuthenticationInfo info) {</a>
<span class="sourceLineNo">403</span><a id="line.403"> Object salt = null;</a>
<span class="sourceLineNo">404</span><a id="line.404"> if (info instanceof SaltedAuthenticationInfo) {</a>
<span class="sourceLineNo">405</span><a id="line.405"> salt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();</a>
<span class="sourceLineNo">406</span><a id="line.406"> } else {</a>
<span class="sourceLineNo">407</span><a id="line.407"> //retain 1.0 backwards compatibility:</a>
<span class="sourceLineNo">408</span><a id="line.408"> if (isHashSalted()) {</a>
<span class="sourceLineNo">409</span><a id="line.409"> salt = getSalt(token);</a>
<span class="sourceLineNo">410</span><a id="line.410"> }</a>
<span class="sourceLineNo">411</span><a id="line.411"> }</a>
<span class="sourceLineNo">412</span><a id="line.412"> return hashProvidedCredentials(token.getCredentials(), salt, getHashIterations());</a>
<span class="sourceLineNo">413</span><a id="line.413"> }</a>
<span class="sourceLineNo">414</span><a id="line.414"></a>
<span class="sourceLineNo">415</span><a id="line.415"> /**</a>
<span class="sourceLineNo">416</span><a id="line.416"> * Returns the {@link #getHashAlgorithmName() hashAlgorithmName} property, but will throw an</a>
<span class="sourceLineNo">417</span><a id="line.417"> * {@link IllegalStateException} if it has not been set.</a>
<span class="sourceLineNo">418</span><a id="line.418"> *</a>
<span class="sourceLineNo">419</span><a id="line.419"> * @return the required {@link #getHashAlgorithmName() hashAlgorithmName} property</a>
<span class="sourceLineNo">420</span><a id="line.420"> * @throws IllegalStateException if the property has not been set prior to calling this method.</a>
<span class="sourceLineNo">421</span><a id="line.421"> * @since 1.1</a>
<span class="sourceLineNo">422</span><a id="line.422"> */</a>
<span class="sourceLineNo">423</span><a id="line.423"> private String assertHashAlgorithmName() throws IllegalStateException {</a>
<span class="sourceLineNo">424</span><a id="line.424"> String hashAlgorithmName = getHashAlgorithmName();</a>
<span class="sourceLineNo">425</span><a id="line.425"> if (hashAlgorithmName == null) {</a>
<span class="sourceLineNo">426</span><a id="line.426"> String msg = "Required 'hashAlgorithmName' property has not been set. This is required to execute " +</a>
<span class="sourceLineNo">427</span><a id="line.427"> "the hashing algorithm.";</a>
<span class="sourceLineNo">428</span><a id="line.428"> throw new IllegalStateException(msg);</a>
<span class="sourceLineNo">429</span><a id="line.429"> }</a>
<span class="sourceLineNo">430</span><a id="line.430"> return hashAlgorithmName;</a>
<span class="sourceLineNo">431</span><a id="line.431"> }</a>
<span class="sourceLineNo">432</span><a id="line.432"></a>
<span class="sourceLineNo">433</span><a id="line.433"> /**</a>
<span class="sourceLineNo">434</span><a id="line.434"> * Hashes the provided credentials a total of {@code hashIterations} times, using the given salt. The hash</a>
<span class="sourceLineNo">435</span><a id="line.435"> * implementation/algorithm used is based on the {@link #getHashAlgorithmName() hashAlgorithmName} property.</a>
<span class="sourceLineNo">436</span><a id="line.436"> *</a>
<span class="sourceLineNo">437</span><a id="line.437"> * @param credentials the submitted authentication token's credentials to hash</a>
<span class="sourceLineNo">438</span><a id="line.438"> * @param salt the value to salt the hash, or {@code null} if a salt will not be used.</a>
<span class="sourceLineNo">439</span><a id="line.439"> * @param hashIterations the number of times to hash the credentials. At least one hash will always occur though,</a>
<span class="sourceLineNo">440</span><a id="line.440"> * even if this argument is 0 or negative.</a>
<span class="sourceLineNo">441</span><a id="line.441"> * @return the hashed value of the provided credentials, according to the specified salt and hash iterations.</a>
<span class="sourceLineNo">442</span><a id="line.442"> */</a>
<span class="sourceLineNo">443</span><a id="line.443"> protected Hash hashProvidedCredentials(Object credentials, Object salt, int hashIterations) {</a>
<span class="sourceLineNo">444</span><a id="line.444"> String hashAlgorithmName = assertHashAlgorithmName();</a>
<span class="sourceLineNo">445</span><a id="line.445"> return new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);</a>
<span class="sourceLineNo">446</span><a id="line.446"> }</a>
<span class="sourceLineNo">447</span><a id="line.447"></a>
<span class="sourceLineNo">448</span><a id="line.448"> /**</a>
<span class="sourceLineNo">449</span><a id="line.449"> * Returns a new, &lt;em&gt;uninitialized&lt;/em&gt; instance, without its byte array set. Used as a utility method in the</a>
<span class="sourceLineNo">450</span><a id="line.450"> * {@link SimpleCredentialsMatcher#getCredentials(org.apache.shiro.authc.AuthenticationInfo) getCredentials(AuthenticationInfo)} implementation.</a>
<span class="sourceLineNo">451</span><a id="line.451"> *</a>
<span class="sourceLineNo">452</span><a id="line.452"> * @return a new, &lt;em&gt;uninitialized&lt;/em&gt; instance, without its byte array set.</a>
<span class="sourceLineNo">453</span><a id="line.453"> */</a>
<span class="sourceLineNo">454</span><a id="line.454"> protected AbstractHash newHashInstance() {</a>
<span class="sourceLineNo">455</span><a id="line.455"> String hashAlgorithmName = assertHashAlgorithmName();</a>
<span class="sourceLineNo">456</span><a id="line.456"> return new SimpleHash(hashAlgorithmName);</a>
<span class="sourceLineNo">457</span><a id="line.457"> }</a>
<span class="sourceLineNo">458</span><a id="line.458"></a>
<span class="sourceLineNo">459</span><a id="line.459">}</a>
</pre>
</div>
</main>
</body>
</html>