1) 使用请求报文[body]、签名密钥[signature]、时间戳[ts],计算得出签名信息[sign],签名规则如下:
a.报文前拼接签名密钥,后面拼接时间戳(毫秒)字符串:
signature+ body + ts
signature: 签名密钥;
body: 请求JSON报文;
ts: 时间戳,毫秒;
b.使用SHA-256算法计算HASH值;
2) 使用产品token、签名信息[sign]、时间戳[ts]、请求报文[body],发送API调用请求。
3.3.1 SHA256签名算法
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * encode By SHA-256 * @param str * @return */ public static String encodeBySHA256(String str) { if (str == null) { return null; } try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); messageDigest.reset(); messageDigest.update(str.getBytes(“UTF-8”)); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } /** * Takes the raw bytes from the digest and formats them correct. * * @param bytes the raw bytes from the digest. * @return the formatted bytes. */ private static String getFormattedText(byte[] bytes) { int len = bytes.length; StringBuilder buf = new StringBuilder(len * 2); // 把密文转换成十六进制的字符串形式 for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); buf.append(HEX_DIGITS[bytes[j] & 0x0f]); } return buf.toString(); }