Skip to content

Commit 3cce0a5

Browse files
committed
HMAC-SHA1签名算法
1 parent 3fbf791 commit 3cce0a5

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
* [Blowfish加密与解密](src/cn/aofeng/demo/encrypt/Blowfish.java)
112112
* [AES加密与解密](src/cn/aofeng/demo/encrypt/AES.java)
113113
* [Blowfish, AES性能比较](src/cn/aofeng/demo/encrypt/PerformanceCompare.java)
114+
* [HMAC-SHA1签名算法](src/cn/aofeng/demo/encrypt/HmacSha1.java)
114115

115116
#Java企业开发
116117
##开源框架

src/cn/aofeng/demo/encrypt/EncryptAndDecrypt.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
public class EncryptAndDecrypt {
2222

23-
private final static String CHARSET = "utf8";
23+
public final static String CHARSET = "utf8";
2424

2525
/**
2626
* 创建安全密钥。
@@ -46,8 +46,8 @@ public SecretKey createSecretKey(String encryptType, String keyStr)
4646
*
4747
* @param encryptType 加密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
4848
* @param secretKey 密钥
49-
* @param srcData 待加密的数据
50-
* @return 数据加密后的字节数组
49+
* @param srcData 待加密的源数据
50+
* @return 加密后的二进制数据(字节数组)
5151
* @see #encrypt(String, SecretKey, String, String)
5252
*/
5353
public byte[] encrypt(String encryptType, SecretKey secretKey,
@@ -58,6 +58,15 @@ public byte[] encrypt(String encryptType, SecretKey secretKey,
5858
return encrypt(encryptType, secretKey, srcData, null);
5959
}
6060

61+
/**
62+
* 加密数据。
63+
*
64+
* @param encryptType 加密类型,如:AES/CBC/PKCS5Padding
65+
* @param secretKey 密钥
66+
* @param srcData 待加密的源数据
67+
* @param algorithmParam 某些加密算法的附加参数
68+
* @return 加密后的二进制数据(字节数组)
69+
*/
6170
public byte[] encrypt(String encryptType, SecretKey secretKey,
6271
String srcData, String algorithmParam) throws InvalidKeyException,
6372
IllegalBlockSizeException, BadPaddingException,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.aofeng.demo.encrypt;
2+
3+
import static cn.aofeng.demo.util.LogUtil.log;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import java.security.InvalidAlgorithmParameterException;
7+
import java.security.InvalidKeyException;
8+
import java.security.NoSuchAlgorithmException;
9+
10+
import javax.crypto.BadPaddingException;
11+
import javax.crypto.IllegalBlockSizeException;
12+
import javax.crypto.Mac;
13+
import javax.crypto.NoSuchPaddingException;
14+
import javax.crypto.SecretKey;
15+
16+
import org.apache.commons.codec.binary.Base64;
17+
18+
/**
19+
* HMAC-SHA1签名算法。
20+
*
21+
* @author <a href="mailto:[email protected]">聂勇</a>
22+
*/
23+
public class HmacSha1 {
24+
25+
public final String encryptType = "HmacSHA1";
26+
public final String key = "abcdefgh_1234567";
27+
28+
public void execute(String data) throws UnsupportedEncodingException,
29+
NoSuchAlgorithmException, InvalidKeyException {
30+
EncryptAndDecrypt ead = new EncryptAndDecrypt();
31+
32+
byte[] srcData = data.getBytes(EncryptAndDecrypt.CHARSET);
33+
SecretKey secretKey = ead.createSecretKey(encryptType, key); // 生成密钥对象
34+
Mac mac = Mac.getInstance(encryptType);
35+
mac.init(secretKey);
36+
byte[] result = mac.doFinal(srcData);
37+
38+
log("使用%s签名后的数据:", encryptType);
39+
log(Base64.encodeBase64String(result));
40+
}
41+
42+
public static void main(String[] args) throws InvalidKeyException,
43+
UnsupportedEncodingException, IllegalBlockSizeException,
44+
BadPaddingException, NoSuchAlgorithmException,
45+
NoSuchPaddingException, InvalidAlgorithmParameterException {
46+
String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
47+
log("待签名的数据:\n%s", data);
48+
49+
HmacSha1 hs = new HmacSha1();
50+
hs.execute(data);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)