Java - 生成随机盐不是随机的
Java - Generating a random salt isn't random
我正在尝试在 Java 中生成盐,以与哈希算法一起使用以实现安全密码存储。我正在使用以下代码创建随机盐:
private static String getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
System.out.println(salt.toString());
return salt.toString();
}
它应该生成一个完全安全的、随机生成的盐以用于我的哈希算法。但是,当我 运行 代码时,它每次都会输出相同的盐...表明生成的盐根本不是随机的。
出于明显的安全目的,每个用户都需要一个唯一的盐,但是如果我在每次创建新帐户时都使用此代码,那么每个用户都会有相同的盐,这就违背了最初的目的。
我的问题是:为什么这一直给我相同的盐,我该怎么做才能确保每次代码生成的盐都是完全随机的 运行?
编辑:
我想我会包含整个散列程序的源代码,该程序现已修复并且可以正常工作。这是一个简单的原型,用于模拟在创建帐户时生成哈希,然后在登录系统时检查密码。
package hashingwstest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;
public class HashingWSTest {
public static void main(String[] args) throws NoSuchAlgorithmException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Password: ");
String passwordToHash = sc.nextLine();
byte[] bytes = getBytes();
String salt = new String(bytes);
String securePassword = hash256(passwordToHash, salt);
System.out.println("Hash successfully generated");
System.out.print("Enter your password again: ");
String checkPassword = sc.nextLine();
String checkHash = hash256(checkPassword,salt);
if (checkHash.equals(securePassword)) {
System.out.println("MATCH");
}
else {
System.out.println("NO MATCH");
}
}
private static String hash256(String passwordToHash, String salt) {
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(salt.getBytes());
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for (int i=0; i<bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return generatedPassword;
}
private static byte[] getBytes() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] bytes = new byte[16];
sr.nextBytes(bytes);
return bytes;
}
}
来自 java.security.SecureRandom.getInstance(String)
的 javadoc:
The returned SecureRandom object has not been seeded. To seed the returned object, call the setSeed method.
所以显而易见的答案是调用 setSeed
。不过如果只用时间的话可能会有问题,因为种子很容易被猜到。
另一种方法是共享安全随机实例(如 it is thread safe)
可以使用方法
/**
* Reseeds this random object, using the eight bytes contained
* in the given <code>long seed</code>. The given seed supplements,
* rather than replaces, the existing seed. Thus, repeated calls
* are guaranteed never to reduce randomness.
*
* <p>This method is defined for compatibility with
* <code>java.util.Random</code>.
*
* @param seed the seed.
*
* @see #getSeed
*/
public void setSeed(long seed)
通过例如当前时间
salt.toString
没有返回字节数组的内容,而是 hashCode
如果您在每次请求时替换为 sr.nextInt()
,您将收到不同的值。
如果打印字节数组的内容,您会注意到差异
您正在打印字节数组本身,而不是它的内容。您需要遍历数组以查看它包含的内容。
编辑:
还将 getSalt 更改为 return 字节数组。 return 从字节数组(使用 new String(salt))构造的字符串是不安全的,因为字节序列可能无法形成有效的字符串。
import java.security.*;
public class Salt {
public static void main(String[] args) throws NoSuchAlgorithmException {
getSalt();
}
private static byte[] getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
for(int i = 0; i<16; i++) {
System.out.print(salt[i] & 0x00FF);
System.out.print(" ");
}
return salt;
}
}
我正在尝试在 Java 中生成盐,以与哈希算法一起使用以实现安全密码存储。我正在使用以下代码创建随机盐:
private static String getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
System.out.println(salt.toString());
return salt.toString();
}
它应该生成一个完全安全的、随机生成的盐以用于我的哈希算法。但是,当我 运行 代码时,它每次都会输出相同的盐...表明生成的盐根本不是随机的。
出于明显的安全目的,每个用户都需要一个唯一的盐,但是如果我在每次创建新帐户时都使用此代码,那么每个用户都会有相同的盐,这就违背了最初的目的。
我的问题是:为什么这一直给我相同的盐,我该怎么做才能确保每次代码生成的盐都是完全随机的 运行?
编辑:
我想我会包含整个散列程序的源代码,该程序现已修复并且可以正常工作。这是一个简单的原型,用于模拟在创建帐户时生成哈希,然后在登录系统时检查密码。
package hashingwstest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;
public class HashingWSTest {
public static void main(String[] args) throws NoSuchAlgorithmException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Password: ");
String passwordToHash = sc.nextLine();
byte[] bytes = getBytes();
String salt = new String(bytes);
String securePassword = hash256(passwordToHash, salt);
System.out.println("Hash successfully generated");
System.out.print("Enter your password again: ");
String checkPassword = sc.nextLine();
String checkHash = hash256(checkPassword,salt);
if (checkHash.equals(securePassword)) {
System.out.println("MATCH");
}
else {
System.out.println("NO MATCH");
}
}
private static String hash256(String passwordToHash, String salt) {
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(salt.getBytes());
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for (int i=0; i<bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return generatedPassword;
}
private static byte[] getBytes() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] bytes = new byte[16];
sr.nextBytes(bytes);
return bytes;
}
}
来自 java.security.SecureRandom.getInstance(String)
的 javadoc:
The returned SecureRandom object has not been seeded. To seed the returned object, call the setSeed method.
所以显而易见的答案是调用 setSeed
。不过如果只用时间的话可能会有问题,因为种子很容易被猜到。
另一种方法是共享安全随机实例(如 it is thread safe)
可以使用方法
/**
* Reseeds this random object, using the eight bytes contained
* in the given <code>long seed</code>. The given seed supplements,
* rather than replaces, the existing seed. Thus, repeated calls
* are guaranteed never to reduce randomness.
*
* <p>This method is defined for compatibility with
* <code>java.util.Random</code>.
*
* @param seed the seed.
*
* @see #getSeed
*/
public void setSeed(long seed)
通过例如当前时间
salt.toString
没有返回字节数组的内容,而是 hashCode
如果您在每次请求时替换为 sr.nextInt()
,您将收到不同的值。
如果打印字节数组的内容,您会注意到差异
您正在打印字节数组本身,而不是它的内容。您需要遍历数组以查看它包含的内容。
编辑:
还将 getSalt 更改为 return 字节数组。 return 从字节数组(使用 new String(salt))构造的字符串是不安全的,因为字节序列可能无法形成有效的字符串。
import java.security.*;
public class Salt {
public static void main(String[] args) throws NoSuchAlgorithmException {
getSalt();
}
private static byte[] getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
for(int i = 0; i<16; i++) {
System.out.print(salt[i] & 0x00FF);
System.out.print(" ");
}
return salt;
}
}