CDN加速

HMAC-SHA1签名

更新时间:2021-06-17 14:15:44

HMAC-SHA1签名

Java版范例代码

/**
 * HMAC-SHA1加密方案<br>
 * @param content-待加密内容
 * @param secretKey-密钥
 * @return HMAC_SHA1加密后的字符串
 */
publicstatic String HMACSHA1(String content, String secretKey) {
	try {
		byte[] secretKeyBytes = secretKey.getBytes();
		SecretKey secretKeyObj = new SecretKeySpec(secretKeyBytes, "HmacSHA1");
		Mac mac = Mac.getInstance("HmacSHA1");
		mac.init(secretKeyObj);
		byte[] text = content.getBytes("UTF-8");
		byte[] encryptContentBytes = mac.doFinal(text);
		
		//SHA1算法得到的签名长度,都是160位二进制码,换算成十六进制编码字符串表示
		String encryptContent = bytesToHexString(encryptContentBytes);
		
        return encryptContent;  
	} catch (Exception e) {
		e.printStackTrace();
	}
	return content;
}

/**
 * 获取字节数组的16进制字符串表示形式<br>
 * 范例:0xff->'ff'
 * @param bytes 字节数组
 * @return string-16进制的字符串表示形式
 */
privatestatic String bytesToHexString(byte[] bytes) {
	StringBuilder hexString = new StringBuilder("");
	for(byte ib : bytes) {
		char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
		char[] ob = newchar[2];
		ob[0] = Digit[(ib >>> 4) & 0X0f];
		ob[1] = Digit[ib & 0X0F];
		hexString.append(ob);
	}
	return hexString.toString();
 }