I'm trying to convert a Node.js authentication using passport-local to a Java based one
Node.js use this code:
crypto.pbkdf2Sync(password, new Buffer(this.salt, 'base64'), 10000, 64).toString('base64');
I'm trying to replicate this in java using this code:
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 100000, 64 * 8);
SecretKeyFactory key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hashedPassword = key.generateSecret(spec).getEncoded();
hashPassword = new String(Base64.encodeBase64(hashedPassword));
But my generated key is always different than node.js one. i've also tried with different encodings in getBytes() using UTF-16LE as someone pointed here in other threads but with no luck. I'm pretty sure that pbkdf2Sync in node.js use SHA1 when no digest is used. Can anyone confirm also this?
Thank you
UPDATE
I've resolved using Java8 Base64 decoding. This is the right function to use (if someone needs it):
final char[] cPassword = password.toCharArray();
final byte[] bSalt = Base64.getDecoder().decode(utente.getSalt().getBytes());
KeySpec spec = new PBEKeySpec(cPassword, bSalt, 10000, 64 * 8);
SecretKeyFactory key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hashedPassword = key.generateSecret(spec).getEncoded();
String hashPassword = new String(Base64.getEncoder().encode(hashedPassword));
hashPassword will have the same Node.js password