Created
September 14, 2015 12:14
-
-
Save gaurish/6dd52b69d6ae9b16341c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment