如何在Java中散列密码?

Chr*_*row 170 java passwords cryptographic-hash-function

我需要哈希密码以存储在数据库中.我怎么能用Java做到这一点?

我希望获取纯文本密码,添加随机盐,然后将salt和散列密码存储在数据库中.

然后,当用户想要登录时,我可以获取他们提交的密码,从他们的帐户信息中添加随机盐,哈希并查看它是否等于存储的哈希密码及其帐户信息.

eri*_*son 149

实际上,您可以使用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));
Run Code Online (Sandbox Code Playgroud)

这是一个可用于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);
  }

}
Run Code Online (Sandbox Code Playgroud)

  • @ thomas-pornin强调了为什么我们需要一个*库*,而不是一个*几乎*那里的代码块.可怕的是,接受的答案没有回答关于这样一个重要主题的问题. (22认同)
  • 您可能希望对使用`BigInteger`的字节到十六进制转换有点警惕:删除前导零.这对于快速调试是可以的,但是由于这种影响,我在生产代码中看到了错误. (11认同)
  • 使用从Java 8开始的算法PBKDF2WithHmacSHA512.它有点强大. (8认同)
  • @TheTosters是的,*错误*密码的执行时间会更长; 更具体地说,错误的密码将与正确的密码同时使用.它可以防止[计时攻击](https://en.wikipedia.org/wiki/Timing_attack),虽然我承认我无法想到在这种情况下利用这种漏洞的实用方法.但你不要偷工减料.仅仅因为我看不到它,并不意味着一个更加狡猾的头脑不会. (4认同)
  • @erickson 很抱歉挖出这篇文章,但我花了一些时间分析这段代码。我想知道为什么在方法 `authenticate(char[] password, String token)` 中将 `if` 更改为 `|=` 运算符?这有什么好处吗?看起来唯一的效果是执行时间更长,或者我错过了什么? (2认同)

Mar*_*cek 92

这是一个完整的实现,有两个方法正是你想要的:

String getSaltedHash(String password)
boolean checkPassword(String password, String stored)
Run Code Online (Sandbox Code Playgroud)

关键是即使攻击者可以访问您的数据库和源代码,密码仍然是安全的.

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[] saltAndHash = stored.split("\\$");
        if (saltAndHash.length != 2) {
            throw new IllegalStateException(
                "The stored password must have the form 'salt$hash'");
        }
        String hashOfInput = hash(password, Base64.decodeBase64(saltAndHash[0]));
        return hashOfInput.equals(saltAndHash[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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我们正在存储'salt$iterated_hash(password, salt)'.盐是32个随机字节,其目的是如果两个不同的人选择相同的密码,存储的密码仍然会有所不同.

iterated_hash,这基本上是hash(hash(hash(... hash(password, salt) ...)))使得对于谁可以访问你的数据库信息来猜测密码,哈希他们,查找哈希数据库中的一个潜在的攻击者很昂贵.iterated_hash每次用户登录时都必须计算这一点,但与花费近100%的时间计算哈希值的攻击者相比,它不会花费太多.

  • 抱歉唠叨,但为什么我要选择现有的库?图书馆可能更有可能被彻底审查.我怀疑14个投票中的每一个都分析了代码中的任何问题. (13认同)
  • 您应该将方法签名更改为`char [] password`而不是`String password`. (3认同)
  • 你确定字符串上的.equals()不会短路(即:当找到两个不相等的字节时停止循环)?如果确实存在,则存在定时攻击泄露密码哈希信息的风险. (3认同)
  • @JoachimSauer这实际上只是使用一个库(javax.crypto),但是您是对的-不支持空密码。添加了一个异常使其明确。谢谢! (2认同)
  • 虽然看来参考文献没有得到一致同意.另见:http://security.stackexchange.com/a/20369/12614 (2认同)

Mic*_*rdt 27

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

  • bcrypt用随机盐吮吸 (2认同)

Boz*_*zho 8

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

您应该使用其他算法,如bcrypt,PBKDF2和scrypt来存储密码.看到这里.

  • 使用用户名(或其他ID如电子邮件)作为盐的一个问题是,您无法在不让用户也设置新密码的情况下更改ID. (13认同)
  • 使用用户名作为盐并不是一个致命的缺陷,但它远不如使用加密RNG中的盐那么好.并且在数据库中存储salt绝对没有问题.盐并不是秘密. (9认同)
  • 如果不在数据库中存储salt,您将如何在登录时散列密码? (3认同)

BuZ*_*dEE 7

您可以使用Spring Security Crypto(只有2 个可选的编译依赖项),它支持PBKDF2BCryptSCryptArgon2密码加密。

Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder();
String aCryptedPassword = argon2PasswordEncoder.encode("password");
boolean passwordIsValid = argon2PasswordEncoder.matches("password", aCryptedPassword);
Run Code Online (Sandbox Code Playgroud)
SCryptPasswordEncoder sCryptPasswordEncoder = new SCryptPasswordEncoder();
String sCryptedPassword = sCryptPasswordEncoder.encode("password");
boolean passwordIsValid = sCryptPasswordEncoder.matches("password", sCryptedPassword);
Run Code Online (Sandbox Code Playgroud)
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String bCryptedPassword = bCryptPasswordEncoder.encode("password");
boolean passwordIsValid = bCryptPasswordEncoder.matches("password", bCryptedPassword);
Run Code Online (Sandbox Code Playgroud)
Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder();
String pbkdf2CryptedPassword = pbkdf2PasswordEncoder.encode("password");
boolean passwordIsValid = pbkdf2PasswordEncoder.matches("password", pbkdf2CryptedPassword);
Run Code Online (Sandbox Code Playgroud)


laz*_*laz 6

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

它看起来像JASYPT库有类似的实用程序.


Dav*_*oni 6

完全同意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


Era*_*dan 6

除了其他答案中提到的bcrypt和PBKDF2之外,我建议看看scrypt

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

如果必须使用它们,那么至少要对算法进行预定义的重复次数(1000+).


Qw3*_*3ry 5

虽然已经提到了NIST 推荐的 PBKDF2,但我想指出的是,从 2013 年到 2015 年有一场公开的密码哈希竞赛。最终,Argon2被选为推荐的密码哈希函数。

对于您可以使用的原始(本机 C)库,有一个相当不错的Java 绑定

在一般用例中,如果您选择 PBKDF2 而不是 Argon2,我认为从安全角度来看这并不重要,反之亦然。如果您有很强的安全要求,我建议您在评估中考虑 Argon2。

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


use*_*316 5

截至 2020 年,正在使用的最可靠的密码哈希算法(在任何硬件下最有可能优化其强度)是Argon2idArgon2i,但不是其 Spring 实现。

\n

PBKDF2 标准包括分组密码 BCRYPT 算法的 CPU 贪婪/计算昂贵的功能,并添加其流密码功能。PBKDF2 被内存指数贪婪的 SCRYPT 淹没,然后被抗侧通道攻击的 Argon2 淹没

\n

Argon2 提供了必要的校准工具,可以在给定目标哈希时间和所使用的硬件的情况下找到优化的强度参数。

\n
    \n
  • Argon2i专门研究内存贪婪哈希
  • \n
  • Argon2d专门研究 CPU 贪婪哈希
  • \n
  • Argon2id使用这两种方法。
  • \n
\n

内存贪婪哈希将有助于防止 GPU 使用 GPU 进行破解。

\n

Spring security/Bouncy Castle 实现并未优化,考虑到攻击者可能使用的内容,相对较弱。\ncf:Spring doc Argon2Scrypt

\n
\n

当前的实现使用 Bouncy castle,它不利用密码破解者所利用的并行性/优化,因此攻击者和防御者之间存在不必要的不​​对称。

\n
\n

java 使用的最可靠的实现是mkammerer的一个,

\n

用 C 编写的官方本机实现的包装器 jar/库。

\n

它写得很好并且使用简单。

\n

嵌入式版本提供适用于 Linux、Windows 和 OSX 的本机构建。

\n

例如,摩根大通在其tessera安全项目中使用它来保护Quorum(其以太坊加密货币实现)。

\n

这是一个例子:

\n
    final char[] password = "a4e9y2tr0ngAnd7on6P\xc3\xa0\xc2\xa7\xc2\xa7M\xc2\xb0RD".toCharArray();\n    byte[] salt = new byte[128];\n    new SecureRandom().nextBytes(salt);\n    final Argon2Advanced argon2 = Argon2Factory.createAdvanced(Argon2Factory.Argon2Types.ARGON2id);\n    byte[] hash = argon2.rawHash(10, 1048576, 4, password, salt);\n
Run Code Online (Sandbox Code Playgroud)\n

(参见tessera

\n

在 POM 中声明该库:

\n
<dependency>\n    <groupId>de.mkammerer</groupId>\n    <artifactId>argon2-jvm</artifactId>\n    <version>2.7</version>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n

或使用 gradle:

\n
compile \'de.mkammerer:argon2-jvm:2.7\'\n
Run Code Online (Sandbox Code Playgroud)\n

可以使用 de.mkammerer.argon2.Argon2Helper#findIterations进行校准

\n

SCRYPT 和 Pbkdf2 算法也可以通过编写一些简单的基准来校准,但当前的最小安全迭代值将需要更高的哈希时间。

\n