Skip to content

Commit

Permalink
#2 Add ECDSA sign util!
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinsteny committed Jun 5, 2019
1 parent a833163 commit b6d874e
Show file tree
Hide file tree
Showing 19 changed files with 687 additions and 145 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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文件中加入依赖

```
<dependency>
<groupId>com.github.hinsteny</groupId>
<artifactId>commons-core</artifactId>
<version>0.0.2</version>
</dependency>
```

2. 在项目的module-info.java文件中添加模块依赖

```
requires com.github.hinsteny.commons.core;
```

3. 使用模块中的工具类
3 changes: 1 addition & 2 deletions commons-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.hinsteny</groupId>
<artifactId>commons-parent</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -87,7 +87,6 @@
</execution>
</executions>
<configuration>
<aggregate>true</aggregate>
<charset>${project.build.sourceEncoding}</charset>
<encoding>${project.build.sourceEncoding}</encoding>
<docencoding>${project.build.sourceEncoding}</docencoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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, "加密内容不能为空");
Expand All @@ -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);
Expand All @@ -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, "解密内容不能为空");
Expand All @@ -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, "解密内容不能为空");
Expand All @@ -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, "解密内容不能为空");
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -43,21 +43,21 @@ 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);
return new String(dst);
}

/**
* 进行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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -103,20 +103,20 @@ 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);
return new String(dst, charset);
}

/**
* 对字节数组进行base64加码
* @param data
* @return
* 对字节数组进行base64编码
* @param data 被编码内容
* @return 返回编码结果
*/
public static byte[] base64Encode(byte[] data) {
byte[] dst = Base64.getEncoder().encode(data);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit b6d874e

Please sign in to comment.