我需要散列密码以存储在数据库中。我怎样才能在 Java 中做到这一点?
我希望获取纯文本密码,添加随机盐,然后将盐和散列密码存储在数据库中。
然后当用户想要登录时,我可以获取他们提交的密码,从他们的帐户信息中添加随机盐,对其进行哈希处理,看看它是否等同于存储的哈希密码和他们的帐户信息。
我需要散列密码以存储在数据库中。我怎样才能在 Java 中做到这一点?
我希望获取纯文本密码,添加随机盐,然后将盐和散列密码存储在数据库中。
然后当用户想要登录时,我可以获取他们提交的密码,从他们的帐户信息中添加随机盐,对其进行哈希处理,看看它是否等同于存储的哈希密码和他们的帐户信息。
您实际上可以使用 Java 运行时内置的工具来执行此操作。Java 6 中的 SunJCE 支持 PBKDF2,这是一个很好的密码散列算法。
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="http://stackoverflow.com/a/2861125/3474">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);
}
}
BCrypt 是一个非常好的库,并且有一个Java 端口。
您可以使用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
,但这在安全性方面是错误的。哈希不能用于存储密码,因为它们很容易被破解。
您应该使用另一种算法,例如 bcrypt、PBKDF2 和 scrypt 来存储您的密码。见这里。
完全同意 Erickson 的观点,即PBKDF2就是答案。
如果您没有该选项,或者只需要使用哈希,Apache Commons DigestUtils 比正确获取 JCE 代码要容易得多: https ://commons.apache.org/proper/commons-codec/apidocs/org/apache /commons/codec/digest/DigestUtils.html
如果您使用哈希,请使用 sha256 或 sha512。此页面对密码处理和散列有很好的建议(注意它不建议对密码处理进行散列): http ://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
除了其他答案中提到的 bcrypt 和 PBKDF2 之外,我建议您查看scrypt
不建议使用 MD5 和 SHA-1,因为它们相对较快,因此使用“每小时租金”分布式计算(例如 EC2)或现代高端 GPU,可以使用暴力/字典攻击以相对较低的成本和合理的方式“破解”密码时间。
如果您必须使用它们,那么至少将算法迭代预定义的大量时间(1000+)。
请参阅此处了解更多信息:https ://security.stackexchange.com/questions/211/how-to-securely-hash-passwords
在这里:http ://codahale.com/how-to-safely-store-a-password/ (批评 SHA 系列、MD5 等用于密码散列)
虽然已经提到了NIST 推荐的 PBKDF2 ,但我想指出,从 2013 年到 2015 年有一场公开的密码哈希竞赛。最终, Argon2被选为推荐的密码哈希函数。
对于您可以使用的原始(本机 C)库,有一个相当好的Java 绑定。
在一般用例中,我认为从安全角度来看,如果您选择 PBKDF2 而不是 Argon2,反之亦然。如果您有严格的安全要求,我建议您在评估中考虑 Argon2。
有关密码散列函数安全性的更多信息,请参阅security.se。
这里有两个 MD5 散列和其他散列方法的链接:
Javadoc API:https ://docs.oracle.com/javase/1.5.0/docs/api/java/security/MessageDigest.html
教程: http: //www.twmacinta.com/myjava/fast_md5.php
在所有标准哈希方案中,LDAP ssha 是最安全的一种,
http://www.openldap.org/faq/data/cache/347.html
我会按照那里指定的算法并使用 MessageDigest 进行哈希处理。
您需要按照您的建议将盐存储在数据库中。
截至 2020 年,Argon2id 或 Argon2i 是Argon2id或Argon2i,但不是其 Spring 实现,在使用中最可靠的密码散列算法,最有可能在任何硬件的情况下优化其强度。
PBKDF2 标准包括块密码 BCRYPT 算法的 CPU 贪婪/计算成本高的特性,并添加了它的流密码功能。PBKDF2 被内存指数贪婪的 SCRYPT 淹没,然后被抗侧信道攻击的 Argon2 淹没
Argon2 提供必要的校准工具,以在给定目标散列时间和使用的硬件的情况下找到优化的强度参数。
内存贪婪散列有助于防止 GPU 用于破解。
Spring security/Bouncy Castle 的实现没有优化,并且考虑到攻击者可以使用的相对一周。cf: Spring doc Argon2和Scrypt
当前的实现使用了 Bouncy castle,它没有利用密码破解者将使用的并行性/优化,因此攻击者和防御者之间存在不必要的不对称。
用于 java 的最可靠的实现是mkammerer的实现,
用 C 编写的官方本机实现的包装 jar/库。
它写得很好,使用简单。
嵌入式版本为 Linux、windows 和 OSX 提供本地构建。
例如,摩根大通在其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>
或使用 gradle:
compile 'de.mkammerer:argon2-jvm:2.7'
可以使用 de.mkammerer.argon2.Argon2Helper#findIterations进行校准
SCRYPT 和 Pbkdf2 算法也可以通过编写一些简单的基准来校准,但当前的最小安全迭代值将需要更高的哈希时间。
我从 udemy 上的视频中学习并编辑为更强大的随机密码
}
private String pass() {
String passswet="1234567890zxcvbbnmasdfghjklop[iuytrtewq@#$%^&*" ;
char icon1;
char[] t=new char[20];
int rand1=(int)(Math.random()*6)+38;//to make a random within the range of special characters
icon1=passswet.charAt(rand1);//will produce char with a special character
int i=0;
while( i <11) {
int rand=(int)(Math.random()*passswet.length());
//notice (int) as the original value of Math>random() is double
t[i] =passswet.charAt(rand);
i++;
t[10]=icon1;
//to replace the specified item with icon1
}
return new String(t);
}
}