Created
September 8, 2015 22:06
-
-
Save garcia-jj/f7188fdcf8ff136b7a25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package ot.security; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.security.Key; | |
import java.security.SecureRandom; | |
import java.util.Base64; | |
import java.util.Base64.Encoder; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class MacTest { | |
public static void main(String[] args) throws Exception { | |
Path macKey = Paths.get("/tmp/crypt/mac.key"); | |
if (Files.notExists(macKey)) { | |
final byte[] keybytes0 = new byte[512]; | |
new SecureRandom().nextBytes(keybytes0); | |
Files.write(macKey, keybytes0); | |
} | |
final Encoder encoder = Base64.getEncoder(); | |
final byte[] keybytes = Files.readAllBytes(macKey); | |
Key key = new SecretKeySpec(keybytes, "HMACSHA512"); | |
Mac mac = Mac.getInstance("HMACSHA512"); | |
mac.init(key); | |
System.out.println(mac.getMacLength()); | |
byte[] hashValue = mac.doFinal("the lazy dog jumps over the quick brown fox".getBytes()); | |
System.out.println(encoder.encodeToString(hashValue)); | |
System.out.println(encoder.encodeToString(key.getEncoded())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment