• 开发者平台
  • >
  • 餐饮文档中心

签名规则

上下行接口sign计算规则一样(详见签名示例)

1.加密步骤

  1. 公共参数(除去sign)按照参数名ASCII码进行自然排序,然后按照k1v1k2v2…拼接,结果为parameters;
  2. 拼接parameters+token;
  3. 将所得字符串进行SHA-256运算,返回即为sign的值。

排序后再组装参数字符串,将客如云提供的token放在排序字符串的最后面,对新组装的参数字符串进行SHA-256加密生成sign字符串。生成的sign参数放到请求接口参数后面当成普通参数即可,参数名为sign

2.客如云提供接口签名规则

加密规则

sign=SHA256(appKey+shopIdenty+timestamp+version+token)

注意: sign一定要按顺序拼接字段

加密规则示例

{
  appKey:7857ca1808d370e2501290bc853eecdc
  shopIdenty:810094162
  timestamp:1528683797798
  version:1.0
  token:66e53b22f1496d183e71b4ab90f4acf7

  sign=SHA256(appKey7857ca1808d370e2501290bc853eecdcshopIdenty810094162timestamp1528683797798version1.066e53b22f1496d183e71b4ab90f4acf7)

}

参数拼接并排序 appKey301001shopIdenty247900001timestamp1425635264version1.0${TOKEN}【token值直接跟在version1.0后面,中间没有任何符号隔开】

3.签名规则描述示例

(1)公共参数

https://openapi.keruyun.com/open/v1/signTest?appKey=301001&shopIdenty=247900001&version=1.0&timestamp=1425635264&sign

SHA256运算得到签名sign sign=SHA256(appKey301001shopIdenty247900001timestamp1425635264version1.066e53b22f1496d183e71b4ab90f4acf7)

如上计算完成签名之后,把sign结果放到URL参数里面:

/open/v1/signTest?appKey=301001&shopIdenty=247900001&version=1.0&timestamp=1425635264&sign=acb4355599742492518363653dd060b818ef7813284e823dcf5a61d722f27771

(2)业务参数

{
"orderId": "f14cfdb04119451998200d1a83f785da"
}

签名示例(JAVA)

/**
 * @Description: 获取token时签名验证(只在获取token时调用一次)
 * @param
 * @throws
 */
public void signForToken() {
		Map<String, Object> params = new TreeMap<>();
		params.put("appKey", "301001");
		params.put("shopIdenty", 247900001);
		params.put("version", "1.0");
		params.put("timestamp", 1425635264);
		StringBuilder sortedParams = new StringBuilder();
		params.entrySet().stream().forEachOrdered(paramEntry -> sortedParams.append(paramEntry.getKey()).append(paramEntry.getValue()));
		sortedParams.append("secretKey");//请替换成真实的secretKey
		String SHA256Sign = null;
		try {
			SHA256Sign = getSign(sortedParams.toString());
		} catch (NoSuchAlgorithmException e) {
			logger.info("获取签名出错" + e.getMessage(), e);
		}
		if (!StringUtils.equals(sign, SHA256Sign)) {// 签名校验
			String msg = String.format("sign=%s", sign);
			System.out.println("签名校验不通过": + msg);

        }
	}

	/**
	* 普通接口加密,获取到token之后
	**/
	public static void main(String[] args) {
		Map<String, Object> params = new TreeMap<>();
		params.put("appKey", "301001");
		params.put("shopIdenty", 247900001);
		params.put("version", "1.0");
		params.put("timestamp", 1425635264);
		StringBuilder sortedParams = new StringBuilder();
		params.entrySet().stream().forEachOrdered(paramEntry -> sortedParams.append(paramEntry.getKey()).append(paramEntry.getValue()));
		sortedParams.append("TOKEN");//请替换成真实的token
		System.out.println(sortedParams);
		try {
			String sign = getSign(sortedParams.toString());
			System.out.println(sign + "       " + sign.length());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

		/**
	 	* @Description: SHA256加密字符串
		 * @param
		 * @return String
		 * @throws NoSuchAlgorithmException
		 */
	private static String getSign(String sortedParams) throws NoSuchAlgorithmException {
		MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(sortedParams.getBytes());
        byte byteBuffer[] = messageDigest.digest();
        StringBuffer strHexString = new StringBuffer();
        for (int i = 0; i < byteBuffer.length; i++){
        	String hex = Integer.toHexString(0xff & byteBuffer[i]);
        	if (hex.length() == 1) {
        		strHexString.append('0');
        	}
        	strHexString.append(hex);
        }
        // 得到返回結果
        String SHA256Sign = strHexString.toString();
        return SHA256Sign;
	}

目录