/** * 仅将请求参数作为签名因子进行签名 * * @param params api请求的各参数键值对 * @param appSecretKey * @return */ public static String signatureWithParamsOnly(Map<String, String> params, String appSecretKey) { List<String> paramValueList = new ArrayList<String>(); if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) { paramValueList.add(entry.getKey() + entry.getValue()); } } Collections.sort(paramValueList); String[] datas = new String[paramValueList.size()]; paramValueList.toArray(datas); byte[] signature = SecurityUtil.hmacSha1(datas, StringUtil.toBytes(appSecretKey)); return StringUtil.encodeHexStr(signature); }
/** * 将urlPath和请求参数同时作为签名因子进行签名 * * @param urlPath protocol/version/namespace/name/appKey * @param params api请求的各参数键值对 * @param appSecretKey app签名密钥 * @return */ public static String signatureWithParamsAndUrlPath( String urlPath, Map<String, String> params, String appSecretKey) { List<String> paramValueList = new ArrayList<String>(); if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) { paramValueList.add(entry.getKey() + entry.getValue()); } } final String[] datas = new String[1 + paramValueList.size()]; datas[0] = urlPath; Collections.sort(paramValueList); for (int i = 0; i < paramValueList.size(); i++) { datas[i + 1] = paramValueList.get(i); } byte[] signature = SecurityUtil.hmacSha1(datas, StringUtil.toBytes(appSecretKey)); return StringUtil.encodeHexStr(signature); }