Signature là một chuỗi ký tự được tạo ra từ một thuật toán cho trước, sử dụng để kiểm tra tính đúng đắn của dữ liệu trên đường truyền giữa 2 hệ thống. Một số thuật toán đang sử dụng là MD5, SHA1, SHA256 và Hmac.
Trong tài liệu này VZpay sử dụng các yếu tố sau để tạo Signature :
Dữ liệu đầu vào bao gồm SecretKey & data.
Thuật toán: RSA – Sign HASH.
HASH: HmacSHA512.
Keysize: 2048.
Cách tạo chữ ký điện tử:
Truyền dữ liệu: (lưu ý: Chỉmã hoá các trường thông tin dưới đây & truyền theo đúng thứ tự các giá trị)
publicstaticStringhmacSHA512(finalString key,finalString data) {try {if (key ==null|| data ==null) {thrownewNullPointerException(); }finalMac hmac512 =Mac.getInstance("HmacSHA512");byte[] hmacKeyBytes =key.getBytes();finalSecretKeySpec secretKey =newSecretKeySpec(hmacKeyBytes,"HmacSHA512");hmac512.init(secretKey);byte[] dataBytes =data.getBytes(StandardCharsets.UTF_8);byte[] result =hmac512.doFinal(dataBytes);StringBuilder sb =newStringBuilder(2*result.length);for (byte b : result) {sb.append(String.format("%02x", b &0xff)); }returnsb.toString(); } catch (Exception ex) {returnex.getMessage(); }}
functionhmacSHA512($key, $data) {if ($key ===null|| $data ===null) {thrownewException('Key and data cannot be null'); } $hmacKeyBytes =utf8_decode($key); $dataBytes =utf8_decode($data); $result =hash_hmac('sha512', $dataBytes, $hmacKeyBytes,true);returnbin2hex($result);}// Example usage:$key ="your_secret_key";$data ="your_data";$result =hmacSHA512($key, $data);echo $result;
usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;classProgram{staticvoidMain() {string key ="your_secret_key";string data ="your_data";string result =HmacSHA512(key, data);Console.WriteLine(result); }staticstringHmacSHA512(string key,string data) {if (key ==null|| data ==null) {thrownewArgumentNullException("Key and data cannot be null"); }byte[] hmacKeyBytes =Encoding.UTF8.GetBytes(key);byte[] dataBytes =Encoding.UTF8.GetBytes(data);using (HMACSHA512 hmac =newHMACSHA512(hmacKeyBytes)) {byte[] resultBytes =hmac.ComputeHash(dataBytes);returnBitConverter.ToString(resultBytes).Replace("-","").ToLower(); } }}
constcrypto=require('crypto');functionhmacSHA512(key, data) {if (key ===null|| data ===null) {thrownewError('Key and data cannot be null'); }consthmac=crypto.createHmac('sha512',Buffer.from(key,'utf-8'));hmac.update(Buffer.from(data,'utf-8'));constresultBytes=hmac.digest();returnresultBytes.toString('hex');}// Example usage:constkey='your_secret_key';constdata='your_data';constresult=hmacSHA512(key, data);console.log(result);