签名方法
测试环境logistic_provider_id和secretKey线下提供
如果使用正确的签名方法有时能请求成功有时报签名失败,可能是发请求时自动urldecode导致+变成了空格,可以对data_digest和logistics_interface进行urlencode后再发送请求JAVA
/**
*
* @param content 加密报文
* @param charset 编码,UTF-8
* @param secretKey 密钥
* @return
*/
public static String doSign(String content, String charset, String secretKey) {
String sign = "";
content = content + secretKey;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(content.getBytes(charset));
sign = new String(Base64.encodeBase64(md.digest()), charset);
} catch (Exception e) {
throw new RuntimeException(e);
}
return sign;
}
PHP
$content = [ "testkey" => "testkey", "testdata" => "testdata" ];
$appSecret = '8026ny111111111111'; // 秘钥
$digest = base64_encode(md5(json_encode($content).$appSecret, true)); //生成签名
C#
public static string DoSign(string content, string charset, string appSecret)
{
string toSignContent = content + appSecret;
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.GetEncoding(charset).GetBytes(toSignContent);
byte[] hash = md5.ComputeHash(inputBytes);
return System.Convert.ToBase64String(hash);
}