https://ideone.com/RACrQH 出力結果:YKJZs5xPTHmotKAw4/tzwciAyis=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* package whatever; // don't place package name! */ import java.util.*; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class Main { public static void main(String[] args) throws Exception { String algo = "HMacSHA1"; String keySource = "qwertyuiopasdfghjklz"; String str = "hogehoge"; final SecretKeySpec keySpec = new SecretKeySpec(keySource.getBytes(), algo); final Mac mac = Mac.getInstance(algo); mac.init(keySpec); final byte[] signBytes = mac.doFinal(str.getBytes()); String encoded = Base64.getEncoder().encodeToString(signBytes); System.out.print(encoded); } } |
https://ideone.com/5QRDJZ 出力結果:YKJZs5xPTHmotKAw4/tzwciAyis=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Text; using System.Security.Cryptography; namespace TestCSharpHMAC { public class Program { public static void Main(string[] args) { string hash = ""; string str = "hogehoge"; //HMAC鍵 string keySource = "qwertyuiopasdfghjklz"; byte[] HmacKey = Encoding.UTF8.GetBytes(keySource); HMACSHA1 mac = new HMACSHA1(HmacKey); byte[] hashValue = mac.ComputeHash(Encoding.UTF8.GetBytes(str)); hash = Convert.ToBase64String(hashValue); Console.WriteLine(hash); } } } |