fix jwt bug

This commit is contained in:
Looly
2021-06-18 15:59:20 +08:00
parent 7ead906910
commit 86d711cc03
10 changed files with 167 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.crypto.digest;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
@@ -25,8 +26,8 @@ import java.security.MessageDigest;
* 一般的,消息鉴别码用于验证传输于两个共 同享有一个密钥的单位之间的消息。<br>
* HMAC 可以与任何迭代散列函数捆绑使用。MD5 和 SHA-1 就是这种散列函数。HMAC 还可以使用一个用于计算和确认消息鉴别值的密钥。<br>
* 注意:此对象实例化后为非线程安全!
* @author Looly
*
* @author Looly
*/
public class HMac implements Serializable {
private static final long serialVersionUID = 1L;
@@ -34,18 +35,21 @@ public class HMac implements Serializable {
private final MacEngine engine;
// ------------------------------------------------------------------------------------------- Constructor start
/**
* 构造,自动生成密钥
*
* @param algorithm 算法 {@link HmacAlgorithm}
*/
public HMac(HmacAlgorithm algorithm) {
this(algorithm, (Key)null);
this(algorithm, (Key) null);
}
/**
* 构造
*
* @param algorithm 算法 {@link HmacAlgorithm}
* @param key 密钥
* @param key 密钥
*/
public HMac(HmacAlgorithm algorithm, byte[] key) {
this(algorithm.getValue(), key);
@@ -53,8 +57,9 @@ public class HMac implements Serializable {
/**
* 构造
*
* @param algorithm 算法 {@link HmacAlgorithm}
* @param key 密钥
* @param key 密钥
*/
public HMac(HmacAlgorithm algorithm, Key key) {
this(algorithm.getValue(), key);
@@ -62,8 +67,9 @@ public class HMac implements Serializable {
/**
* 构造
*
* @param algorithm 算法
* @param key 密钥
* @param key 密钥
* @since 4.5.13
*/
public HMac(String algorithm, byte[] key) {
@@ -72,8 +78,9 @@ public class HMac implements Serializable {
/**
* 构造
*
* @param algorithm 算法
* @param key 密钥
* @param key 密钥
* @since 4.5.13
*/
public HMac(String algorithm, Key key) {
@@ -82,6 +89,7 @@ public class HMac implements Serializable {
/**
* 构造
*
* @param engine MAC算法实现引擎
* @since 4.5.13
*/
@@ -95,15 +103,16 @@ public class HMac implements Serializable {
*
* @return MAC算法引擎
*/
public MacEngine getEngine(){
public MacEngine getEngine() {
return this.engine;
}
// ------------------------------------------------------------------------------------------- Digest
/**
* 生成文件摘要
*
* @param data 被摘要数据
* @param data 被摘要数据
* @param charset 编码
* @return 摘要
*/
@@ -122,9 +131,30 @@ public class HMac implements Serializable {
}
/**
* 生成文件摘要,并转为16进制字符串
* 生成文件摘要,并转为Base64
*
* @param data 被摘要数据
* @return 摘要
*/
public String digestBase64(String data, boolean isUrlSafe) {
return digestBase64(data, CharsetUtil.CHARSET_UTF_8, isUrlSafe);
}
/**
* 生成文件摘要并转为Base64
*
* @param data 被摘要数据
* @param charset 编码
* @return 摘要
*/
public String digestBase64(String data, Charset charset, boolean isUrlSafe) {
return Base64.encodeStr(digest(data, charset), false, isUrlSafe);
}
/**
* 生成文件摘要并转为16进制字符串
*
* @param data 被摘要数据
* @param charset 编码
* @return 摘要
*/
@@ -150,12 +180,12 @@ public class HMac implements Serializable {
* @return 摘要bytes
* @throws CryptoException Cause by IOException
*/
public byte[] digest(File file) throws CryptoException{
public byte[] digest(File file) throws CryptoException {
InputStream in = null;
try {
in = FileUtil.getInputStream(file);
return digest(in);
} finally{
} finally {
IoUtil.close(in);
}
}
@@ -215,7 +245,7 @@ public class HMac implements Serializable {
/**
* 生成摘要
*
* @param data {@link InputStream} 数据流
* @param data {@link InputStream} 数据流
* @param bufferLength 缓存长度不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 摘要bytes
*/
@@ -227,7 +257,7 @@ public class HMac implements Serializable {
* 生成摘要并转为16进制字符串<br>
* 使用默认缓存大小,见 {@link IoUtil#DEFAULT_BUFFER_SIZE}
*
* @param data 被摘要数据
* @param data 被摘要数据
* @param bufferLength 缓存长度不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 摘要
*/
@@ -239,22 +269,23 @@ public class HMac implements Serializable {
* 验证生成的摘要与给定的摘要比较是否一致<br>
* 简单比较每个byte位是否相同
*
* @param digest 生成的摘要
* @param digest 生成的摘要
* @param digestToCompare 需要比较的摘要
* @return 是否一致
* @since 5.6.8
* @see MessageDigest#isEqual(byte[], byte[])
* @since 5.6.8
*/
public boolean verify(byte[] digest, byte[] digestToCompare){
public boolean verify(byte[] digest, byte[] digestToCompare) {
return MessageDigest.isEqual(digest, digestToCompare);
}
/**
* 获取MAC算法块长度
*
* @return MAC算法块长度
* @since 5.3.3
*/
public int getMacLength(){
public int getMacLength() {
return this.engine.getMacLength();
}