I'm moving the hash logic to convert a project developed in Node.js to Spring Boot.
The digest value and salt value generated in Node.js are stored in the DB, and when I search for the salt value stored in the DB for the same password and verify it with Java, different values are displayed. Is there a problem with my code?
When creating in Node.js
crypto.randomBytes(64,(err,buf)=>{ if(err){ logger.error(err); throw(err); } const salt = buf.toString('base64'); crypto.pbkdf2(login_pw,salt,100000,64,'sha256',(err2,key)=>{ if(err2){ logger.error(err2); throw(err2); } const hashepassword = key.toString('base64'); Result:
password:
a12345678!digest:
SulzpU0gAlevyU/HVUegl1SCvu1u/U5ie1VT9GM+9XYVo1KKzUDVeeUwRsjioRF3Kwk2LV7QRcKs4Iy+0L2dxQ==salt:
bMUfGq65zn5ifDIWB7tE0WHT1fSi1H8VzRWa4WIQkssraVGVIEdoIf8Oj0jZ0PkMCcAl6lDNYms5/MSFf4KppA==
When generating a digest in Java with a salt value generated using Node.js and stored in the DB
public String matches(String salt_str,String password){ try{ byte[] salt = Base64.getDecoder().decode(salt_str); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(),salt,100000,64 * 8); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); byte[] key = factory.generateSecret(spec).getEncoded(); return Base64.getEncoder().encodeToString(key); } catch (Exception e){ throw new RuntimeException(e); } } Result:
- digest:
rPS8XqPOcq5PYOTriSs17kfpr/QeRFvyaBMZESx+fsq/qW3/ze57m7Og1DijYL6mNlNGt0BVWJbx5o10xWduNA==
How do I make the digest values generated in Node.js the same as in Java?