Signature Description
When you decided to use page integration, you need to sign the parameters. Generating a signature is divided into two steps:
Step 1: Confirm the encrypted string participating in the signature
You need to sort the fields that need to be signed in the parameters you pass in by first letter to generate a signature string(if the first letters are the same, sort by the second letter, and so on).
Step 2: Generate your signature
Generate a signature as below.
package com.example;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.TreeMap;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.util.Random;
public class nft {
private final static String CHARSET = "UTF-8";
public static String sign(String s, String key) throws Exception {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(CHARSET), mac.getAlgorithm());
mac.init(secretKeySpec);
byte[] hash = mac.doFinal(s.getBytes(CHARSET));
return DatatypeConverter.printBase64Binary(hash);
}
public static void main(String[] args) throws Exception {
final String APP_ID = "ahzxh0klegv1fzol";
final String APP_SECRET = "py2bwighth62ajq6";
String signStr = "amount=3000&appId=ahzxh0klegv1fzol&callbackUrl=https://alchemypay.org&fiat=USD&merchantName=merchantName&merchantOrderNo=ACH100001234&name=nftname&nonce=1862325104&picture=https://download.bit.store/official/BitStore/pic/user_portrait/20.jpeg&redirectUrl=https://alchemypay.org&targetFiat=SGD&timeout=1675394255000×tamp=1675417608&type=MARKET&uniqueId=1113"
String signture = sign(signStr, APP_SECRET); // 公共参数
System.out.println("signture is: " + signture);
}
}
//node v14.15.1
const crypto = require('crypto');
function encrypt(plainText, secretKeyData) {
try {
var hmac = crypto.createHmac("sha1", secretKeyData);
var signed = hmac.update(Buffer.from(plainText, 'utf-8')).digest("base64");
return signed;
} catch (e) {
console.log(`HmacSHA1 encrypting exception, msg is ${e.toString()}`);
}
return null;
}
let key = "py2bwighth62ajq6";
const plaintext = "amount=3000&appId=ahzxh0klegv1fzol&callbackUrl=https://alchemypay.org&fiat=USD&merchantName=merchantName&merchantOrderNo=ACH100001234&name=nftname&nonce=28518016&picture=https://download.bit.store/official/BitStore/pic/user_portrait/20.jpeg&redirectUrl=https://alchemypay.org&targetFiat=SGD&timeout=1675394255000×tamp=1676525212&type=MARKET&uniqueId=1113";
const ciphertext = encrypt(plaintext,"py2bwighth62ajq6"); //appSecret
console.log("sign is ",ciphertext);
const urlEncodeText = encodeURIComponent(ciphertext)
console.log("encode sign is ",urlEncodeText);
#Python 3.7.4
import base64
import urllib
import requests
import base64
import hashlib
import hmac
def encrypt(key, s, method):
hmac_str = hmac.new(key.encode("utf8"), s.encode("utf8"), method).digest()
return base64.b64encode(hmac_str)
secret_key="py2bwighth62ajq6"
rawdata = rawData = "amount=3000&appId=ahzxh0klegv1fzol&callbackUrl=https://alchemypay.org&fiat=USD&merchantName=merchantName&merchantOrderNo=ACH100001234&name=nftname&nonce=28518016&picture=https://download.bit.store/official/BitStore/pic/user_portrait/20.jpeg&redirectUrl=https://alchemypay.org&targetFiat=SGD&timeout=1675394255000×tamp=1676525212&type=MARKET&uniqueId=1113"
sign = encrypt(secret_key, rawData, hashlib.sha1)
print("sign is:", sign)
urlEncodeData = urllib.parse.quote_plus(sign)
print("encode sign is: ", urlEncodeData)
//PHP 8.0.7
<?php
function encrypt($plaintext,$secretKey){
$plaintextData = utf8_encode($plaintext);
$secretKeyData = utf8_encode($secretKey);
$ciphertext = hash_hmac('sha1', $plaintextData, $secretKeyData,true);
return base64_encode($ciphertext);
}
$data = "amount=3000&appId=ahzxh0klegv1fzol&callbackUrl=https://alchemypay.org&fiat=USD&merchantName=merchantName&merchantOrderNo=ACH100001234&name=nftname&nonce=28518016&picture=https://download.bit.store/official/BitStore/pic/user_portrait/20.jpeg&redirectUrl=https://alchemypay.org&targetFiat=SGD&timeout=1675394255000×tamp=1676525212&type=MARKET&uniqueId=1113";
$key = 'py2bwighth62ajq6';
$sign = encrypt($data,$key);
printf("sign is: %s\n",$sign);
$urlEncodeData = urlencode($sign);
printf("encode sign is:: %s",$urlEncodeData);
?>
//go version go1.18.2
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/url"
)
func encrypt(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha1.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString([]byte(h.Sum(nil)))
}
func main() {
rawData := "amount=3000&appId=ahzxh0klegv1fzol&callbackUrl=https://alchemypay.org&fiat=USD&merchantName=merchantName&merchantOrderNo=ACH100001234&name=nftname&nonce=28518016&picture=https://download.bit.store/official/BitStore/pic/user_portrait/20.jpeg&redirectUrl=https://alchemypay.org&targetFiat=SGD&timeout=1675394255000×tamp=1676525212&type=MARKET&uniqueId=1113"
key := "py2bwighth62ajq6"
sign := encrypt(rawData, key)
fmt.Printf("sign is %s \n", sign)
encodeSign := url.QueryEscape(sign)
fmt.Printf("encode sign is %s \n", encodeSign)
}
Updated 3 months ago