hash hmac sha256 in java

Solutions on MaxInterview for hash hmac sha256 in java by the best coders in the world

showing results for - "hash hmac sha256 in java"
Clotilde
02 Jan 2020
1import javax.crypto.Mac;
2import javax.crypto.spec.SecretKeySpec;
3import org.apache.commons.codec.binary.Base64;
4
5public class ApiSecurityExample {
6  public static void main(String[] args) {
7    try {
8     String secret = "secret";
9     String message = "Message";
10
11     Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
12     SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
13     sha256_HMAC.init(secret_key);
14
15     String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
16     System.out.println(hash);
17    }
18    catch (Exception e){
19     System.out.println("Error");
20    }
21   }
22}
23