我希望将明文密码与随机盐相结合,然后将盐和哈希密码存储在数据库中。
然后,当用户想要登录时,我可以获取他们提交的密码,添加他们帐户信息中的随机盐,对其进行哈希,并查看它是否等于存储在他们帐户信息中的哈希密码。
你可以使用Java运行时内置的一个设施来实现这一点。Java 6中的SunJCE支持PBKDF2,这是一个用于密码哈希的好算法。
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec("password".toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = f.generateSecret(spec).getEncoded();
Base64.Encoder enc = Base64.getEncoder();
System.out.printf("salt: %s%n", enc.encodeToString(salt));
System.out.printf("hash: %s%n", enc.encodeToString(hash));
这是一个实用的类,您可以用它进行PBKDF2密码验证:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
/**
* Hash passwords for storage, and test passwords against password tokens.
*
* Instances of this class can be used concurrently by multiple threads.
*
* @author erickson
* @see <a href="https://dev59.com/SnE85IYBdhLWcg3wRBRU#2861125">StackOverflow</a>
*/
public final class PasswordAuthentication
{
/**
* Each token produced by this class uses this identifier as a prefix.
*/
public static final String ID = "$31$";
/**
* The minimum recommended cost, used by default
*/
public static final int DEFAULT_COST = 16;
private static final String ALGORITHM = "PBKDF2WithHmacSHA1";
private static final int SIZE = 128;
private static final Pattern layout = Pattern.compile("\\$31\\$(\\d\\d?)\\$(.{43})");
private final SecureRandom random;
private final int cost;
public PasswordAuthentication()
{
this(DEFAULT_COST);
}
/**
* Create a password manager with a specified cost
*
* @param cost the exponential computational cost of hashing a password, 0 to 30
*/
public PasswordAuthentication(int cost)
{
iterations(cost); /* Validate cost */
this.cost = cost;
this.random = new SecureRandom();
}
private static int iterations(int cost)
{
if ((cost < 0) || (cost > 30))
throw new IllegalArgumentException("cost: " + cost);
return 1 << cost;
}
/**
* Hash a password for storage.
*
* @return a secure authentication token to be stored for later authentication
*/
public String hash(char[] password)
{
byte[] salt = new byte[SIZE / 8];
random.nextBytes(salt);
byte[] dk = pbkdf2(password, salt, 1 << cost);
byte[] hash = new byte[salt.length + dk.length];
System.arraycopy(salt, 0, hash, 0, salt.length);
System.arraycopy(dk, 0, hash, salt.length, dk.length);
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();
return ID + cost + '$' + enc.encodeToString(hash);
}
/**
* Authenticate with a password and a stored password token.
*
* @return true if the password and token match
*/
public boolean authenticate(char[] password, String token)
{
Matcher m = layout.matcher(token);
if (!m.matches())
throw new IllegalArgumentException("Invalid token format");
int iterations = iterations(Integer.parseInt(m.group(1)));
byte[] hash = Base64.getUrlDecoder().decode(m.group(2));
byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8);
byte[] check = pbkdf2(password, salt, iterations);
int zero = 0;
for (int idx = 0; idx < check.length; ++idx)
zero |= hash[salt.length + idx] ^ check[idx];
return zero == 0;
}
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations)
{
KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE);
try {
SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM);
return f.generateSecret(spec).getEncoded();
}
catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("Missing algorithm: " + ALGORITHM, ex);
}
catch (InvalidKeySpecException ex) {
throw new IllegalStateException("Invalid SecretKeyFactory", ex);
}
}
/**
* Hash a password in an immutable {@code String}.
*
* <p>Passwords should be stored in a {@code char[]} so that it can be filled
* with zeros after use instead of lingering on the heap and elsewhere.
*
* @deprecated Use {@link #hash(char[])} instead
*/
@Deprecated
public String hash(String password)
{
return hash(password.toCharArray());
}
/**
* Authenticate with a password in an immutable {@code String} and a stored
* password token.
*
* @deprecated Use {@link #authenticate(char[],String)} instead.
* @see #hash(String)
*/
@Deprecated
public boolean authenticate(String password, String token)
{
return authenticate(password.toCharArray(), token);
}
}
BigInteger
进行字节到十六进制转换时,你可能需要小心一些:前导零会被删除。虽然这对于快速调试来说是可以接受的,但我曾经见过因此在生产代码中出现过错误。 - Thomas Porninauthenticate(char[] password, String token)
中你将if
改成了|=
运算符?这样做有什么好处吗?看起来唯一的影响就是更长的执行时间,或者我漏掉了什么? - The Tosters您可以使用Spring Security Crypto(仅有2个可选的编译依赖项),支持PBKDF2,BCrypt,SCrypt和Argon2密码加密。
Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder();
String aCryptedPassword = argon2PasswordEncoder.encode("password");
boolean passwordIsValid = argon2PasswordEncoder.matches("password", aCryptedPassword);
SCryptPasswordEncoder sCryptPasswordEncoder = new SCryptPasswordEncoder();
String sCryptedPassword = sCryptPasswordEncoder.encode("password");
boolean passwordIsValid = sCryptPasswordEncoder.matches("password", sCryptedPassword);
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String bCryptedPassword = bCryptPasswordEncoder.encode("password");
boolean passwordIsValid = bCryptPasswordEncoder.matches("password", bCryptedPassword);
Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder();
String pbkdf2CryptedPassword = pbkdf2PasswordEncoder.encode("password");
boolean passwordIsValid = pbkdf2PasswordEncoder.matches("password", pbkdf2CryptedPassword);
MessageDigest
计算哈希值,但从安全角度来看这是错误的。哈希值不应用于存储密码,因为它们很容易被破解。虽然已经提到了NIST推荐的PBKDF2, 但我想指出,从2013年到2015年有一场公开的密码哈希比赛。最终,Argon2被选为推荐使用的密码哈希函数。
原始(本地C)库有一个相当广泛采用的Java绑定可供使用。
在平均使用情况下,无论您选择PBKDF2还是Argon2,从安全角度来看都不重要。如果您具有强大的安全要求,请考虑在评估中使用Argon2。
有关密码哈希函数安全性的更多信息,请参见security.se。
Spring security/Bouncy Castle实现并不是最优化的,相对较弱,攻击者可以利用。 参考:Spring文档 Argon2 和 Scrypt
当前实现使用了Bouncy Castle,它没有利用密码破解器所使用的并行性/优化,因此攻击者和防御者之间存在不必要的不对称。
Java中目前最可信的实现是mkammerer的一个包装JAR /库, 它是C语言编写的官方本地实现的封装。
它编写得非常好,并且易于使用。
嵌入式版本为Linux、Windows和OSX提供本机构建。
作为一个例子,它被jpmorganchase用于其tessera安全项目中,该项目用于保护Quorum,其以太坊加密货币实现。 final char[] password = "a4e9y2tr0ngAnd7on6P১M°RD".toCharArray();
byte[] salt = new byte[128];
new SecureRandom().nextBytes(salt);
final Argon2Advanced argon2 = Argon2Factory.createAdvanced(Argon2Factory.Argon2Types.ARGON2id);
byte[] hash = argon2.rawHash(10, 1048576, 4, password, salt);
(查看tessera)
在你的POM文件中声明该库:
<dependency>
<groupId>de.mkammerer</groupId>
<artifactId>argon2-jvm</artifactId>
<version>2.7</version>
</dependency>
compile 'de.mkammerer:argon2-jvm:2.7'
校准可以使用de.mkammerer.argon2.Argon2Helper#findIterations进行。
SCRYPT和Pbkdf2算法也可以通过编写一些简单的基准测试来进行校准,但当前最小安全迭代值将需要更长的哈希时间。
这里有两个链接,可以用来进行MD5哈希和其他哈希方法:
Javadoc API: https://docs.oracle.com/javase/1.5.0/docs/api/java/security/MessageDigest.html