我怎样才能在Java中散列密码?

我需要哈希密码存储在数据库中。 我怎样才能在Java中做到这一点?

我希望能拿到明文密码,添加一个随机盐,然后将盐和散列的密码存储在数据库中。

然后,当用户想要login时,我可以提取他们提交的密码,从他们的账户信息中添加随机的盐,散列它,并查看它是否等同于存储的散列密码与他们的账户信息。

实际上,您可以使用内置于Java运行时的设施来执行此操作。 Java 6中的SunJCE支持PBKDF2,这是一个很好的密码散列algorithm。

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密码authentication:

 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); } } 

这里有两个方法完全实现你想要的完整实现

 String getSaltedHash(String password) boolean checkPassword(String password, String stored) 

重点是即使攻击者可以访问数据库和源代码,密码仍然是安全的。

 import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import java.security.SecureRandom; import org.apache.commons.codec.binary.Base64; public class Password { // The higher the number of iterations the more // expensive computing the hash is for us and // also for an attacker. private static final int iterations = 20*1000; private static final int saltLen = 32; private static final int desiredKeyLen = 256; /** Computes a salted PBKDF2 hash of given plaintext password suitable for storing in a database. Empty passwords are not supported. */ public static String getSaltedHash(String password) throws Exception { byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen); // store the salt with the password return Base64.encodeBase64String(salt) + "$" + hash(password, salt); } /** Checks whether given plaintext password corresponds to a stored salted hash of the password. */ public static boolean check(String password, String stored) throws Exception{ String[] saltAndPass = stored.split("\\$"); if (saltAndPass.length != 2) { throw new IllegalStateException( "The stored password have the form 'salt$hash'"); } String hashOfInput = hash(password, Base64.decodeBase64(saltAndPass[0])); return hashOfInput.equals(saltAndPass[1]); } // using PBKDF2 from Sun, an alternative is https://github.com/wg/scrypt // cf. http://www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html private static String hash(String password, byte[] salt) throws Exception { if (password == null || password.length() == 0) throw new IllegalArgumentException("Empty passwords are not supported."); SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = f.generateSecret(new PBEKeySpec( password.toCharArray(), salt, iterations, desiredKeyLen) ); return Base64.encodeBase64String(key.getEncoded()); } } 

我们正在储存'salt$iterated_hash(password, salt)' 。 盐是32个随机字节,它的目的是,如果两个不同的人select相同的密码,存储的密码将仍然看起来不同。

iterated_hash基本上是hash(hash(hash(... hash(password, salt) ...))) ,它使得访问数据库的潜在攻击者猜测密码,哈希和查看在数据库中join哈希。 每次用户login时都必须计算这个iterated_hash ,但与花费近100%时间计算散列的攻击者相比,这并不会花费太多。

BCrypt是一个非常好的库,它有一个Java端口 。

您可以使用Shiro库(以前称为JSecurity ) 实现 OWASP描述的内容。

它也看起来像JASYPT库有一个类似的工具 。

您可以使用MessageDigest来计算哈希值,但这在安全性方面是错误的。 哈希不能用于存储密码,因为它们很容易被破坏。

你应该使用另一个algorithm,如bcrypt,PBKDF2和scrypt来存储你的密码。 看到这里 。

除了在其他答案中提到的bcrypt和PBKDF2,我会build议看看scrypt

不build议MD5和SHA-1,因为它们相对较快,因此使用“租用每小时”分布式计算(例如EC2)或现代高端GPU,可以使用蛮力/字典攻击以相对较低的成本和合理的方式“破解”密码时间。

如果你必须使用它们,那么至less迭代algorithm预定义的重要次数(1000+)。

  • 请参阅这里了解更多: https : //security.stackexchange.com/questions/211/how-to-securely-hash-passwords

  • 在这里: http : //codahale.com/how-to-safely-store-a-password/ (批评SHA家庭,MD5等密码散列的目的)

  • 在这里: http : //www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html (批评bcrypt并build议scrypt和PBKDF2)

完全同意埃里克森认为PBKDF2是答案。

如果你没有这个选项,或者只需要使用哈希,Apache Commons DigestUtils比获取JCE代码更容易: https ://commons.apache.org/proper/commons-codec/apidocs/org/apache /commons/codec/digest/DigestUtils.html

如果您使用散列,请使用sha256或sha512。 这个页面在密码处理和散列方面有很好的build议(注意,它不推荐使用散列来处理密码): http : //www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html

这里有两个用于MD5哈希和其他哈希方法的链接:

Javadoc API: http : //java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html

教程: http : //www.twmacinta.com/myjava/fast_md5.php

虽然NIST的build议PBKDF2已经被提及,但我想指出的是,2013年到2015年间,有一个公开的密码哈希竞争 。最后,select了Argon2作为推荐的密码哈希函数。

对于您可以使用的原始(本机C)库有相当好的采用的Java绑定 。

在平均使用情况下,如果您selectPBKDF2 over Argon2,反之亦然,从安全angular度来看,我认为这并不重要。 如果您有很强的安全性要求,我build议您考虑使用Argon2。

有关密码散列函数安全性的更多信息,请参阅security.se 。

在所有的标准哈希scheme中,LDAP ssha是最安全的一个,

http://www.openldap.org/faq/data/cache/347.html

我只是按照那里指定的algorithm,并使用MessageDigest做哈希。

您需要按照您的build议将盐存储在数据库中。