From b6d874ecbed35504fe6dfda3ccb552abfa34da35 Mon Sep 17 00:00:00 2001
From: Hinsteny
Date: Wed, 5 Jun 2019 13:01:01 +0800
Subject: [PATCH] #2 Add ECDSA sign util!
---
README.md | 29 +-
commons-core/pom.xml | 3 +-
.../core/functional/secret/AESUtil.java | 46 ++-
.../core/functional/secret/Base64Util.java | 72 ++---
.../core/functional/secret/DESUtil.java | 59 ++--
.../core/functional/secret/DSASignUtil.java | 56 ++++
.../core/functional/secret/ECDSASignUtil.java | 265 ++++++++++++++++++
.../core/functional/secret/MD5Util.java | 22 +-
.../core/functional/secret/RSASignUtil.java | 78 ++++--
.../core/functional/secret/RSAUtil.java | 77 +++--
.../core/functional/secret/SHAUtil.java | 23 +-
commons-core/src/main/java/module-info.java | 3 +-
.../core/functional/secret/AESUtilTest.java | 5 +-
.../functional/secret/DSASignUtilTest.java | 7 +-
.../functional/secret/ECDSASignUtilTest.java | 42 +++
.../functional/secret/RSASignUtilTest.java | 38 +++
commons-warp/pom.xml | 3 +-
commons-warp/src/main/java/module-info.java | 2 +-
pom.xml | 2 +-
19 files changed, 687 insertions(+), 145 deletions(-)
create mode 100644 commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/ECDSASignUtil.java
create mode 100644 commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/ECDSASignUtilTest.java
create mode 100644 commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/RSASignUtilTest.java
diff --git a/README.md b/README.md
index d6065a9..8ed43bc 100644
--- a/README.md
+++ b/README.md
@@ -7,16 +7,39 @@ oracle-jdk12
## 模块划分
- * commons-parent: 父模块, 进行项目说明及相关依赖声明
- * commons-core: 核心模块, 定义最基础的Java相关操作工具集合
- * commons-warp: 对一些常用的Java生态中的工具进行封装集成, 比如okhhtp, httpclient, javax.mail, poi等
+* commons-parent: 父模块, 进行项目说明及相关依赖声明
+* commons-core: 核心模块, 定义最基础的Java相关操作工具集合
+* commons-warp: 对一些常用的Java生态中的工具进行封装集成, 比如okhhtp, httpclient, javax.mail, poi等
## 发布
1. 发布到sonatype, 供大家引入使用
+
```
mvn clean deploy -Dmaven.skip.test=true -X
```
## 功能描述
+* 常用签名及加解密工具类: Base64, MD5, DES, 3DES, DSA, ECDSA, RSA, SHARS;
+
+
+## 引入使用
+
+1. 在项目pom.xml文件中加入依赖
+
+```
+
+ com.github.hinsteny
+ commons-core
+ 0.0.2
+
+```
+
+2. 在项目的module-info.java文件中添加模块依赖
+
+```
+ requires com.github.hinsteny.commons.core;
+```
+
+3. 使用模块中的工具类
\ No newline at end of file
diff --git a/commons-core/pom.xml b/commons-core/pom.xml
index d4f98db..fb0a9ba 100644
--- a/commons-core/pom.xml
+++ b/commons-core/pom.xml
@@ -5,7 +5,7 @@
com.github.hinsteny
commons-parent
- 0.0.1
+ 0.0.2
4.0.0
@@ -87,7 +87,6 @@
- true
${project.build.sourceEncoding}
${project.build.sourceEncoding}
${project.build.sourceEncoding}
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/AESUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/AESUtil.java
index 45b82df..2601b8c 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/AESUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/AESUtil.java
@@ -45,7 +45,8 @@ public class AESUtil {
/**
* 生成一个默认长度为128的AES key
- * @return
+ * @return 秘钥
+ * @throws NoSuchAlgorithmException 异常
*/
public static String generateAESKey() throws NoSuchAlgorithmException {
return generateAESKey(DEFAULT_KEY_LENGTH);
@@ -54,7 +55,8 @@ public static String generateAESKey() throws NoSuchAlgorithmException {
/**
* 指定秘钥长度, 生成AES key
* @param keyLen AES秘钥长度可选值有[128, 192, 256]
- * @return
+ * @return 秘钥
+ * @throws NoSuchAlgorithmException 异常
*/
public static String generateAESKey(int keyLen) throws NoSuchAlgorithmException {
byte[] keyByte = generateAESKeyByte(keyLen);
@@ -64,7 +66,8 @@ public static String generateAESKey(int keyLen) throws NoSuchAlgorithmException
/**
*
* @param keyLen AES秘钥长度可选值有
- * @return
+ * @return 秘钥
+ * @throws NoSuchAlgorithmException 异常
*/
public static byte[] generateAESKeyByte(int keyLen) throws NoSuchAlgorithmException {
AssertUtil.assertTrue(128 == keyLen || 192 == keyLen || 256 == keyLen, "AES key length is not correct");
@@ -86,7 +89,8 @@ public static byte[] generateAESKeyByte(int keyLen) throws NoSuchAlgorithmExcept
*
* @param content 需要加密的内容
* @param key 加密秘钥
- * @return
+ * @return 加密后的内容
+ * @throws Exception 异常
*/
public static String encrypt(String content, String key) throws Exception {
return encryptThenBase64(content, key, CHARCODE, DEFAULT_ALGORITHM);
@@ -98,7 +102,9 @@ public static String encrypt(String content, String key) throws Exception {
* @param content 需要加密的内容
* @param key 加密秘钥
* @param charset 编码
- * @return
+ * @param algorithm 加密算法
+ * @return 加密后的内容
+ * @throws Exception 异常
*/
public static String encryptThenBase64(String content, String key, String charset, AlgorithmType algorithm) throws Exception {
byte[] contentBytes = content.getBytes(charset);
@@ -113,7 +119,9 @@ public static String encryptThenBase64(String content, String key, String charse
* @param content 需要加密的内容
* @param key 加密秘钥
* @param charset 编码
- * @return
+ * @param algorithm 加密算法
+ * @return 加密后的内容
+ * @throws Exception 异常
*/
public static String encryptThenHex(String content, String key, String charset, AlgorithmType algorithm) throws Exception {
byte[] contentBytes = content.getBytes(charset);
@@ -127,7 +135,9 @@ public static String encryptThenHex(String content, String key, String charset,
*
* @param content 需要加密的内容
* @param key 加密秘钥
- * @return
+ * @param algorithm 加密算法
+ * @return 加密后的内容
+ * @throws Exception 异常
*/
public static byte[] encryptBytes(byte[] content, byte[] key, AlgorithmType algorithm) throws Exception {
AssertUtil.assertTrue(null != content && content.length != 0, "加密内容不能为空");
@@ -150,7 +160,8 @@ public static byte[] encryptBytes(byte[] content, byte[] key, AlgorithmType algo
*
* @param content 密文
* @param key 密钥
- * @return
+ * @return 解密后的内容
+ * @throws Exception 异常
*/
public static String decrypt(String content, String key) throws Exception {
return decryptAfterBase64Decode(content, key, CHARCODE, DEFAULT_ALGORITHM);
@@ -161,7 +172,10 @@ public static String decrypt(String content, String key) throws Exception {
*
* @param content 密文
* @param key 密钥
- * @return
+ * @param charset 编码
+ * @param algorithm 加密算法
+ * @return 解密后的内容
+ * @throws Exception 异常
*/
public static String decryptAfterBase64Decode(String content, String key, String charset, AlgorithmType algorithm) throws Exception {
AssertUtil.assertTrue(null != content && content.length() != 0, "解密内容不能为空");
@@ -177,7 +191,10 @@ public static String decryptAfterBase64Decode(String content, String key, String
*
* @param content 密文
* @param key 密钥
- * @return
+ * @param charset 编码
+ * @param algorithm 加密算法
+ * @return 解密后的内容
+ * @throws Exception 异常
*/
public static String decryptAfterHexDecode(String content, String key, String charset, AlgorithmType algorithm) throws Exception {
AssertUtil.assertTrue(null != content && content.length() != 0, "解密内容不能为空");
@@ -193,7 +210,9 @@ public static String decryptAfterHexDecode(String content, String key, String ch
*
* @param content 密文
* @param key 密钥
- * @return
+ * @param algorithm 加密算法
+ * @return 解密后的内容
+ * @throws Exception 异常
*/
public static byte[] decryptBytes(byte[] content, byte[] key, AlgorithmType algorithm) throws Exception {
AssertUtil.assertTrue(null != content && content.length != 0, "解密内容不能为空");
@@ -211,6 +230,11 @@ public static byte[] decryptBytes(byte[] content, byte[] key, AlgorithmType algo
return original;
}
+ /**
+ * 判断所使用的加密算法是否需要向量
+ * @param algorithm 算法
+ * @return result
+ */
private static boolean hasIv(AlgorithmType algorithm) {
return AlgorithmType.AES_CBC_NOPADDING == algorithm || AlgorithmType.AES_CBC_PKCS5Padding == algorithm;
}
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/Base64Util.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/Base64Util.java
index ccdd751..08c26f2 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/Base64Util.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/Base64Util.java
@@ -17,10 +17,10 @@ public class Base64Util {
private static String default_charset = "8859_1";
/**
- * 进行base64加码
- * @param data
- * @return
- * @throws UnsupportedEncodingException
+ * 进行base64编码
+ * @param data 被编码内容
+ * @return 返回编码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Encodes(String data) throws UnsupportedEncodingException {
String charset = default_charset;
@@ -30,11 +30,11 @@ public static String base64Encodes(String data) throws UnsupportedEncodingExcept
}
/**
- * 进行base64加码
- * @param data
- * @param charset
- * @return
- * @throws UnsupportedEncodingException
+ * 进行base64编码
+ * @param data 被加密内容
+ * @param charset 字符串编码
+ * @return 返回编码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Encodes(String data, String charset) throws UnsupportedEncodingException {
byte[] srcs = data.getBytes(charset);
@@ -43,10 +43,9 @@ public static String base64Encodes(String data, String charset) throws Unsupport
}
/**
- * 进行base64加码
- * @param data
- * @return
- * @throws UnsupportedEncodingException
+ * 进行base64编码
+ * @param data 被编码内容
+ * @return 返回编码结果
*/
public static String base64Encodes(byte[] data) {
byte[] dst = base64Encode(data);
@@ -54,10 +53,11 @@ public static String base64Encodes(byte[] data) {
}
/**
- * 进行base64加码
- * @param data
- * @return
- * @throws UnsupportedEncodingException
+ * 进行base64编码
+ * @param data 被编码内容
+ * @param charset 字符串编码
+ * @return 返回编码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Encodes(byte[] data, String charset) throws UnsupportedEncodingException {
byte[] dst = base64Encode(data);
@@ -66,9 +66,9 @@ public static String base64Encodes(byte[] data, String charset) throws Unsupport
/**
* 进行base64解码
- * @param data
- * @return
- * @throws UnsupportedEncodingException
+ * @param data 被解码内容
+ * @return 返回解码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Decodes(String data) throws UnsupportedEncodingException {
String charset = default_charset;
@@ -79,10 +79,10 @@ public static String base64Decodes(String data) throws UnsupportedEncodingExcept
/**
* 进行base64解码
- * @param data
- * @param charset
- * @return
- * @throws UnsupportedEncodingException
+ * @param data 被解码内容
+ * @param charset 字符编码
+ * @return 返回解码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Decode(String data, String charset) throws UnsupportedEncodingException {
byte[] srcs = data.getBytes(charset);
@@ -92,9 +92,9 @@ public static String base64Decode(String data, String charset) throws Unsupporte
/**
* 进行base64解码
- * @param data
- * @return
- * @throws UnsupportedEncodingException
+ * @param data 被解码内容
+ * @return 返回解码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Decodes(byte[] data) throws UnsupportedEncodingException {
byte[] dst = base64Decode(data);
@@ -103,10 +103,10 @@ public static String base64Decodes(byte[] data) throws UnsupportedEncodingExcept
/**
* 进行base64解码
- * @param data
- * @param charset
- * @return
- * @throws UnsupportedEncodingException
+ * @param data 被解码内容
+ * @param charset 字符编码
+ * @return 返回解码结果
+ * @throws UnsupportedEncodingException 不支持字符编码异常
*/
public static String base64Decodes(byte[] data, String charset) throws UnsupportedEncodingException {
byte[] dst = base64Decode(data);
@@ -114,9 +114,9 @@ public static String base64Decodes(byte[] data, String charset) throws Unsupport
}
/**
- * 对字节数组进行base64加码
- * @param data
- * @return
+ * 对字节数组进行base64编码
+ * @param data 被编码内容
+ * @return 返回编码结果
*/
public static byte[] base64Encode(byte[] data) {
byte[] dst = Base64.getEncoder().encode(data);
@@ -125,8 +125,8 @@ public static byte[] base64Encode(byte[] data) {
/**
* 对字节数组进行base64解码
- * @param data
- * @return
+ * @param data 被解码内容
+ * @return 返回解码结果
*/
public static byte[] base64Decode(byte[] data) {
byte[] dst = Base64.getDecoder().decode(data);
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DESUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DESUtil.java
index 57139dd..4bb4ebb 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DESUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DESUtil.java
@@ -11,10 +11,7 @@
import javax.crypto.spec.SecretKeySpec;
/**
- * DES加解密算法工具
- * 加密用的Key 可以用若干个字母和数字组成,最好不要用保留字符
- * DES: DES-56-CBC加密模式,key需要为8位。
- * 3DES: DESede-168-CBC加密模式,key需要为24位。
+ * DES加解密算法工具 加密用的Key 可以用若干个字母和数字组成,最好不要用保留字符 DES: DES-56-CBC加密模式,key需要为8位。 3DES: DESede-168-CBC加密模式,key需要为24位。
*
* @author Hinsteny
* @version DESUtil: DESUtil 2019-05-10 10:13 All rights reserved.$
@@ -67,26 +64,31 @@ public class DESUtil {
}
/**
- * 生成一个AES加密私钥串
+ * 生成一个DES加密私钥串
*
- * @return
+ * @return 秘钥ken
+ * @throws Exception 异常
*/
public static String generateDESKey() throws Exception {
return generateDESKey(algorithms[0], DEFAULT_DES_KEY_LENGTH);
}
/**
- * 生成一个AES加密私钥串
+ * 生成一个DES加密私钥串
*
- * @return
+ * @return 秘钥ken
+ * @throws Exception 异常
*/
public static String generate3DESKey() throws Exception {
return generateDESKey(algorithms[1], DEFAULT_3DES_KEY_LENGTH);
}
/**
+ * 生成一个DES加密私钥串
+ *
* @param keyLen AES秘钥长度可选值有
- * @return
+ * @return 秘钥ken
+ * @throws Exception 异常
*/
private static String generateDESKey(String algorithm, int keyLen) throws Exception {
if (!(DEFAULT_DES_KEY_LENGTH == keyLen || DEFAULT_3DES_KEY_LENGTH == keyLen)) {
@@ -107,8 +109,9 @@ private static String generateDESKey(String algorithm, int keyLen) throws Except
/**
* 判断key长度有效性
*
- * @param needKeyLen
- * @return
+ * @param key 秘钥
+ * @param needKeyLen 秘钥长度
+ * @return result
*/
private static boolean judgeKey(String key, int needKeyLen) {
if (key == null || "".equals(key.trim())) {
@@ -123,8 +126,8 @@ private static boolean judgeKey(String key, int needKeyLen) {
/**
* byte数组转化为16进制字符串
*
- * @param bytes
- * @return
+ * @param bytes 字节
+ * @return 转化后的内容
*/
public static String byteToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
@@ -141,7 +144,8 @@ public static String byteToHexString(byte[] bytes) {
*
* @param src 需要加密的内容
* @param key 加密秘钥
- * @return
+ * @return 加密结果
+ * @throws Exception 异常
*/
public static String encrypt(String src, String key) throws Exception {
judgeKey(key, 8);
@@ -153,7 +157,8 @@ public static String encrypt(String src, String key) throws Exception {
*
* @param src 需要加密的内容
* @param key 加密秘钥
- * @return
+ * @return 加密结果
+ * @throws Exception 异常
*/
public static String encrypt3DES(String src, String key) throws Exception {
judgeKey(key, 24);
@@ -165,7 +170,8 @@ public static String encrypt3DES(String src, String key) throws Exception {
*
* @param src 密文
* @param key 密钥
- * @return
+ * @return 解密结果
+ * @throws Exception 异常
*/
public static String decrypt(String src, String key) throws Exception {
judgeKey(key, 8);
@@ -177,7 +183,8 @@ public static String decrypt(String src, String key) throws Exception {
*
* @param src 密文
* @param key 密钥
- * @return
+ * @return 解密结果
+ * @throws Exception 异常
*/
public static String decrypt3DES(String src, String key) throws Exception {
judgeKey(key, 24);
@@ -187,11 +194,12 @@ public static String decrypt3DES(String src, String key) throws Exception {
/**
* 加密
*
- * @param src 需要加密的内容
- * @param key 加密秘钥
+ * @param src 需要加密的内容
+ * @param key 加密秘钥
* @param algorithm 加密所用算法
- * @param charset 编码
- * @return
+ * @param charset 编码
+ * @return 加密结果
+ * @throws Exception 异常
*/
private static String encrypt(String src, String key, String algorithm, String charset) throws Exception {
String afterCode;
@@ -209,9 +217,12 @@ private static String encrypt(String src, String key, String algorithm, String c
/**
* 解密
*
- * @param src 密文
- * @param key 密钥
- * @return
+ * @param src 需要解密的内容
+ * @param key 解密秘钥
+ * @param algorithm 解密所用算法
+ * @param charset 编码
+ * @return 解密结果
+ * @throws Exception 异常
*/
private static String decrypt(String src, String key, String algorithm, String charset) throws Exception {
String originalString;
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DSASignUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DSASignUtil.java
index 3ee8384..8a81254 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DSASignUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/DSASignUtil.java
@@ -18,6 +18,8 @@
* DSA签名工具类
*
* 签名算法中公私钥及数据都是以字节的方式运作, 因此如果我们需要将公私钥导出时, 或者将签名完的数据存储完字符串, 都需要做防乱码编码, 这里默认使用BASE64
+ *
+ * 算法: 基于模幂运算和离散对数
*
* @author Hinsteny
* @version DSASignUtil: DSASignUtil 2019-06-04 10:14 All rights reserved.$
@@ -34,6 +36,9 @@ public class DSASignUtil {
*/
public enum Algorithm {
+ //限制被签名数据长度必须为20bytes, 常用于rawdata签名, 比如文件
+ NONEwithDSA("NONEwithDSA"),
+ // 不限制被签名数据长度
SHA1withDSA("SHA1withDSA"),
;
@@ -54,6 +59,7 @@ public String getAlgorithm() {
*
*
* @return KeyPair
+ * @throws Exception 异常
*/
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
@@ -95,11 +101,22 @@ public static String getPrivateKey(KeyPair keyPair) {
* @param privateKey 私钥(BASE64编码)
* @param charset 编码格式
* @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
public static String sign(Map param, String privateKey, String charset) throws Exception {
return sign(Algorithm.SHA1withDSA, buildParam(param), privateKey, charset);
}
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param param 需要签名的数据
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
public static String sign(Algorithm algorithm, Map param, String privateKey, String charset) throws Exception {
return sign(algorithm, buildParam(param), privateKey, charset);
}
@@ -111,17 +128,35 @@ public static String sign(Algorithm algorithm, Map param, String
* @param privateKey 私钥(BASE64编码)
* @param charset 编码格式
* @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
public static String sign(String text, String privateKey, String charset) throws Exception {
return doSign(Algorithm.SHA1withDSA, text, privateKey, charset);
}
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
public static String sign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
return doSign(algorithm, text, privateKey, charset);
}
/**
* 进行RSA签名
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
private static String doSign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
@@ -145,17 +180,37 @@ private static String doSign(Algorithm algorithm, String text, String privateKey
* @param publicKey 公钥(BASE64编码)
* @param charset 编码格式
* @return 验签结果
+ * @throws Exception 异常
*/
public static boolean verify(String text, String sign, String publicKey, String charset) throws Exception {
return verify(Algorithm.SHA1withDSA, text, sign, publicKey, charset);
}
+ /**
+ * 验签
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
+ */
public static boolean verify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
return doVerify(algorithm, text, sign, publicKey, charset);
}
/**
* 进行RSA验签
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
*/
private static boolean doVerify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(publicKey);
@@ -173,6 +228,7 @@ private static boolean doVerify(Algorithm algorithm, String text, String sign, S
* @param content origin content
* @param charset get byte encoding
* @return content bytes
+ * @throws UnsupportedEncodingException 异常
*/
private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException {
if (null == charset || "".equals(charset.trim())) {
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/ECDSASignUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/ECDSASignUtil.java
new file mode 100644
index 0000000..80bca55
--- /dev/null
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/ECDSASignUtil.java
@@ -0,0 +1,265 @@
+package com.github.hinsteny.commons.core.functional.secret;
+
+import java.io.UnsupportedEncodingException;
+import java.security.Key;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.Signature;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Base64;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * ECDSA签名工具类
+ *
+ * 签名算法中公私钥及数据都是以字节的方式运作, 因此如果我们需要将公私钥导出时, 或者将签名完的数据存储完字符串, 都需要做防乱码编码, 这里默认使用BASE64
+ *
+ * 算法: 作为DSA的变体,基于椭圆曲线
+ *
+ * @author Hinsteny
+ * @version ECDSASignUtil: ECDSASignUtil 2019-06-04 21:23 All rights reserved.$
+ */
+public class ECDSASignUtil {
+
+ /**
+ * 签名算法ECDSA
+ */
+ private static final String KEY_ALGORITHM = "EC";
+
+ /**
+ * 秘钥锁支持的长度
+ */
+ private static final int[] KEY_SIZE = {571};
+
+ /**
+ * 签名算法
+ */
+ public enum Algorithm {
+
+ NONEwithECDSA("NONEwithECDSA"),
+ SHA1withECDSA("SHA1withECDSA"),
+ SHA256withECDSA("SHA256withECDSA"),
+ SHA384withECDSA("SHA384withECDSA"),
+ SHA512withECDSA("SHA512withECDSA"),
+ ;
+
+ private String algorithm;
+
+ Algorithm(String algorithm) {
+ this.algorithm = algorithm;
+ }
+
+ public String getAlgorithm() {
+ return algorithm;
+ }
+ }
+
+ /**
+ *
+ * 生成密钥对(公钥和私钥)
+ *
+ *
+ * @return KeyPair
+ * @throws Exception 异常
+ */
+ public static KeyPair generateKeyPair() throws Exception {
+ KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
+ keyPairGen.initialize(KEY_SIZE[0]);
+ KeyPair keyPair = keyPairGen.generateKeyPair();
+ return keyPair;
+ }
+
+ /**
+ *
+ * 获取公钥
+ *
+ *
+ * @param keyPair 密钥对
+ * @return public key
+ */
+ public static String getPublicKey(KeyPair keyPair) {
+ Key key = keyPair.getPublic();
+ return Base64.getEncoder().encodeToString(key.getEncoded());
+ }
+
+ /**
+ *
+ * 获取私钥
+ *
+ *
+ * @param keyPair 密钥对
+ * @return private key
+ */
+ public static String getPrivateKey(KeyPair keyPair) {
+ Key key = keyPair.getPrivate();
+ return Base64.getEncoder().encodeToString(key.getEncoded());
+ }
+
+ /**
+ * 签名字符串
+ *
+ * @param param 需要签名的数据
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
+ public static String sign(Map param, String privateKey, String charset) throws Exception {
+ return sign(Algorithm.SHA256withECDSA, buildParam(param), privateKey, charset);
+ }
+
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param param 需要签名的数据
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
+ public static String sign(Algorithm algorithm, Map param, String privateKey, String charset) throws Exception {
+ return sign(algorithm, buildParam(param), privateKey, charset);
+ }
+
+ /**
+ * 签名字符串
+ *
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
+ public static String sign(String text, String privateKey, String charset) throws Exception {
+ return doSign(Algorithm.SHA256withECDSA, text, privateKey, charset);
+ }
+
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
+ public static String sign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
+ return doSign(algorithm, text, privateKey, charset);
+ }
+
+ /**
+ * 进行RSA签名
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
+ private static String doSign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
+ byte[] keyBytes = Base64.getDecoder().decode(privateKey);
+ PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
+
+ KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
+ PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
+ Signature signature = Signature.getInstance(algorithm.getAlgorithm());
+ signature.initSign(privateK);
+ signature.update(getContentBytes(text, charset));
+ byte[] result = signature.sign();
+
+ return Base64.getEncoder().encodeToString(result);
+ }
+
+ /**
+ * 验签
+ *
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
+ */
+ public static boolean verify(String text, String sign, String publicKey, String charset) throws Exception {
+ return verify(Algorithm.SHA256withECDSA, text, sign, publicKey, charset);
+ }
+
+ /**
+ * 验签
+ *
+ * @param algorithm 验签算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
+ */
+ public static boolean verify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
+ return doVerify(algorithm, text, sign, publicKey, charset);
+ }
+
+ /**
+ * 进行RSA验签
+ *
+ * @param algorithm 验签算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
+ */
+ private static boolean doVerify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
+ byte[] keyBytes = Base64.getDecoder().decode(publicKey);
+ X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
+ KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
+ PublicKey publicK = keyFactory.generatePublic(keySpec);
+
+ Signature signature = Signature.getInstance(algorithm.getAlgorithm());
+ signature.initVerify(publicK);
+ signature.update(getContentBytes(text, charset));
+ return signature.verify(Base64.getDecoder().decode(sign));
+ }
+
+ /**
+ * @param content origin content
+ * @param charset get byte encoding
+ * @return content bytes
+ * @throws UnsupportedEncodingException 异常
+ */
+ private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException {
+ if (null == charset || "".equals(charset.trim())) {
+ return content.getBytes();
+ }
+ return content.getBytes(charset);
+ }
+
+ /**
+ * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
+ *
+ * @param params 需要排序并参与字符拼接的参数组
+ * @return 拼接后字符串
+ */
+ private static String buildParam(Map params) {
+ StringBuilder sbr = new StringBuilder();
+ if (null != params && params.size() > 0) {
+ Map sortMap = new TreeMap<>(String::compareTo);
+ sortMap.putAll(params);
+ final String KVItem = "%s=%s&";
+ sortMap.forEach((key, value) -> sbr.append(String.format(KVItem, key, value)));
+ sbr.setLength(sbr.length() - 1);
+ }
+
+ return sbr.toString();
+ }
+
+}
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/MD5Util.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/MD5Util.java
index ee9ef57..638bd04 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/MD5Util.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/MD5Util.java
@@ -20,8 +20,10 @@ public class MD5Util {
/**
* 对文本进行32位小写MD5加密
*
- * @param text
- * @return 加密后的内容
+ * @param text 签名内容
+ * @return 签名后的内容
+ * @throws NoSuchAlgorithmException 异常
+ * @throws UnsupportedEncodingException 异常
*/
public static String sign(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return sign(text, DEFAULT_CHART_SET);
@@ -30,8 +32,11 @@ public static String sign(String text) throws NoSuchAlgorithmException, Unsuppor
/**
* 对文本进行32位小写MD5加密
*
- * @param text
+ * @param text 签名内容
+ * @param charset 字符编码
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
+ * @throws UnsupportedEncodingException 异常
*/
public static String sign(String text, String charset) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md5Encoder = MessageDigest.getInstance(MD5);
@@ -50,13 +55,12 @@ public static String sign(String text, String charset) throws NoSuchAlgorithmExc
}
/**
- * @param content
- * @param charset
- * @return
- * @throws SignatureException
- * @throws UnsupportedEncodingException
+ * @param content 签名内容
+ * @param charset 字符编码
+ * @return 签名结果
+ * @throws UnsupportedEncodingException 异常
*/
- private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException{
+ private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSASignUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSASignUtil.java
index ebf7b6b..ef69775 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSASignUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSASignUtil.java
@@ -53,6 +53,7 @@ public String getAlgorithm() {
*
*
* @return KeyPair
+ * @throws Exception 异常
*/
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
@@ -90,15 +91,26 @@ public static String getPrivateKey(KeyPair keyPair) {
/**
* 签名字符串
*
- * @param param 需要签名的数据
+ * @param param 需要签名的数据
* @param privateKey 私钥(BASE64编码)
- * @param charset 编码格式
+ * @param charset 编码格式
* @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
public static String sign(Map param, String privateKey, String charset) throws Exception {
return sign(Algorithm.SHA1withRSA, buildParam(param), privateKey, charset);
}
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param param 需要签名的数据
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
public static String sign(Algorithm algorithm, Map param, String privateKey, String charset) throws Exception {
return sign(algorithm, buildParam(param), privateKey, charset);
}
@@ -106,27 +118,39 @@ public static String sign(Algorithm algorithm, Map param, String
/**
* 签名字符串
*
- * @param text 需要签名的字符串
+ * @param text 需要签名的字符串
* @param privateKey 私钥(BASE64编码)
- * @param charset 编码格式
+ * @param charset 编码格式
* @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
public static String sign(String text, String privateKey, String charset) throws Exception {
return doSign(Algorithm.SHA1withRSA, text, privateKey, charset);
}
+ /**
+ * 签名字符串
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
+ */
public static String sign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
return doSign(algorithm, text, privateKey, charset);
}
/**
* 进行RSA签名
- * @param algorithm
- * @param text
- * @param privateKey
- * @param charset
- * @return
- * @throws Exception
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param privateKey 私钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 签名结果(BASE64编码)
+ * @throws Exception 异常
*/
private static String doSign(Algorithm algorithm, String text, String privateKey, String charset) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
@@ -145,28 +169,42 @@ private static String doSign(Algorithm algorithm, String text, String privateKey
/**
* 验签
*
- * @param text 需要签名的字符串
- * @param sign 客户签名结果
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
* @param publicKey 公钥(BASE64编码)
- * @param charset 编码格式
+ * @param charset 编码格式
* @return 验签结果
+ * @throws Exception 异常
*/
public static boolean verify(String text, String sign, String publicKey, String charset) throws Exception {
return verify(Algorithm.SHA1withRSA, text, sign, publicKey, charset);
}
+ /**
+ * 验签
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
+ */
public static boolean verify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
return doVerify(algorithm, text, sign, publicKey, charset);
}
/**
* 进行RSA验签
- * @param text
- * @param sign
- * @param publicKey
- * @param charset
- * @return
- * @throws Exception
+ *
+ * @param algorithm 签名算法
+ * @param text 需要签名的字符串
+ * @param sign 客户签名结果
+ * @param publicKey 公钥(BASE64编码)
+ * @param charset 编码格式
+ * @return 验签结果
+ * @throws Exception 异常
*/
private static boolean doVerify(Algorithm algorithm, String text, String sign, String publicKey, String charset) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(publicKey);
@@ -184,6 +222,7 @@ private static boolean doVerify(Algorithm algorithm, String text, String sign, S
* @param content origin content
* @param charset get byte encoding
* @return content bytes
+ * @throws UnsupportedEncodingException 异常
*/
private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException {
if (null == charset || "".equals(charset.trim())) {
@@ -194,6 +233,7 @@ private static byte[] getContentBytes(String content, String charset) throws Uns
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
+ *
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSAUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSAUtil.java
index a9c9607..c6aeb90 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSAUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/RSAUtil.java
@@ -40,6 +40,7 @@ public class RSAUtil {
*
*
* @return KeyPair
+ * @throws Exception 异常
*/
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
@@ -77,54 +78,88 @@ public static String getPrivateKey(KeyPair keyPair) {
/**
* 使用给定key, 对字符串数据按照指定编码, 进行加密
- * @param key
- * @param data
- * @param charset
- * @return
- * @throws Exception
+ * @param key 加密私钥
+ * @param data 被加密内容
+ * @param charset 字符编码
+ * @return 加密后内容
+ * @throws Exception 异常
*/
public static String encryptData(String key, String data, String charset) throws Exception {
return encryptData(key, data, charset, true);
}
+ /**
+ * 使用给定key, 对字符串数据按照指定编码, 进行加密
+ * @param key 加密私钥
+ * @param data 被加密内容
+ * @param charset 字符编码
+ * @param usePubKey 使用公钥
+ * @return 加密后内容
+ * @throws Exception 异常
+ */
public static String encryptData(String key, String data, String charset, boolean usePubKey) throws Exception {
byte[] dataBytes = data.getBytes(charset);
byte[] encryptData = encryptData(key, dataBytes, usePubKey);
return ByteUtil.byteToHex(encryptData);
}
+ /**
+ * 使用给定key, 对字符串数据按照指定编码, 进行加密
+ * @param key 加密私钥
+ * @param content 被加密内容
+ * @param usePubKey 使用公钥
+ * @return 加密后内容
+ * @throws Exception 异常
+ */
public static byte[] encryptData(String key, byte[] content, boolean usePubKey) throws Exception {
return encrypt(content, buildKey(key, usePubKey));
}
/**
* 使用给定key, 对字符串数据按照指定编码, 进行解密
- * @param key
- * @param data
- * @param charset
- * @return
- * @throws Exception
+ * @param key 解密私钥
+ * @param data 解密内容
+ * @param charset 字符编码
+ * @return 解密结果
+ * @throws Exception 异常
*/
public static String decryptData(String key, String data, String charset) throws Exception {
return decryptData(key, data, charset, false);
}
+ /**
+ * 使用给定key, 对字符串数据按照指定编码, 进行解密
+ * @param key 解密私钥
+ * @param data 解密内容
+ * @param charset 字符编码
+ * @param usePubKey 使用公钥
+ * @return 解密结果
+ * @throws Exception 异常
+ */
public static String decryptData(String key, String data, String charset, boolean usePubKey) throws Exception {
byte[] dataBytes = ByteUtil.hexTBytes(data.getBytes(charset));
byte[] encryptData = decryptData(key, dataBytes, usePubKey);
return new String(encryptData, charset);
}
+ /**
+ * 使用给定key, 对字符串数据按照指定编码, 进行解密
+ * @param key 解密私钥
+ * @param content 解密内容
+ * @param usePubKey 使用公钥
+ * @return 解密结果
+ * @throws Exception 异常
+ */
public static byte[] decryptData(String key, byte[] content, boolean usePubKey) throws Exception {
return decrypt(content, buildKey(key, usePubKey));
}
/**
* [公钥/私钥]加密
- * @param content
+ * @param content 加密内容
* @param key [PublicKey/PrivateKey]
- * @return
- * @throws Exception
+ * @return 加密结果
+ * @throws Exception 异常
*/
private static byte[] encrypt(byte[] content, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
@@ -134,10 +169,10 @@ private static byte[] encrypt(byte[] content, Key key) throws Exception {
/**
* [公钥/私钥]解密
- * @param content
+ * @param content 解密内容
* @param key [PublicKey/PrivateKey]
- * @return
- * @throws Exception
+ * @return 解密结果
+ * @throws Exception 异常
*/
public static byte[] decrypt(byte[] content, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
@@ -147,11 +182,11 @@ public static byte[] decrypt(byte[] content, Key key) throws Exception {
/**
* 根据原始密钥串构建key对象
- * @param key
- * @param isPubKey
- * @return
- * @throws NoSuchAlgorithmException
- * @throws InvalidKeySpecException
+ * @param key [PublicKey/PrivateKey]
+ * @param isPubKey 使用公钥
+ * @return key对象
+ * @throws NoSuchAlgorithmException 异常
+ * @throws InvalidKeySpecException 异常
*/
private static Key buildKey(String key, boolean isPubKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyByte = Base64.getDecoder().decode(key);
diff --git a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/SHAUtil.java b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/SHAUtil.java
index e430dde..b8c8482 100644
--- a/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/SHAUtil.java
+++ b/commons-core/src/main/java/com/github/hinsteny/commons/core/functional/secret/SHAUtil.java
@@ -37,8 +37,9 @@ public String getAlgorithm() {
/**
* 对文本进行512分组计算最终生成40位的消息摘要
*
- * @param content
+ * @param content 被散列内容
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
public static String calculateSha1Val(String content) throws NoSuchAlgorithmException {
return calculateSignVal(SignType.SHA1, content);
@@ -47,8 +48,9 @@ public static String calculateSha1Val(String content) throws NoSuchAlgorithmExce
/**
* 对文本进行512分组计算最终生成56位的消息摘要
*
- * @param content
+ * @param content 被散列内容
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
public static String calculateSha224Val(String content) throws NoSuchAlgorithmException {
return calculateSignVal(SignType.SHA224, content);
@@ -57,8 +59,9 @@ public static String calculateSha224Val(String content) throws NoSuchAlgorithmEx
/**
* 对文本进行512分组计算最终生成64位的消息摘要
*
- * @param content
+ * @param content 被散列内容
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
public static String calculateSha256Val(String content) throws NoSuchAlgorithmException {
return calculateSignVal(SignType.SHA256, content);
@@ -67,8 +70,9 @@ public static String calculateSha256Val(String content) throws NoSuchAlgorithmEx
/**
* 对文本进行512分组计算最终生成96位的消息摘要
*
- * @param content
+ * @param content 被散列内容
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
public static String calculateSha384Val(String content) throws NoSuchAlgorithmException {
return calculateSignVal(SignType.SHA384, content);
@@ -77,8 +81,9 @@ public static String calculateSha384Val(String content) throws NoSuchAlgorithmEx
/**
* 对文本进行512分组计算最终生成128位的消息摘要
*
- * @param content
+ * @param content 被散列内容
* @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
public static String calculateSha512Val(String content) throws NoSuchAlgorithmException {
return calculateSignVal(SignType.SHA512, content);
@@ -86,10 +91,10 @@ public static String calculateSha512Val(String content) throws NoSuchAlgorithmEx
/**
* 根据指定的签名算法, 对输入内容进行签名
- * @param type
- * @param content
- * @return
- * @throws NoSuchAlgorithmException
+ * @param type 散列算法
+ * @param content 被散列内容
+ * @return 加密后的内容
+ * @throws NoSuchAlgorithmException 异常
*/
private static String calculateSignVal (SignType type, String content) throws NoSuchAlgorithmException {
MessageDigest sha1Encoder = MessageDigest.getInstance(type.getAlgorithm());
diff --git a/commons-core/src/main/java/module-info.java b/commons-core/src/main/java/module-info.java
index a74d6c8..3081dd2 100644
--- a/commons-core/src/main/java/module-info.java
+++ b/commons-core/src/main/java/module-info.java
@@ -3,13 +3,14 @@
* @version module-info: module-info 2019-05-09 16:05 All rights reserved.$
* @since 9
*/
-module org.github.hinsteny.commons.core {
+module com.github.hinsteny.commons.core {
requires java.desktop;
requires org.apache.logging.log4j;
exports com.github.hinsteny.commons.core.base;
exports com.github.hinsteny.commons.core.functional.page;
+ exports com.github.hinsteny.commons.core.functional.secret;
exports com.github.hinsteny.commons.core.io.csv;
exports com.github.hinsteny.commons.core.io.image;
exports com.github.hinsteny.commons.core.utils;
diff --git a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/AESUtilTest.java b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/AESUtilTest.java
index d33a3c1..2b3c248 100644
--- a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/AESUtilTest.java
+++ b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/AESUtilTest.java
@@ -14,9 +14,8 @@ public class AESUtilTest {
@Test(testName = "使用AES工具对字符串进行加解密示例")
public void testAesUtilEncryptAndDecryptToStr() throws Exception {
String data = "走遍世界的心不能停...O(∩_∩)O哈哈~", key, secret, origin;
- int keyLen = 128;
key = AESUtil.generateAESKey();
- System.out.println(String.format("key length %d: %s", keyLen, key));
+ System.out.println(String.format("key length %d: %s", key.length(), key));
System.out.println("============== 默认AES加解密前后使用Base64加解码 ==============");
secret = AESUtil.encrypt(data, key);
System.out.println(String.format("encrypt data: %s, result: %s", data, secret));
@@ -34,7 +33,7 @@ public void testAesUtilEncryptAndDecryptToByte() throws Exception {
String data = "走遍世界的心不能停...O(∩_∩)O哈哈~";
int keyLen = 128;
byte[] key = AESUtil.generateAESKeyByte(keyLen);
- System.out.println(String.format("key length %d: %s", keyLen, key));
+ System.out.println(String.format("key length %d: %s", key.length, key));
byte[] secret = AESUtil.encryptBytes(data.getBytes("UTF-8"), key, AlgorithmType.AES_ECB_NoPadding);
System.out.println(String.format("encrypt data: %s, result: ", data) + ByteUtil.byteToStr(secret));
byte[] origin = AESUtil.decryptBytes(secret, key, AlgorithmType.AES_ECB_NoPadding);
diff --git a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/DSASignUtilTest.java b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/DSASignUtilTest.java
index b2e259e..11e51b5 100644
--- a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/DSASignUtilTest.java
+++ b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/DSASignUtilTest.java
@@ -1,6 +1,7 @@
package com.github.hinsteny.test.commons.core.functional.secret;
import com.github.hinsteny.commons.core.functional.secret.DSASignUtil;
+import com.github.hinsteny.commons.core.functional.secret.DSASignUtil.Algorithm;
import java.security.KeyPair;
import org.testng.annotations.Test;
@@ -13,15 +14,15 @@ public class DSASignUtilTest {
private static final String DEFAULT_CHARSET = "UTF-8";
@Test(testName = "使用DSA工具对字符串进行签名验签示例")
- public void testAesUtilEncryptAndDecryptToStr() throws Exception {
+ public void testDSASignUtilSignAndVerifyForStr() throws Exception {
String data = "走遍世界的心不能停...O(∩_∩)O哈哈~", publicKey, privateKey, secret;
boolean result;
- int keyLen = 1024;
KeyPair keyPair = DSASignUtil.generateKeyPair();
publicKey = DSASignUtil.getPublicKey(keyPair);
privateKey = DSASignUtil.getPrivateKey(keyPair);
- System.out.println(String.format("key length %d: %s", keyLen, privateKey));
+ System.out.println(String.format("key length %d: %s", privateKey.length(), privateKey));
System.out.println("============== 默认DSA签名前后使用Base64加解码 ==============");
+ System.out.println("============== 使用默认签名算法 SHA1withDSA ==============");
secret = DSASignUtil.sign(data, privateKey, DEFAULT_CHARSET);
System.out.println(String.format("sign data: %s, result: %s", data, secret));
result = DSASignUtil.verify(data, secret, publicKey, DEFAULT_CHARSET);
diff --git a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/ECDSASignUtilTest.java b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/ECDSASignUtilTest.java
new file mode 100644
index 0000000..bf1b88d
--- /dev/null
+++ b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/ECDSASignUtilTest.java
@@ -0,0 +1,42 @@
+package com.github.hinsteny.test.commons.core.functional.secret;
+
+import com.github.hinsteny.commons.core.functional.secret.ECDSASignUtil;
+import com.github.hinsteny.commons.core.functional.secret.ECDSASignUtil.Algorithm;
+import java.security.KeyPair;
+import org.testng.annotations.Test;
+
+/**
+ * @author Hinsteny
+ * @version ECDSASignUtilTest: ECDSASignUtilTest 2019-06-05 08:21 All rights reserved.$
+ */
+public class ECDSASignUtilTest {
+
+ private static final String DEFAULT_CHARSET = "UTF-8";
+
+ @Test(testName = "使用ECDSA工具对字符串进行签名验签示例")
+ public void testECDSASignUtilSignAndVerifyForStr() throws Exception {
+ String data = "走遍世界的心不能停...O(∩_∩)O哈哈~", publicKey, privateKey, secret;
+ boolean result;
+ KeyPair keyPair = ECDSASignUtil.generateKeyPair();
+ publicKey = ECDSASignUtil.getPublicKey(keyPair);
+ privateKey = ECDSASignUtil.getPrivateKey(keyPair);
+ System.out.println(String.format("key length %d: %s", privateKey.length(), privateKey));
+ System.out.println("============== 默认ECDSA签名前后使用Base64加解码 ==============");
+ System.out.println("============== 使用默认签名算法 SHA256withECDSA ==============");
+ secret = ECDSASignUtil.sign(data, privateKey, DEFAULT_CHARSET);
+ System.out.println(String.format("sign data: %s, result: %s", data, secret));
+ result = ECDSASignUtil.verify(data, secret, publicKey, DEFAULT_CHARSET);
+ System.out.println("verify result: " + result);
+ System.out.println("============== 使用非默认签名算法 NONEwithECDSA ==============");
+ secret = ECDSASignUtil.sign(Algorithm.NONEwithECDSA, data, privateKey, DEFAULT_CHARSET);
+ System.out.println(String.format("sign data: %s, result: %s", data, secret));
+ result = ECDSASignUtil.verify(Algorithm.NONEwithECDSA, data, secret, publicKey, DEFAULT_CHARSET);
+ System.out.println("verify result: " + result);
+ System.out.println("============== 使用非默认签名算法 SHA1withECDSA ==============");
+ secret = ECDSASignUtil.sign(Algorithm.SHA1withECDSA, data, privateKey, DEFAULT_CHARSET);
+ System.out.println(String.format("sign data: %s, result: %s", data, secret));
+ result = ECDSASignUtil.verify(Algorithm.SHA1withECDSA, data, secret, publicKey, DEFAULT_CHARSET);
+ System.out.println("verify result: " + result);
+ }
+
+}
diff --git a/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/RSASignUtilTest.java b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/RSASignUtilTest.java
new file mode 100644
index 0000000..5dfae19
--- /dev/null
+++ b/commons-core/src/test/java/com/github/hinsteny/test/commons/core/functional/secret/RSASignUtilTest.java
@@ -0,0 +1,38 @@
+package com.github.hinsteny.test.commons.core.functional.secret;
+
+import com.github.hinsteny.commons.core.functional.secret.RSASignUtil;
+import com.github.hinsteny.commons.core.functional.secret.RSASignUtil.Algorithm;
+import java.security.KeyPair;
+import org.testng.annotations.Test;
+
+/**
+ * @author Hinsteny
+ * @version RSASignUtilTest: RSASignUtilTest 2019-06-05 08:42 All rights reserved.$
+ */
+public class RSASignUtilTest {
+
+ private static final String DEFAULT_CHARSET = "UTF-8";
+
+ @Test(testName = "使用DSA工具对字符串进行签名验签示例")
+ public void testRSASignUtilSignAndVerifyForStr() throws Exception {
+ String data = "走遍世界的心不能停...O(∩_∩)O哈哈~", publicKey, privateKey, secret;
+ boolean result;
+ int keyLen = 1024;
+ KeyPair keyPair = RSASignUtil.generateKeyPair();
+ publicKey = RSASignUtil.getPublicKey(keyPair);
+ privateKey = RSASignUtil.getPrivateKey(keyPair);
+ System.out.println(String.format("key length %d: %s", keyLen, privateKey));
+ System.out.println("============== 默认RSA签名前后使用Base64加解码 ==============");
+ System.out.println("============== 使用默认签名算法 SHA1withRSA ==============");
+ secret = RSASignUtil.sign(data, privateKey, DEFAULT_CHARSET);
+ System.out.println(String.format("sign data: %s, result: %s", data, secret));
+ result = RSASignUtil.verify(data, secret, publicKey, DEFAULT_CHARSET);
+ System.out.println("verify result: " + result);
+ System.out.println("============== 使用非默认签名算法 MD5withRSA ==============");
+ secret = RSASignUtil.sign(Algorithm.MD5withRSA, data, privateKey, DEFAULT_CHARSET);
+ System.out.println(String.format("sign data: %s, result: %s", data, secret));
+ result = RSASignUtil.verify(Algorithm.MD5withRSA, data, secret, publicKey, DEFAULT_CHARSET);
+ System.out.println("verify result: " + result);
+ }
+
+}
diff --git a/commons-warp/pom.xml b/commons-warp/pom.xml
index 48f7443..92428a7 100644
--- a/commons-warp/pom.xml
+++ b/commons-warp/pom.xml
@@ -5,7 +5,7 @@
com.github.hinsteny
commons-parent
- 0.0.1
+ 0.0.2
4.0.0
@@ -112,7 +112,6 @@
- true
${project.build.sourceEncoding}
${project.build.sourceEncoding}
${project.build.sourceEncoding}
diff --git a/commons-warp/src/main/java/module-info.java b/commons-warp/src/main/java/module-info.java
index 0bd02af..c350b4c 100644
--- a/commons-warp/src/main/java/module-info.java
+++ b/commons-warp/src/main/java/module-info.java
@@ -8,7 +8,7 @@
requires java.xml;
requires java.xml.bind;
- requires org.github.hinsteny.commons.core;
+ requires com.github.hinsteny.commons.core;
requires org.apache.logging.log4j;
diff --git a/pom.xml b/pom.xml
index e4a00bc..0e7feef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.github.hinsteny
commons-parent
- 0.0.1
+ 0.0.2
commons-core