-3

Below code is in Node.js Which library in java I can use for same purpose?

const crypto = require("crypto"); let cryptkey = crypto.createHash('sha256').update('Nixnogen').digest();

Sagar
  • 1
  • 2

2 Answers2

0

As explained in this question, you can use:

 MessageDigest digest = MessageDigest.getInstance("SHA-256");
 byte[] hash = digest.digest("Nitrogen".getBytes(StandardCharsets.UTF_8));
Community
  • 1
  • 1
Derlin
  • 9,572
  • 2
  • 32
  • 53
0

You can refer to javax.crypto.SecretKeyFactory for hashing.

sample code for hashing is as below :

String algorithm = "your algorithm value";//ex:PBKDF2WithHmacSHA512
String salt="randomString";
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
PBEKeySpec spec = new PBEKeySpec(password, salt.getBytes(), 1000,256);
SecretKey key = skf.generateSecret(spec);
byte[] res = key.getEncoded();
String hashedPassword = res.toString();