Answers for "hash sha256 java"

1

java sha256 hex digest

import org.apache.commons.codec.digest.DigestUtils;

String password = "123456";
String result = DigestUtils.sha256Hex(password);
Posted by: Guest on November-08-2020
0

hash_hmac sha256 in java

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class ApiSecurityExample {
  public static void main(String[] args) {
    try {
     String secret = "secret";
     String message = "Message";

     Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
     SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
     sha256_HMAC.init(secret_key);

     String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
     System.out.println(hash);
    }
    catch (Exception e){
     System.out.println("Error");
    }
   }
}
Posted by: Guest on March-06-2021
-1

java sha256 hex digest

String password = "123456";

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[]hashInBytes = md.digest(password.getBytes(StandardCharsets.UTF__8));

//bytes to hex
StringBuilder sb = new StringBuilder();
for (byte b : hashInBytes) {
	sb.append(String.format("%02x", b));
}
System.out.println(sb.toString());
Posted by: Guest on November-08-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language