您现在的位置是:首页 > 电脑 > 

使用JAVA实现PHP中hash_hmac 函数

2025-07-27 15:54:33
package com.trade796; import java.security.InvalidKeyException; import java.security.oSuchAlgorithmException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; /** * 1. hmac_sha1编码结
package ;

import java.security.InvalidKeyException;
import java.;
import Mac;
import spec.SecretKeySpec;

/**
 * 1. hmac_sha1编码结果需要转换成hex格式
 * 
 * 2. java中base64的实现和php不一致,其中java并不会在字符串末尾填补=号以把字节数补充为8的整数
 * 
 * . hmac_sha1并非sha1, hmac_sha1是需要共享密钥的
 * 
 * @author LEI
 * 
 */
public class HMACSHA1 {

	private static final String HMAC_SHA1 = HmacSHA1;

	/**
	 * 生成签名数据_HmacSHA1加密
	 * 
	 * @param data
	 *            待加密的数据
	 * @param key
	 *            加密使用的key
	 * @throws InvalidKeyException
	 * @throws oSuchAlgorithmException
	 */
	public static String getSignature(String data, String key) throws Exception {

		byte[] keyBytes = key.getBytes();
		// 根据给定的字节数组构造一个密钥。
		SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1);
		Mac mac = Mac.getInstance(HMAC_SHA1);
		mac.init(signingKey);

		byte[] rawHmac = mac.doFinal(data.getBytes());

		String hexBytes = byte2hex(rawHmac);
		return hexBytes;
	}

	private static String byte2hex(final byte[] b) {
		String hs = ;
		String stmp = ;
		for (int n = 0; n < b.length; n) {
			// 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
			stmp = (java.lang.(b[n] & 0xFF));
			if (stmp.length() == 1) {
				hs = hs  0  stmp;
			} else {
				hs = hs  stmp;
			}
		}
		return hs;
	}

	// /**
	// * @param args
	// */
	// public static void main(String[] args) {
	// try {
	// println(HMACSHA1.getSignature(abc, abc));
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

}

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/70434.html

相关标签:无
上传时间: 2023-04-24 14:55:15
留言与评论(共有 10 条评论)
本站网友 如何提高受孕率
0秒前 发表
String key) throws Exception { byte[] keyBytes = key.getBytes(); // 根据给定的字节数组构造一个密钥
本站网友 哈尔滨人流多少钱
25分钟前 发表
HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes()); String hexBytes = byte2hex(rawHmac); return hexBytes; } private static String byte2hex(final byte[] b) { String hs = ; String stmp = ; for (int n = 0; n < b.length; n) { // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式
本站网友 长春搜房网租房
17分钟前 发表
String key) throws Exception { byte[] keyBytes = key.getBytes(); // 根据给定的字节数组构造一个密钥
本站网友 拜腾官网
19分钟前 发表
stmp = (java.lang.(b[n] & 0xFF)); if (stmp.length() == 1) { hs = hs  0  stmp; } else { hs = hs  stmp; } } return hs; } // /** // * @param args // */ // public static void main(String[] args) { // try { // println(HMACSHA1.getSignature(abc
本站网友 哪种婴儿奶粉好
25分钟前 发表
hmac_sha1是需要共享密钥的 * * @author LEI * */ public class HMACSHA1 { private static final String HMAC_SHA1 = HmacSHA1; /** * 生成签名数据_HmacSHA1加密 * * @param data * 待加密的数据 * @param key * 加密使用的key * @throws InvalidKeyException * @throws oSuchAlgorithmException */ public static String getSignature(String data
本站网友 西京医院网上挂号
0秒前 发表
hmac_sha1是需要共享密钥的 * * @author LEI * */ public class HMACSHA1 { private static final String HMAC_SHA1 = HmacSHA1; /** * 生成签名数据_HmacSHA1加密 * * @param data * 待加密的数据 * @param key * 加密使用的key * @throws InvalidKeyException * @throws oSuchAlgorithmException */ public static String getSignature(String data
本站网友 肺在哪个位置图
5分钟前 发表
SecretKeySpec signingKey = new SecretKeySpec(keyBytes
本站网友 纽交所上市
19分钟前 发表
hmac_sha1是需要共享密钥的 * * @author LEI * */ public class HMACSHA1 { private static final String HMAC_SHA1 = HmacSHA1; /** * 生成签名数据_HmacSHA1加密 * * @param data * 待加密的数据 * @param key * 加密使用的key * @throws InvalidKeyException * @throws oSuchAlgorithmException */ public static String getSignature(String data
本站网友 重庆中信银行
19分钟前 发表
HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes()); String hexBytes = byte2hex(rawHmac); return hexBytes; } private static String byte2hex(final byte[] b) { String hs = ; String stmp = ; for (int n = 0; n < b.length; n) { // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式