签名说明

当您通过网页接入的方式集成时,需要对您传入的参数进行进行签名。生成签名分为两步:

第一步:确认参与签名的加密串

您需要将您传入的参数中需要参与签名的字段按照首字母排序,生成签名串。(如果首字母相同按照第二个字母排序,以此类推)

第二步:生成您的签名

按照以下方法生成签名

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&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=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) }

Did this page help you?