· 8 years ago · Dec 28, 2017, 02:56 PM
1import java.util.*;
2import javax.crypto.*;
3import javax.crypto.spec.*;
4
5public class Test {
6 public static void main (String[] args) throws Exception {
7 String secretAccessKey = "mykey";
8 String data = "my data";
9 byte[] secretKey = secretAccessKey.getBytes();
10 SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
11 Mac mac = Mac.getInstance("HmacSHA256");
12 mac.init(signingKey);
13 byte[] bytes = data.getBytes();
14 byte[] rawHmac = mac.doFinal(bytes);
15 System.out.println(Base64.getUrlEncoder().encodeToString(rawHmac));
16 }
17}
18
19using System;
20using System.Security.Cryptography;
21using System.Text;
22
23class Test
24{
25 static void Main()
26 {
27 String secretAccessKey = "mykey";
28 String data = "my data";
29 byte[] secretKey = Encoding.UTF8.GetBytes(secretAccessKey);
30 HMACSHA256 hmac = new HMACSHA256(secretKey);
31 hmac.Initialize();
32 byte[] bytes = Encoding.UTF8.GetBytes(data);
33 byte[] rawHmac = hmac.ComputeHash(bytes);
34 Console.WriteLine(Convert.ToBase64String(rawHmac));
35 }
36}