mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
通过添加系统属性hutool.crypto.decodeHex强制关闭hex识别以解决hex和Base64歧义问题
This commit is contained in:
parent
6accf8fca0
commit
e948273f2d
@ -13,6 +13,7 @@
|
|||||||
package org.dromara.hutool.core.codec;
|
package org.dromara.hutool.core.codec;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.binary.Base16Codec;
|
import org.dromara.hutool.core.codec.binary.Base16Codec;
|
||||||
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.text.CharPool;
|
import org.dromara.hutool.core.text.CharPool;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
|
||||||
* Hutool is licensed under Mulan PSL v2.
|
* Hutool is licensed under Mulan PSL v2.
|
||||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
* You may obtain a copy of Mulan PSL v2 at:
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
@ -10,13 +10,12 @@
|
|||||||
* See the Mulan PSL v2 for more details.
|
* See the Mulan PSL v2 for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.dromara.hutool.core.codec;
|
package org.dromara.hutool.core.codec.binary;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.binary.Base16Codec;
|
|
||||||
import org.dromara.hutool.core.exception.HutoolException;
|
import org.dromara.hutool.core.exception.HutoolException;
|
||||||
|
import org.dromara.hutool.core.text.CharUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.dromara.hutool.core.text.CharUtil;
|
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
@ -35,43 +34,15 @@ import java.nio.charset.Charset;
|
|||||||
*/
|
*/
|
||||||
public class HexUtil {
|
public class HexUtil {
|
||||||
|
|
||||||
/**
|
// region ----- encode
|
||||||
* 判断给定字符串是否为16进制数<br>
|
|
||||||
* 如果是,需要使用对应数字类型对象的{@code decode}方法解码<br>
|
|
||||||
* 例如:{@code Integer.decode}方法解码int类型的16进制数字
|
|
||||||
*
|
|
||||||
* @param value 值
|
|
||||||
* @return 是否为16进制
|
|
||||||
*/
|
|
||||||
public static boolean isHexNumber(final String value) {
|
|
||||||
if (StrUtil.startWith(value, '-')) {
|
|
||||||
// issue#2875
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
int index = 0;
|
|
||||||
if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
|
|
||||||
index += 2;
|
|
||||||
} else if (value.startsWith("#", index)) {
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
new BigInteger(value.substring(index), 16);
|
|
||||||
} catch (final NumberFormatException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------- encode
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字节数组转换为十六进制字符数组
|
* 将字节数组转换为十六进制字符数组
|
||||||
*
|
*
|
||||||
* @param data byte[]
|
* @param data byte[]
|
||||||
* @return 十六进制char[]
|
* @return 十六进制char[]
|
||||||
*/
|
*/
|
||||||
public static char[] encodeHex(final byte[] data) {
|
public static char[] encode(final byte[] data) {
|
||||||
return encodeHex(data, true);
|
return encode(data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,8 +52,8 @@ public class HexUtil {
|
|||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
* @return 十六进制char[]
|
* @return 十六进制char[]
|
||||||
*/
|
*/
|
||||||
public static char[] encodeHex(final String str, final Charset charset) {
|
public static char[] encode(final String str, final Charset charset) {
|
||||||
return encodeHex(ByteUtil.toBytes(str, charset), true);
|
return encode(ByteUtil.toBytes(str, charset), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,7 +63,7 @@ public class HexUtil {
|
|||||||
* @param toLowerCase {@code true} 传换成小写格式 , {@code false} 传换成大写格式
|
* @param toLowerCase {@code true} 传换成小写格式 , {@code false} 传换成大写格式
|
||||||
* @return 十六进制char[]。如果提供的data为{@code null},返回{@code null}
|
* @return 十六进制char[]。如果提供的data为{@code null},返回{@code null}
|
||||||
*/
|
*/
|
||||||
public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
|
public static char[] encode(final byte[] data, final boolean toLowerCase) {
|
||||||
if(null == data){
|
if(null == data){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -105,8 +76,18 @@ public class HexUtil {
|
|||||||
* @param data byte[]
|
* @param data byte[]
|
||||||
* @return 十六进制String
|
* @return 十六进制String
|
||||||
*/
|
*/
|
||||||
public static String encodeHexStr(final byte[] data) {
|
public static String encodeStr(final byte[] data) {
|
||||||
return encodeHexStr(data, true);
|
return encodeStr(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转换为十六进制字符串,结果为小写,默认编码是UTF-8
|
||||||
|
*
|
||||||
|
* @param data 被编码的字符串
|
||||||
|
* @return 十六进制String
|
||||||
|
*/
|
||||||
|
public static String encodeStr(final String data) {
|
||||||
|
return encodeStr(data, CharsetUtil.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,18 +97,8 @@ public class HexUtil {
|
|||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
* @return 十六进制String
|
* @return 十六进制String
|
||||||
*/
|
*/
|
||||||
public static String encodeHexStr(final String data, final Charset charset) {
|
public static String encodeStr(final String data, final Charset charset) {
|
||||||
return encodeHexStr(ByteUtil.toBytes(data, charset), true);
|
return encodeStr(ByteUtil.toBytes(data, charset), true);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将字符串转换为十六进制字符串,结果为小写,默认编码是UTF-8
|
|
||||||
*
|
|
||||||
* @param data 被编码的字符串
|
|
||||||
* @return 十六进制String
|
|
||||||
*/
|
|
||||||
public static String encodeHexStr(final String data) {
|
|
||||||
return encodeHexStr(data, CharsetUtil.UTF_8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,11 +108,12 @@ public class HexUtil {
|
|||||||
* @param toLowerCase {@code true} 传换成小写格式 , {@code false} 传换成大写格式
|
* @param toLowerCase {@code true} 传换成小写格式 , {@code false} 传换成大写格式
|
||||||
* @return 十六进制String
|
* @return 十六进制String
|
||||||
*/
|
*/
|
||||||
public static String encodeHexStr(final byte[] data, final boolean toLowerCase) {
|
public static String encodeStr(final byte[] data, final boolean toLowerCase) {
|
||||||
return StrUtil.str(encodeHex(data, toLowerCase), CharsetUtil.UTF_8);
|
return StrUtil.str(encode(data, toLowerCase), CharsetUtil.UTF_8);
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------- decode
|
// region ----- decode
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将十六进制字符数组转换为字符串,默认编码UTF-8
|
* 将十六进制字符数组转换为字符串,默认编码UTF-8
|
||||||
@ -149,8 +121,8 @@ public class HexUtil {
|
|||||||
* @param hexStr 十六进制String
|
* @param hexStr 十六进制String
|
||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String decodeHexStr(final String hexStr) {
|
public static String decodeStr(final String hexStr) {
|
||||||
return decodeHexStr(hexStr, CharsetUtil.UTF_8);
|
return decodeStr(hexStr, CharsetUtil.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,11 +132,11 @@ public class HexUtil {
|
|||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String decodeHexStr(final String hexStr, final Charset charset) {
|
public static String decodeStr(final String hexStr, final Charset charset) {
|
||||||
if (StrUtil.isEmpty(hexStr)) {
|
if (StrUtil.isEmpty(hexStr)) {
|
||||||
return hexStr;
|
return hexStr;
|
||||||
}
|
}
|
||||||
return StrUtil.str(decodeHex(hexStr), charset);
|
return StrUtil.str(decode(hexStr), charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,8 +146,8 @@ public class HexUtil {
|
|||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String decodeHexStr(final char[] hexData, final Charset charset) {
|
public static String decodeStr(final char[] hexData, final Charset charset) {
|
||||||
return StrUtil.str(decodeHex(hexData), charset);
|
return StrUtil.str(decode(hexData), charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -184,8 +156,8 @@ public class HexUtil {
|
|||||||
* @param hexStr 十六进制String
|
* @param hexStr 十六进制String
|
||||||
* @return byte[]
|
* @return byte[]
|
||||||
*/
|
*/
|
||||||
public static byte[] decodeHex(final String hexStr) {
|
public static byte[] decode(final String hexStr) {
|
||||||
return decodeHex((CharSequence) hexStr);
|
return decode((CharSequence) hexStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,8 +167,8 @@ public class HexUtil {
|
|||||||
* @return byte[]
|
* @return byte[]
|
||||||
* @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
* @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
||||||
*/
|
*/
|
||||||
public static byte[] decodeHex(final char[] hexData) {
|
public static byte[] decode(final char[] hexData) {
|
||||||
return decodeHex(String.valueOf(hexData));
|
return decode(String.valueOf(hexData));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -207,11 +179,13 @@ public class HexUtil {
|
|||||||
* @throws HutoolException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
* @throws HutoolException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
||||||
* @since 5.6.6
|
* @since 5.6.6
|
||||||
*/
|
*/
|
||||||
public static byte[] decodeHex(final CharSequence hexData) {
|
public static byte[] decode(final CharSequence hexData) {
|
||||||
return Base16Codec.CODEC_LOWER.decode(hexData);
|
return Base16Codec.CODEC_LOWER.decode(hexData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------- Color
|
// endregion
|
||||||
|
|
||||||
|
// region ----- Color
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link Color}编码为Hex形式
|
* 将{@link Color}编码为Hex形式
|
||||||
@ -264,6 +238,35 @@ public class HexUtil {
|
|||||||
return Color.decode(hexColor);
|
return Color.decode(hexColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断给定字符串是否为16进制数<br>
|
||||||
|
* 如果是,需要使用对应数字类型对象的{@code decode}方法解码<br>
|
||||||
|
* 例如:{@code Integer.decode}方法解码int类型的16进制数字
|
||||||
|
*
|
||||||
|
* @param value 值
|
||||||
|
* @return 是否为16进制
|
||||||
|
*/
|
||||||
|
public static boolean isHexNumber(final String value) {
|
||||||
|
if (StrUtil.startWith(value, '-')) {
|
||||||
|
// issue#2875
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
|
||||||
|
index += 2;
|
||||||
|
} else if (value.startsWith("#", index)) {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
new BigInteger(value.substring(index), 16);
|
||||||
|
} catch (final NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定int值转换为Unicode字符串形式,常用于特殊字符(例如汉字)转Unicode形式<br>
|
* 将指定int值转换为Unicode字符串形式,常用于特殊字符(例如汉字)转Unicode形式<br>
|
||||||
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
@ -14,7 +14,7 @@ package org.dromara.hutool.core.convert;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.convert.impl.CollectionConverter;
|
import org.dromara.hutool.core.convert.impl.CollectionConverter;
|
||||||
import org.dromara.hutool.core.convert.impl.EnumConverter;
|
import org.dromara.hutool.core.convert.impl.EnumConverter;
|
||||||
import org.dromara.hutool.core.convert.impl.MapConverter;
|
import org.dromara.hutool.core.convert.impl.MapConverter;
|
||||||
@ -874,10 +874,10 @@ public class Convert {
|
|||||||
* @param str 待转换的ASCII字符串
|
* @param str 待转换的ASCII字符串
|
||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
* @return 16进制字符串
|
* @return 16进制字符串
|
||||||
* @see HexUtil#encodeHexStr(String, Charset)
|
* @see HexUtil#encodeStr(String, Charset)
|
||||||
*/
|
*/
|
||||||
public static String toHex(final String str, final Charset charset) {
|
public static String toHex(final String str, final Charset charset) {
|
||||||
return HexUtil.encodeHexStr(str, charset);
|
return HexUtil.encodeStr(str, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -885,10 +885,10 @@ public class Convert {
|
|||||||
*
|
*
|
||||||
* @param bytes 被转换的byte数组
|
* @param bytes 被转换的byte数组
|
||||||
* @return 转换后的值
|
* @return 转换后的值
|
||||||
* @see HexUtil#encodeHexStr(byte[])
|
* @see HexUtil#encodeStr(byte[])
|
||||||
*/
|
*/
|
||||||
public static String toHex(final byte[] bytes) {
|
public static String toHex(final byte[] bytes) {
|
||||||
return HexUtil.encodeHexStr(bytes);
|
return HexUtil.encodeStr(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -896,10 +896,10 @@ public class Convert {
|
|||||||
*
|
*
|
||||||
* @param src Byte字符串,每个Byte之间没有分隔符
|
* @param src Byte字符串,每个Byte之间没有分隔符
|
||||||
* @return byte[]
|
* @return byte[]
|
||||||
* @see HexUtil#decodeHex(char[])
|
* @see HexUtil#decode(char[])
|
||||||
*/
|
*/
|
||||||
public static byte[] hexToBytes(final String src) {
|
public static byte[] hexToBytes(final String src) {
|
||||||
return HexUtil.decodeHex(src.toCharArray());
|
return HexUtil.decode(src.toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -908,11 +908,11 @@ public class Convert {
|
|||||||
* @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B])
|
* @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B])
|
||||||
* @param charset 编码 {@link Charset}
|
* @param charset 编码 {@link Charset}
|
||||||
* @return 对应的字符串
|
* @return 对应的字符串
|
||||||
* @see HexUtil#decodeHexStr(String, Charset)
|
* @see HexUtil#decodeStr(String, Charset)
|
||||||
* @since 4.1.11
|
* @since 4.1.11
|
||||||
*/
|
*/
|
||||||
public static String hexToStr(final String hexStr, final Charset charset) {
|
public static String hexToStr(final String hexStr, final Charset charset) {
|
||||||
return HexUtil.decodeHexStr(hexStr, charset);
|
return HexUtil.decodeStr(hexStr, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io;
|
package org.dromara.hutool.core.io;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.collection.iter.LineIter;
|
import org.dromara.hutool.core.collection.iter.LineIter;
|
||||||
import org.dromara.hutool.core.exception.HutoolException;
|
import org.dromara.hutool.core.exception.HutoolException;
|
||||||
import org.dromara.hutool.core.io.copy.FileChannelCopier;
|
import org.dromara.hutool.core.io.copy.FileChannelCopier;
|
||||||
@ -404,7 +404,7 @@ public class IoUtil extends NioUtil {
|
|||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public static String readHex(final InputStream in, final int length, final boolean toLowerCase) throws IORuntimeException {
|
public static String readHex(final InputStream in, final int length, final boolean toLowerCase) throws IORuntimeException {
|
||||||
return HexUtil.encodeHexStr(readBytes(in, length), toLowerCase);
|
return HexUtil.encodeStr(readBytes(in, length), toLowerCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io.checksum.crc16;
|
package org.dromara.hutool.core.io.checksum.crc16;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io.file;
|
package org.dromara.hutool.core.io.file;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
@ -71,7 +71,7 @@ public class FileTypeUtil {
|
|||||||
return fileTypeEntry.getValue();
|
return fileTypeEntry.getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final byte[] bytes = (HexUtil.decodeHex(fileStreamHexHead));
|
final byte[] bytes = (HexUtil.decode(fileStreamHexHead));
|
||||||
return FileMagicNumber.getMagicNumber(bytes).getExtension();
|
return FileMagicNumber.getMagicNumber(bytes).getExtension();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.text;
|
package org.dromara.hutool.core.text;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 提供Unicode字符串和普通字符串之间的转换
|
* 提供Unicode字符串和普通字符串之间的转换
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.codec.hash.metro;
|
package org.dromara.hutool.core.codec.hash.metro;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.Number128;
|
import org.dromara.hutool.core.codec.Number128;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
@ -128,6 +128,6 @@ public class MetroHash128Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String hex(final byte[] bytes){
|
private static String hex(final byte[] bytes){
|
||||||
return HexUtil.encodeHexStr(bytes).toUpperCase();
|
return HexUtil.encodeStr(bytes).toUpperCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.codec.hash.metro;
|
package org.dromara.hutool.core.codec.hash.metro;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -125,6 +125,6 @@ public class MetroHash64Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String hex(final byte[] bytes){
|
private static String hex(final byte[] bytes){
|
||||||
return HexUtil.encodeHexStr(bytes).toUpperCase();
|
return HexUtil.encodeStr(bytes).toUpperCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
package org.dromara.hutool.core.codec.hash.metro;
|
package org.dromara.hutool.core.codec.hash.metro;
|
||||||
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.hash.CityHash;
|
import org.dromara.hutool.core.codec.hash.CityHash;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.convert;
|
package org.dromara.hutool.core.convert;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.collection.set.SetUtil;
|
import org.dromara.hutool.core.collection.set.SetUtil;
|
||||||
import org.dromara.hutool.core.date.DateException;
|
import org.dromara.hutool.core.date.DateException;
|
||||||
import org.dromara.hutool.core.date.DateUtil;
|
import org.dromara.hutool.core.date.DateUtil;
|
||||||
@ -416,7 +416,7 @@ public class ConvertTest {
|
|||||||
public void toFloatTest(){
|
public void toFloatTest(){
|
||||||
// https://gitee.com/dromara/hutool/issues/I4M0E4
|
// https://gitee.com/dromara/hutool/issues/I4M0E4
|
||||||
final String hex2 = "CD0CCB43";
|
final String hex2 = "CD0CCB43";
|
||||||
final byte[] value = HexUtil.decodeHex(hex2);
|
final byte[] value = HexUtil.decode(hex2);
|
||||||
final float f = Convert.toFloat(value);
|
final float f = Convert.toFloat(value);
|
||||||
Assertions.assertEquals(406.1F, f, 0);
|
Assertions.assertEquals(406.1F, f, 0);
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io.checksum;
|
package org.dromara.hutool.core.io.checksum;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.io.checksum.crc16.CRC16XModem;
|
import org.dromara.hutool.core.io.checksum.crc16.CRC16XModem;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
@ -12,8 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.util;
|
package org.dromara.hutool.core.util;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -30,15 +29,15 @@ public class HexUtilTest {
|
|||||||
public void hexStrTest(){
|
public void hexStrTest(){
|
||||||
final String str = "我是一个字符串";
|
final String str = "我是一个字符串";
|
||||||
|
|
||||||
final String hex = HexUtil.encodeHexStr(str, CharsetUtil.UTF_8);
|
final String hex = HexUtil.encodeStr(str, CharsetUtil.UTF_8);
|
||||||
final String decodedStr = HexUtil.decodeHexStr(hex);
|
final String decodedStr = HexUtil.decodeStr(hex);
|
||||||
|
|
||||||
Assertions.assertEquals(str, decodedStr);
|
Assertions.assertEquals(str, decodedStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void issueI50MI6Test(){
|
public void issueI50MI6Test(){
|
||||||
final String s = HexUtil.encodeHexStr("烟".getBytes(StandardCharsets.UTF_16BE));
|
final String s = HexUtil.encodeStr("烟".getBytes(StandardCharsets.UTF_16BE));
|
||||||
Assertions.assertEquals("70df", s);
|
Assertions.assertEquals("70df", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +74,8 @@ public class HexUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void decodeTest(){
|
public void decodeTest(){
|
||||||
final String str = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
|
final String str = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
|
||||||
Assertions.assertArrayEquals(HexUtil.decodeHex(str),
|
Assertions.assertArrayEquals(HexUtil.decode(str),
|
||||||
HexUtil.decodeHex(str.toUpperCase()));
|
HexUtil.decode(str.toUpperCase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -88,8 +87,8 @@ public class HexUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void decodeHexTest(){
|
public void decodeHexTest(){
|
||||||
final String s = HexUtil.encodeHexStr("6");
|
final String s = HexUtil.encodeStr("6");
|
||||||
final String s1 = HexUtil.decodeHexStr(s);
|
final String s1 = HexUtil.decodeStr(s);
|
||||||
Assertions.assertEquals("6", s1);
|
Assertions.assertEquals("6", s1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,22 +12,23 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto;
|
package org.dromara.hutool.crypto;
|
||||||
|
|
||||||
|
import org.bouncycastle.crypto.AlphabetMapper;
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
|
||||||
import org.dromara.hutool.core.lang.Validator;
|
import org.dromara.hutool.core.lang.Validator;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
|
import org.dromara.hutool.core.util.SystemUtil;
|
||||||
import org.dromara.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
import org.dromara.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||||
import org.dromara.hutool.crypto.asymmetric.RSA;
|
import org.dromara.hutool.crypto.asymmetric.RSA;
|
||||||
import org.dromara.hutool.crypto.digest.DigestAlgorithm;
|
import org.dromara.hutool.crypto.digest.DigestAlgorithm;
|
||||||
import org.dromara.hutool.crypto.digest.Digester;
|
import org.dromara.hutool.crypto.digest.Digester;
|
||||||
|
import org.dromara.hutool.crypto.digest.MD5;
|
||||||
import org.dromara.hutool.crypto.digest.mac.HMac;
|
import org.dromara.hutool.crypto.digest.mac.HMac;
|
||||||
import org.dromara.hutool.crypto.digest.mac.HmacAlgorithm;
|
import org.dromara.hutool.crypto.digest.mac.HmacAlgorithm;
|
||||||
import org.dromara.hutool.crypto.digest.MD5;
|
|
||||||
import org.dromara.hutool.crypto.provider.GlobalProviderFactory;
|
import org.dromara.hutool.crypto.provider.GlobalProviderFactory;
|
||||||
import org.dromara.hutool.crypto.symmetric.*;
|
import org.dromara.hutool.crypto.symmetric.*;
|
||||||
import org.bouncycastle.crypto.AlphabetMapper;
|
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.Mac;
|
import javax.crypto.Mac;
|
||||||
@ -51,6 +52,9 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
public class SecureUtil {
|
public class SecureUtil {
|
||||||
|
|
||||||
|
/** Hutool自定义系统属性:是否解码Hex字符 issue#I90M9D */
|
||||||
|
public static String HUTOOL_CRYPTO_DECODE_HEX = "hutool.crypto.decodeHex";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成算法,格式为XXXwithXXX
|
* 生成算法,格式为XXXwithXXX
|
||||||
*
|
*
|
||||||
@ -528,7 +532,11 @@ public class SecureUtil {
|
|||||||
if(Objects.isNull(key)){
|
if(Objects.isNull(key)){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return Validator.isHex(key) ? HexUtil.decodeHex(key) : Base64.decode(key);
|
|
||||||
|
// issue#I90M9D
|
||||||
|
// 某些特殊字符串会无法区分Hex还是Base64,此处使用系统属性强制关闭Hex解析
|
||||||
|
final boolean decodeHex = SystemUtil.getBoolean(HUTOOL_CRYPTO_DECODE_HEX, true);
|
||||||
|
return (decodeHex && Validator.isHex(key)) ? HexUtil.decode(key) : Base64.decode(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.asymmetric;
|
package org.dromara.hutool.crypto.asymmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
@ -51,7 +51,7 @@ public interface AsymmetricEncryptor {
|
|||||||
* @return Hex字符串
|
* @return Hex字符串
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final byte[] data, final KeyType keyType) {
|
default String encryptHex(final byte[] data, final KeyType keyType) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data, keyType));
|
return HexUtil.encodeStr(encrypt(data, keyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,7 +98,7 @@ public interface AsymmetricEncryptor {
|
|||||||
* @since 4.0.1
|
* @since 4.0.1
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final String data, final KeyType keyType) {
|
default String encryptHex(final String data, final KeyType keyType) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data, keyType));
|
return HexUtil.encodeStr(encrypt(data, keyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,7 +111,7 @@ public interface AsymmetricEncryptor {
|
|||||||
* @since 4.0.1
|
* @since 4.0.1
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final String data, final Charset charset, final KeyType keyType) {
|
default String encryptHex(final String data, final Charset charset, final KeyType keyType) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data, charset, keyType));
|
return HexUtil.encodeStr(encrypt(data, charset, keyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,7 +160,7 @@ public interface AsymmetricEncryptor {
|
|||||||
* @since 4.0.1
|
* @since 4.0.1
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final InputStream data, final KeyType keyType) {
|
default String encryptHex(final InputStream data, final KeyType keyType) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data, keyType));
|
return HexUtil.encodeStr(encrypt(data, keyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +27,7 @@ import org.bouncycastle.crypto.signers.SM2Signer;
|
|||||||
import org.bouncycastle.crypto.signers.StandardDSAEncoding;
|
import org.bouncycastle.crypto.signers.StandardDSAEncoding;
|
||||||
import org.bouncycastle.util.BigIntegers;
|
import org.bouncycastle.util.BigIntegers;
|
||||||
import org.bouncycastle.util.encoders.Hex;
|
import org.bouncycastle.util.encoders.Hex;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.crypto.CryptoException;
|
import org.dromara.hutool.crypto.CryptoException;
|
||||||
import org.dromara.hutool.crypto.SecureUtil;
|
import org.dromara.hutool.crypto.SecureUtil;
|
||||||
@ -347,7 +347,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|||||||
* @return 签名
|
* @return 签名
|
||||||
*/
|
*/
|
||||||
public String signHex(final String dataHex, final String idHex) {
|
public String signHex(final String dataHex, final String idHex) {
|
||||||
return HexUtil.encodeHexStr(sign(HexUtil.decodeHex(dataHex), HexUtil.decodeHex(idHex)));
|
return HexUtil.encodeStr(sign(HexUtil.decode(dataHex), HexUtil.decode(idHex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -409,7 +409,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
|||||||
* @since 5.2.0
|
* @since 5.2.0
|
||||||
*/
|
*/
|
||||||
public boolean verifyHex(final String dataHex, final String signHex, final String idHex) {
|
public boolean verifyHex(final String dataHex, final String signHex, final String idHex) {
|
||||||
return verify(HexUtil.decodeHex(dataHex), HexUtil.decodeHex(signHex), HexUtil.decodeHex(idHex));
|
return verify(HexUtil.decode(dataHex), HexUtil.decode(signHex), HexUtil.decode(idHex));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.asymmetric;
|
package org.dromara.hutool.crypto.asymmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.collection.CollUtil;
|
import org.dromara.hutool.core.collection.CollUtil;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
@ -229,7 +229,7 @@ public class Sign extends BaseAsymmetric<Sign> {
|
|||||||
* @since 5.7.0
|
* @since 5.7.0
|
||||||
*/
|
*/
|
||||||
public String signHex(final String data, final Charset charset) {
|
public String signHex(final String data, final Charset charset) {
|
||||||
return HexUtil.encodeHexStr(sign(data, charset));
|
return HexUtil.encodeStr(sign(data, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -261,7 +261,7 @@ public class Sign extends BaseAsymmetric<Sign> {
|
|||||||
* @since 5.7.0
|
* @since 5.7.0
|
||||||
*/
|
*/
|
||||||
public String signHex(final byte[] data) {
|
public String signHex(final byte[] data) {
|
||||||
return HexUtil.encodeHexStr(sign(data));
|
return HexUtil.encodeStr(sign(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -273,7 +273,7 @@ public class Sign extends BaseAsymmetric<Sign> {
|
|||||||
* @since 5.7.0
|
* @since 5.7.0
|
||||||
*/
|
*/
|
||||||
public String signHex(final InputStream data) {
|
public String signHex(final InputStream data) {
|
||||||
return HexUtil.encodeHexStr(sign(data));
|
return HexUtil.encodeStr(sign(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -297,7 +297,7 @@ public class Sign extends BaseAsymmetric<Sign> {
|
|||||||
* @since 5.7.0
|
* @since 5.7.0
|
||||||
*/
|
*/
|
||||||
public String digestHex(final InputStream data, final int bufferLength) {
|
public String digestHex(final InputStream data, final int bufferLength) {
|
||||||
return HexUtil.encodeHexStr(sign(data, bufferLength));
|
return HexUtil.encodeStr(sign(data, bufferLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
package org.dromara.hutool.crypto.digest;
|
package org.dromara.hutool.crypto.digest;
|
||||||
|
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
|
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
@ -196,7 +196,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
|
|||||||
* @since 4.6.0
|
* @since 4.6.0
|
||||||
*/
|
*/
|
||||||
public String digestHex(final String data, final Charset charset) {
|
public String digestHex(final String data, final Charset charset) {
|
||||||
return HexUtil.encodeHexStr(digest(data, charset));
|
return HexUtil.encodeStr(digest(data, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -235,7 +235,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final File file) {
|
public String digestHex(final File file) {
|
||||||
return HexUtil.encodeHexStr(digest(file));
|
return HexUtil.encodeStr(digest(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -274,7 +274,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final byte[] data) {
|
public String digestHex(final byte[] data) {
|
||||||
return HexUtil.encodeHexStr(digest(data));
|
return HexUtil.encodeStr(digest(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -295,7 +295,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final InputStream data) {
|
public String digestHex(final InputStream data) {
|
||||||
return HexUtil.encodeHexStr(digest(data));
|
return HexUtil.encodeStr(digest(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -334,7 +334,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final InputStream data, final int bufferLength) {
|
public String digestHex(final InputStream data, final int bufferLength) {
|
||||||
return HexUtil.encodeHexStr(digest(data, bufferLength));
|
return HexUtil.encodeStr(digest(data, bufferLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.digest.mac;
|
package org.dromara.hutool.crypto.digest.mac;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
import org.dromara.hutool.core.io.file.FileUtil;
|
import org.dromara.hutool.core.io.file.FileUtil;
|
||||||
@ -116,7 +116,7 @@ public class Mac implements Serializable {
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final String data, final Charset charset) {
|
public String digestHex(final String data, final Charset charset) {
|
||||||
return HexUtil.encodeHexStr(digest(data, charset));
|
return HexUtil.encodeStr(digest(data, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -155,7 +155,7 @@ public class Mac implements Serializable {
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final File file) {
|
public String digestHex(final File file) {
|
||||||
return HexUtil.encodeHexStr(digest(file));
|
return HexUtil.encodeStr(digest(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -175,7 +175,7 @@ public class Mac implements Serializable {
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final byte[] data) {
|
public String digestHex(final byte[] data) {
|
||||||
return HexUtil.encodeHexStr(digest(data));
|
return HexUtil.encodeStr(digest(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -196,7 +196,7 @@ public class Mac implements Serializable {
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final InputStream data) {
|
public String digestHex(final InputStream data) {
|
||||||
return HexUtil.encodeHexStr(digest(data));
|
return HexUtil.encodeStr(digest(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -219,7 +219,7 @@ public class Mac implements Serializable {
|
|||||||
* @return 摘要
|
* @return 摘要
|
||||||
*/
|
*/
|
||||||
public String digestHex(final InputStream data, final int bufferLength) {
|
public String digestHex(final InputStream data, final int bufferLength) {
|
||||||
return HexUtil.encodeHexStr(digest(data, bufferLength));
|
return HexUtil.encodeStr(digest(data, bufferLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.symmetric;
|
package org.dromara.hutool.crypto.symmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.crypto.KeyUtil;
|
import org.dromara.hutool.crypto.KeyUtil;
|
||||||
|
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
@ -73,6 +73,6 @@ public class PBKDF2 {
|
|||||||
* @return 加密后的密码
|
* @return 加密后的密码
|
||||||
*/
|
*/
|
||||||
public String encryptHex(final char[] password, final byte[] salt) {
|
public String encryptHex(final char[] password, final byte[] salt) {
|
||||||
return HexUtil.encodeHexStr(encrypt(password, salt));
|
return HexUtil.encodeStr(encrypt(password, salt));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
package org.dromara.hutool.crypto.symmetric;
|
package org.dromara.hutool.crypto.symmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
@ -287,7 +287,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
|
|||||||
* @since 5.6.8
|
* @since 5.6.8
|
||||||
*/
|
*/
|
||||||
public String updateHex(final byte[] data) {
|
public String updateHex(final byte[] data) {
|
||||||
return HexUtil.encodeHexStr(update(data));
|
return HexUtil.encodeStr(update(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------- Encrypt
|
// --------------------------------------------------------------------------------- Encrypt
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.symmetric;
|
package org.dromara.hutool.crypto.symmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
@ -61,7 +61,7 @@ public interface SymmetricEncryptor {
|
|||||||
* @return 加密后的Hex
|
* @return 加密后的Hex
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final byte[] data) {
|
default String encryptHex(final byte[] data) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data));
|
return HexUtil.encodeStr(encrypt(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,7 +93,7 @@ public interface SymmetricEncryptor {
|
|||||||
* @return 加密后的Hex
|
* @return 加密后的Hex
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final String data, final Charset charset) {
|
default String encryptHex(final String data, final Charset charset) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data, charset));
|
return HexUtil.encodeStr(encrypt(data, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,7 +125,7 @@ public interface SymmetricEncryptor {
|
|||||||
* @return 加密后的Hex
|
* @return 加密后的Hex
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final String data) {
|
default String encryptHex(final String data) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data));
|
return HexUtil.encodeStr(encrypt(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,7 +156,7 @@ public interface SymmetricEncryptor {
|
|||||||
* @return 加密后的Hex
|
* @return 加密后的Hex
|
||||||
*/
|
*/
|
||||||
default String encryptHex(final InputStream data) {
|
default String encryptHex(final InputStream data) {
|
||||||
return HexUtil.encodeHexStr(encrypt(data));
|
return HexUtil.encodeStr(encrypt(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.asymmetric;
|
package org.dromara.hutool.crypto.asymmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
@ -190,7 +190,7 @@ public class RSATest {
|
|||||||
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
|
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
|
||||||
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
|
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
|
||||||
|
|
||||||
final byte[] aByte = HexUtil.decodeHex(a);
|
final byte[] aByte = HexUtil.decode(a);
|
||||||
final byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
final byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
||||||
|
|
||||||
Assertions.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
Assertions.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||||
@ -212,7 +212,7 @@ public class RSATest {
|
|||||||
//jdk原生加密
|
//jdk原生加密
|
||||||
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
final String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
|
final String result1 = HexUtil.encodeStr(cipher.doFinal(finalData));
|
||||||
|
|
||||||
//hutool加密
|
//hutool加密
|
||||||
final RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
final RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.crypto.asymmetric;
|
package org.dromara.hutool.crypto.asymmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
@ -53,8 +53,8 @@ public class SM2Test {
|
|||||||
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
||||||
final String OID = "06082A811CCF5501822D";
|
final String OID = "06082A811CCF5501822D";
|
||||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||||
Assertions.assertTrue(HexUtil.encodeHexStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
|
Assertions.assertTrue(HexUtil.encodeStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
|
||||||
Assertions.assertTrue(HexUtil.encodeHexStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
|
Assertions.assertTrue(HexUtil.encodeStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -140,7 +140,7 @@ public class SM2Test {
|
|||||||
final SM2 sm2 = new SM2(null, publicKeyHex);
|
final SM2 sm2 = new SM2(null, publicKeyHex);
|
||||||
sm2.usePlainEncoding();
|
sm2.usePlainEncoding();
|
||||||
|
|
||||||
final boolean verify = sm2.verify(dataBytes, HexUtil.decodeHex(signHex));
|
final boolean verify = sm2.verify(dataBytes, HexUtil.decode(signHex));
|
||||||
Assertions.assertTrue(verify);
|
Assertions.assertTrue(verify);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,8 +161,8 @@ public class SM2Test {
|
|||||||
|
|
||||||
final SM2 sm2 = SmUtil.sm2();
|
final SM2 sm2 = SmUtil.sm2();
|
||||||
|
|
||||||
final String sign = sm2.signHex(HexUtil.encodeHexStr(content));
|
final String sign = sm2.signHex(HexUtil.encodeStr(content));
|
||||||
final boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
final boolean verify = sm2.verifyHex(HexUtil.encodeStr(content), sign);
|
||||||
Assertions.assertTrue(verify);
|
Assertions.assertTrue(verify);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +186,8 @@ public class SM2Test {
|
|||||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||||
|
|
||||||
final SM2 sm2 = new SM2(//
|
final SM2 sm2 = new SM2(//
|
||||||
HexUtil.encodeHexStr(pair.getPrivate().getEncoded()), //
|
HexUtil.encodeStr(pair.getPrivate().getEncoded()), //
|
||||||
HexUtil.encodeHexStr(pair.getPublic().getEncoded())//
|
HexUtil.encodeStr(pair.getPublic().getEncoded())//
|
||||||
);
|
);
|
||||||
|
|
||||||
final byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
final byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
||||||
@ -200,12 +200,12 @@ public class SM2Test {
|
|||||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||||
final PublicKey publicKey = pair.getPublic();
|
final PublicKey publicKey = pair.getPublic();
|
||||||
final byte[] data = KeyUtil.encodeECPublicKey(publicKey);
|
final byte[] data = KeyUtil.encodeECPublicKey(publicKey);
|
||||||
final String encodeHex = HexUtil.encodeHexStr(data);
|
final String encodeHex = HexUtil.encodeStr(data);
|
||||||
final String encodeB64 = Base64.encode(data);
|
final String encodeB64 = Base64.encode(data);
|
||||||
final PublicKey Hexdecode = KeyUtil.decodeECPoint(encodeHex, SmUtil.SM2_CURVE_NAME);
|
final PublicKey Hexdecode = KeyUtil.decodeECPoint(encodeHex, SmUtil.SM2_CURVE_NAME);
|
||||||
final PublicKey B64decode = KeyUtil.decodeECPoint(encodeB64, SmUtil.SM2_CURVE_NAME);
|
final PublicKey B64decode = KeyUtil.decodeECPoint(encodeB64, SmUtil.SM2_CURVE_NAME);
|
||||||
Assertions.assertEquals(HexUtil.encodeHexStr(publicKey.getEncoded()), HexUtil.encodeHexStr(Hexdecode.getEncoded()));
|
Assertions.assertEquals(HexUtil.encodeStr(publicKey.getEncoded()), HexUtil.encodeStr(Hexdecode.getEncoded()));
|
||||||
Assertions.assertEquals(HexUtil.encodeHexStr(publicKey.getEncoded()), HexUtil.encodeHexStr(B64decode.getEncoded()));
|
Assertions.assertEquals(HexUtil.encodeStr(publicKey.getEncoded()), HexUtil.encodeStr(B64decode.getEncoded()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
package org.dromara.hutool.crypto.symmetric;
|
package org.dromara.hutool.crypto.symmetric;
|
||||||
|
|
||||||
import org.dromara.hutool.core.codec.binary.Base64;
|
import org.dromara.hutool.core.codec.binary.Base64;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.util.RandomUtil;
|
import org.dromara.hutool.core.util.RandomUtil;
|
||||||
import org.dromara.hutool.crypto.KeyUtil;
|
import org.dromara.hutool.crypto.KeyUtil;
|
||||||
import org.dromara.hutool.crypto.Mode;
|
import org.dromara.hutool.crypto.Mode;
|
||||||
@ -77,22 +77,22 @@ public class AESTest {
|
|||||||
public void encryptPKCS7Test2() {
|
public void encryptPKCS7Test2() {
|
||||||
// 构建
|
// 构建
|
||||||
final AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
final AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
||||||
HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
|
HexUtil.decode("0102030405060708090a0b0c0d0e0f10"));
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// 加密数据为16进制字符串
|
// 加密数据为16进制字符串
|
||||||
final String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
|
final String encryptHex = aes.encryptHex(HexUtil.decode("16c5"));
|
||||||
// 加密后的Hex
|
// 加密后的Hex
|
||||||
Assertions.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
|
Assertions.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
|
||||||
|
|
||||||
// 加密数据为16进制字符串
|
// 加密数据为16进制字符串
|
||||||
final String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
|
final String encryptHex2 = aes.encryptBase64(HexUtil.decode("16c5"));
|
||||||
// 加密后的Base64
|
// 加密后的Base64
|
||||||
Assertions.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
|
Assertions.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
|
||||||
|
|
||||||
// 解密
|
// 解密
|
||||||
Assertions.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
|
Assertions.assertEquals("16c5", HexUtil.encodeStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
|
||||||
Assertions.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt(HexUtil.encodeHexStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
|
Assertions.assertEquals("16c5", HexUtil.encodeStr(aes.decrypt(HexUtil.encodeStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
@ -14,7 +14,7 @@ package org.dromara.hutool.json;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.bean.copier.CopyOptions;
|
import org.dromara.hutool.core.bean.copier.CopyOptions;
|
||||||
import org.dromara.hutool.core.codec.HexUtil;
|
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||||
import org.dromara.hutool.core.convert.Convert;
|
import org.dromara.hutool.core.convert.Convert;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user