Created
July 6, 2016 06:38
-
-
Save takaki/67b03ef9a3a163bf9b8db18cd40f0dfc to your computer and use it in GitHub Desktop.
HMACをいろんな言語で ref: http://qiita.com/takaki@github/items/8665e6c518e377840495
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
package example.misc; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class HmacDemo { | |
private static final String SECRET = "secret key"; | |
private static final String TEXT = "This is a pen."; | |
public static void main(String[] args) throws Exception { | |
String algo = "HMacSHA256"; | |
final SecretKeySpec keySpec = new SecretKeySpec(SECRET.getBytes(), | |
algo); | |
final Mac mac = Mac.getInstance(algo); | |
mac.init(keySpec); | |
final byte[] signBytes = mac.doFinal(TEXT.getBytes()); | |
for (byte signByte : signBytes) { | |
System.out.printf("%02x", signByte & 0xff); | |
} | |
System.out.print("\n"); | |
} | |
} |
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
#!/usr/bin/python3 | |
import hashlib | |
import hmac | |
SECRET = "secret key" | |
TEXT = "This is a pen." | |
print(hmac.new(bytes(SECRET, 'ascii'), bytes(TEXT, 'ascii'), hashlib.sha256).hexdigest()) |
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
#!/usr/bin/ruby | |
require 'openssl' | |
SECRET = "secret key" | |
TEXT = "This is a pen." | |
puts OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), | |
SECRET, TEXT).unpack("H*") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment