Hmac-SHA1
1.
In C#:
using System;
using System.Security.Cryptography;
using System.Text;
private static string HmacSHA1(string input, string secretKey)
{
var enc = Encoding.UTF8;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
byte[] hash = hmac.ComputeHash(enc.GetBytes(input));
return Convert.ToBase64String(hash);
}
2.
In Java:
import java.security.SignatureException;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public static String hmacSHA1(String value, String
secretKey) throws java.security.SignatureException {
String result;
try {
String HMAC_SHA1_ALGORITHM = "HmacSHA1";
SecretKeySpec
signingKey = new
SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);
Mac
mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] hash = mac.doFinal(value.getBytes());
result = new
String(Base64.getEncoder().encode(hash));
} catch (Exception e) {
throw new
SignatureException("Failed
to generate HMAC : " + e.getMessage());
}
return result;
}
3.
In Python:
import base64
import hashlib
import hmac
def _hmac_sha1(input_str,key):
inputRaw = input_str.encode("utf-8")
keyRaw = key.encode('utf-8')
hashed = hmac.new(keyRaw, inputRaw,
hashlib.sha1)
return base64.encodebytes(hashed.digest()).decode('utf-8')
4.
In dart
import 'dart:convert';
import 'package:crypto/crypto.dart' as crypto;
static String hmacSHA1(String input, String secretKey) {
var bytes = utf8.encode(input);
var key = utf8.encode(secretKey);
crypto.Hmac hmacSha1 = new crypto.Hmac(crypto.sha1, key);
crypto.Digest digest = hmacSha1.convert(bytes);
return base64.encode(digest.bytes);
}
Không có nhận xét nào:
Đăng nhận xét