【问题标题】:Why is my PBKDF2 hashing method returning a different output?为什么我的 PBKDF2 散列方法返回不同的输出?
【发布时间】:2021-07-12 00:56:48
【问题描述】:

对于我的密码学初学者水平,我深表歉意。
在处理我的项目时,我发现需要在将密码插入数据库之前对其进行哈希处理,因此我搜索了顶级哈希算法并发现了 PBKFD2 和 salt 方法。

我这样做的方法是生成随机的 64char Salt 并使用它来散列密码。然后将散列密码和盐存储在我的数据库中。 (PS:注册时也会生成密码。NOT MY CHOICE)

登录后,我的 Servlet 获取密码,然后使用存储在数据库中的盐对其进行哈希处理。然而我得到不同的结果 这是我的代码。

 public static String generatePassword() {
          CharacterRule alphabets = new CharacterRule(EnglishCharacterData.Alphabetical);
          CharacterRule digits = new CharacterRule(EnglishCharacterData.Digit);
          

          PasswordGenerator passwordGenerator = new PasswordGenerator();
          String password = passwordGenerator.generatePassword(10, alphabets, digits);
          return password ; 
       }
       
       
       public static byte[] hashPassword( final char[] password, final byte[] salt, final int iterations, final int keyLength ) throws UnsupportedEncodingException {

            try {
                SecretKeyFactory skf = SecretKeyFactory.getInstance( "PBKDF2WithHmacSHA512" );
                PBEKeySpec spec = new PBEKeySpec( password, salt, iterations, keyLength );
                SecretKey key = skf.generateSecret( spec );
                byte[] res = key.getEncoded( );
                return res;
            } catch ( NoSuchAlgorithmException | InvalidKeySpecException e ) {
                throw new RuntimeException( e );
            }
        }
       
       public static HashMap<Integer,String> hashGeneeratedPassword(String password) throws UnsupportedEncodingException {
            HashMap<Integer,String> returnValues = new HashMap<Integer,String>() ; 
            int iterations = 20000;
            int keyLength = 512;
            char[] passwordChars = password.toCharArray();
            byte[] saltBytes = generateSalt() ; 

            byte[] hashedBytes = hashPassword(passwordChars, saltBytes, iterations, keyLength);
            String hashedString = HexUtils.toHexString(hashedBytes); 
            String saltString = HexUtils.toHexString(saltBytes) ;
            
            returnValues.put(1,hashedString); 
            returnValues.put(2, saltString);
            
            return returnValues ; 
        }
       
       private static byte[] generateSalt() {
            SecureRandom random = new SecureRandom();
            byte[] salt = new byte[32];
            random.nextBytes(salt);
            return salt;
        }
       
       public static String hashLoginPassword(String password,String salt) throws UnsupportedEncodingException {
            
            int iterations = 20000;
            int keyLength = 512;
            char[] passwordChars = password.toCharArray();
            byte[] saltBytes = salt.getBytes();

            byte[] hashedBytes = hashPassword(passwordChars, saltBytes, iterations, keyLength);
            String hashedString = HexUtils.toHexString(hashedBytes); 

            return hashedString ; 
        }

感谢您花时间阅读我的帖子。

【问题讨论】:

  • 用户第一次注册(或更改密码)的时候就是你创建 salt 的时候。此后,每当用户尝试登录时,您都会从数据库中检索此盐,并与密码一起重新计算哈希,然后与存储的哈希进行比较以验证用户身份。您不会为每次登录生成全新的随机盐。
  • 我知道这一点。这就是我创建 hashLoginPassword(password, salt) 的原因。它使用从数据库中检索到的自己的盐对用户输入的密码进行哈希处理。 Salt 仅在通过 hashGeneratedPassword(String password) 注册时生成,它返回生成的盐和散列以存储在我的数据库中。

标签: java hash salt pbkdf2


【解决方案1】:

解决方案: 我用于转换 byte[] salt 和 byte[] hash 的工具是错误的。我使用了 HexUtil,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多