通过添加系统属性hutool.crypto.decodeHex强制关闭hex识别以解决hex和Base64歧义问题

This commit is contained in:
Looly 2024-02-10 08:37:15 +08:00
parent 6accf8fca0
commit e948273f2d
26 changed files with 169 additions and 158 deletions

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.core.codec;
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.text.CharPool;
import org.dromara.hutool.core.text.StrUtil;

View File

@ -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.
* 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:
@ -10,13 +10,12 @@
* 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.text.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import java.awt.Color;
@ -35,43 +34,15 @@ import java.nio.charset.Charset;
*/
public class HexUtil {
/**
* 判断给定字符串是否为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
// region ----- encode
/**
* 将字节数组转换为十六进制字符数组
*
* @param data byte[]
* @return 十六进制char[]
*/
public static char[] encodeHex(final byte[] data) {
return encodeHex(data, true);
public static char[] encode(final byte[] data) {
return encode(data, true);
}
/**
@ -81,8 +52,8 @@ public class HexUtil {
* @param charset 编码
* @return 十六进制char[]
*/
public static char[] encodeHex(final String str, final Charset charset) {
return encodeHex(ByteUtil.toBytes(str, charset), true);
public static char[] encode(final String str, final Charset charset) {
return encode(ByteUtil.toBytes(str, charset), true);
}
/**
@ -92,7 +63,7 @@ public class HexUtil {
* @param toLowerCase {@code true} 传换成小写格式 {@code false} 传换成大写格式
* @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){
return null;
}
@ -105,8 +76,18 @@ public class HexUtil {
* @param data byte[]
* @return 十六进制String
*/
public static String encodeHexStr(final byte[] data) {
return encodeHexStr(data, true);
public static String encodeStr(final byte[] data) {
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 编码
* @return 十六进制String
*/
public static String encodeHexStr(final String data, final Charset charset) {
return encodeHexStr(ByteUtil.toBytes(data, charset), true);
}
/**
* 将字符串转换为十六进制字符串结果为小写默认编码是UTF-8
*
* @param data 被编码的字符串
* @return 十六进制String
*/
public static String encodeHexStr(final String data) {
return encodeHexStr(data, CharsetUtil.UTF_8);
public static String encodeStr(final String data, final Charset charset) {
return encodeStr(ByteUtil.toBytes(data, charset), true);
}
/**
@ -137,11 +108,12 @@ public class HexUtil {
* @param toLowerCase {@code true} 传换成小写格式 {@code false} 传换成大写格式
* @return 十六进制String
*/
public static String encodeHexStr(final byte[] data, final boolean toLowerCase) {
return StrUtil.str(encodeHex(data, toLowerCase), CharsetUtil.UTF_8);
public static String encodeStr(final byte[] data, final boolean toLowerCase) {
return StrUtil.str(encode(data, toLowerCase), CharsetUtil.UTF_8);
}
// endregion
// ---------------------------------------------------------------------------------------------------- decode
// region ----- decode
/**
* 将十六进制字符数组转换为字符串默认编码UTF-8
@ -149,8 +121,8 @@ public class HexUtil {
* @param hexStr 十六进制String
* @return 字符串
*/
public static String decodeHexStr(final String hexStr) {
return decodeHexStr(hexStr, CharsetUtil.UTF_8);
public static String decodeStr(final String hexStr) {
return decodeStr(hexStr, CharsetUtil.UTF_8);
}
/**
@ -160,11 +132,11 @@ public class HexUtil {
* @param charset 编码
* @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)) {
return hexStr;
}
return StrUtil.str(decodeHex(hexStr), charset);
return StrUtil.str(decode(hexStr), charset);
}
/**
@ -174,8 +146,8 @@ public class HexUtil {
* @param charset 编码
* @return 字符串
*/
public static String decodeHexStr(final char[] hexData, final Charset charset) {
return StrUtil.str(decodeHex(hexData), charset);
public static String decodeStr(final char[] hexData, final Charset charset) {
return StrUtil.str(decode(hexData), charset);
}
/**
@ -184,8 +156,8 @@ public class HexUtil {
* @param hexStr 十六进制String
* @return byte[]
*/
public static byte[] decodeHex(final String hexStr) {
return decodeHex((CharSequence) hexStr);
public static byte[] decode(final String hexStr) {
return decode((CharSequence) hexStr);
}
/**
@ -195,8 +167,8 @@ public class HexUtil {
* @return byte[]
* @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度将抛出运行时异常
*/
public static byte[] decodeHex(final char[] hexData) {
return decodeHex(String.valueOf(hexData));
public static byte[] decode(final char[] hexData) {
return decode(String.valueOf(hexData));
}
/**
@ -207,11 +179,13 @@ public class HexUtil {
* @throws HutoolException 如果源十六进制字符数组是一个奇怪的长度将抛出运行时异常
* @since 5.6.6
*/
public static byte[] decodeHex(final CharSequence hexData) {
public static byte[] decode(final CharSequence hexData) {
return Base16Codec.CODEC_LOWER.decode(hexData);
}
// ---------------------------------------------------------------------------------------- Color
// endregion
// region ----- Color
/**
* {@link Color}编码为Hex形式
@ -264,6 +238,35 @@ public class HexUtil {
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>
* 转换的字符串如果u后不足4位则前面用0填充例如

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.convert;
import org.dromara.hutool.core.array.ArrayUtil;
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.EnumConverter;
import org.dromara.hutool.core.convert.impl.MapConverter;
@ -874,10 +874,10 @@ public class Convert {
* @param str 待转换的ASCII字符串
* @param charset 编码
* @return 16进制字符串
* @see HexUtil#encodeHexStr(String, Charset)
* @see HexUtil#encodeStr(String, 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数组
* @return 转换后的值
* @see HexUtil#encodeHexStr(byte[])
* @see HexUtil#encodeStr(byte[])
*/
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之间没有分隔符
* @return byte[]
* @see HexUtil#decodeHex(char[])
* @see HexUtil#decode(char[])
*/
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 charset 编码 {@link Charset}
* @return 对应的字符串
* @see HexUtil#decodeHexStr(String, Charset)
* @see HexUtil#decodeStr(String, Charset)
* @since 4.1.11
*/
public static String hexToStr(final String hexStr, final Charset charset) {
return HexUtil.decodeHexStr(hexStr, charset);
return HexUtil.decodeStr(hexStr, charset);
}
/**

View File

@ -12,7 +12,7 @@
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.exception.HutoolException;
import org.dromara.hutool.core.io.copy.FileChannelCopier;
@ -404,7 +404,7 @@ public class IoUtil extends NioUtil {
* @throws IORuntimeException IO异常
*/
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);
}
/**

View File

@ -12,7 +12,7 @@
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 java.io.Serializable;

View File

@ -12,7 +12,7 @@
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.IoUtil;
import org.dromara.hutool.core.text.StrUtil;
@ -71,7 +71,7 @@ public class FileTypeUtil {
return fileTypeEntry.getValue();
}
}
final byte[] bytes = (HexUtil.decodeHex(fileStreamHexHead));
final byte[] bytes = (HexUtil.decode(fileStreamHexHead));
return FileMagicNumber.getMagicNumber(bytes).getExtension();
}

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.core.text;
import org.dromara.hutool.core.codec.HexUtil;
import org.dromara.hutool.core.codec.binary.HexUtil;
/**
* 提供Unicode字符串和普通字符串之间的转换

View File

@ -12,7 +12,7 @@
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.util.ByteUtil;
import org.junit.jupiter.api.Assertions;
@ -128,6 +128,6 @@ public class MetroHash128Test {
}
private static String hex(final byte[] bytes){
return HexUtil.encodeHexStr(bytes).toUpperCase();
return HexUtil.encodeStr(bytes).toUpperCase();
}
}

View File

@ -12,7 +12,7 @@
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.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -125,6 +125,6 @@ public class MetroHash64Test {
}
private static String hex(final byte[] bytes){
return HexUtil.encodeHexStr(bytes).toUpperCase();
return HexUtil.encodeStr(bytes).toUpperCase();
}
}

View File

@ -13,7 +13,7 @@
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.util.ByteUtil;
import org.dromara.hutool.core.util.CharsetUtil;

View File

@ -12,7 +12,7 @@
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.date.DateException;
import org.dromara.hutool.core.date.DateUtil;
@ -416,7 +416,7 @@ public class ConvertTest {
public void toFloatTest(){
// https://gitee.com/dromara/hutool/issues/I4M0E4
final String hex2 = "CD0CCB43";
final byte[] value = HexUtil.decodeHex(hex2);
final byte[] value = HexUtil.decode(hex2);
final float f = Convert.toFloat(value);
Assertions.assertEquals(406.1F, f, 0);
}

View File

@ -12,7 +12,7 @@
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.util.ByteUtil;
import org.junit.jupiter.api.Assertions;

View File

@ -12,8 +12,7 @@
package org.dromara.hutool.core.util;
import org.dromara.hutool.core.codec.HexUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.codec.binary.HexUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -30,15 +29,15 @@ public class HexUtilTest {
public void hexStrTest(){
final String str = "我是一个字符串";
final String hex = HexUtil.encodeHexStr(str, CharsetUtil.UTF_8);
final String decodedStr = HexUtil.decodeHexStr(hex);
final String hex = HexUtil.encodeStr(str, CharsetUtil.UTF_8);
final String decodedStr = HexUtil.decodeStr(hex);
Assertions.assertEquals(str, decodedStr);
}
@Test
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);
}
@ -75,8 +74,8 @@ public class HexUtilTest {
@Test
public void decodeTest(){
final String str = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
Assertions.assertArrayEquals(HexUtil.decodeHex(str),
HexUtil.decodeHex(str.toUpperCase()));
Assertions.assertArrayEquals(HexUtil.decode(str),
HexUtil.decode(str.toUpperCase()));
}
@Test
@ -88,8 +87,8 @@ public class HexUtilTest {
@Test
public void decodeHexTest(){
final String s = HexUtil.encodeHexStr("6");
final String s1 = HexUtil.decodeHexStr(s);
final String s = HexUtil.encodeStr("6");
final String s1 = HexUtil.decodeStr(s);
Assertions.assertEquals("6", s1);
}
}

View File

@ -12,22 +12,23 @@
package org.dromara.hutool.crypto;
import org.bouncycastle.crypto.AlphabetMapper;
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.HexUtil;
import org.dromara.hutool.core.lang.Validator;
import org.dromara.hutool.core.text.StrUtil;
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.RSA;
import org.dromara.hutool.crypto.digest.DigestAlgorithm;
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.HmacAlgorithm;
import org.dromara.hutool.crypto.digest.MD5;
import org.dromara.hutool.crypto.provider.GlobalProviderFactory;
import org.dromara.hutool.crypto.symmetric.*;
import org.bouncycastle.crypto.AlphabetMapper;
import javax.crypto.Cipher;
import javax.crypto.Mac;
@ -51,6 +52,9 @@ import java.util.Objects;
*/
public class SecureUtil {
/** Hutool自定义系统属性是否解码Hex字符 issue#I90M9D */
public static String HUTOOL_CRYPTO_DECODE_HEX = "hutool.crypto.decodeHex";
/**
* 生成算法格式为XXXwithXXX
*
@ -528,7 +532,11 @@ public class SecureUtil {
if(Objects.isNull(key)){
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);
}
/**

View File

@ -12,7 +12,7 @@
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.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
@ -51,7 +51,7 @@ public interface AsymmetricEncryptor {
* @return Hex字符串
*/
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
*/
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
*/
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
*/
default String encryptHex(final InputStream data, final KeyType keyType) {
return HexUtil.encodeHexStr(encrypt(data, keyType));
return HexUtil.encodeStr(encrypt(data, keyType));
}
/**

View File

@ -27,7 +27,7 @@ import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.crypto.signers.StandardDSAEncoding;
import org.bouncycastle.util.BigIntegers;
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.crypto.CryptoException;
import org.dromara.hutool.crypto.SecureUtil;
@ -347,7 +347,7 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
* @return 签名
*/
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
*/
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));
}
/**

View File

@ -12,7 +12,7 @@
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.collection.CollUtil;
import org.dromara.hutool.core.io.IoUtil;
@ -229,7 +229,7 @@ public class Sign extends BaseAsymmetric<Sign> {
* @since 5.7.0
*/
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
*/
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
*/
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
*/
public String digestHex(final InputStream data, final int bufferLength) {
return HexUtil.encodeHexStr(sign(data, bufferLength));
return HexUtil.encodeStr(sign(data, bufferLength));
}
/**

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.crypto.digest;
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.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
@ -196,7 +196,7 @@ public class Digester extends SimpleWrapper<MessageDigest> implements Serializab
* @since 4.6.0
*/
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 摘要
*/
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 摘要
*/
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 摘要
*/
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 摘要
*/
public String digestHex(final InputStream data, final int bufferLength) {
return HexUtil.encodeHexStr(digest(data, bufferLength));
return HexUtil.encodeStr(digest(data, bufferLength));
}
/**

View File

@ -12,7 +12,7 @@
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.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
@ -116,7 +116,7 @@ public class Mac implements Serializable {
* @return 摘要
*/
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 摘要
*/
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 摘要
*/
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 摘要
*/
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 摘要
*/
public String digestHex(final InputStream data, final int bufferLength) {
return HexUtil.encodeHexStr(digest(data, bufferLength));
return HexUtil.encodeStr(digest(data, bufferLength));
}
/**

View File

@ -12,7 +12,7 @@
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 javax.crypto.SecretKey;
@ -73,6 +73,6 @@ public class PBKDF2 {
* @return 加密后的密码
*/
public String encryptHex(final char[] password, final byte[] salt) {
return HexUtil.encodeHexStr(encrypt(password, salt));
return HexUtil.encodeStr(encrypt(password, salt));
}
}

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.crypto.symmetric;
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.IoUtil;
import org.dromara.hutool.core.lang.Assert;
@ -287,7 +287,7 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor,
* @since 5.6.8
*/
public String updateHex(final byte[] data) {
return HexUtil.encodeHexStr(update(data));
return HexUtil.encodeStr(update(data));
}
// --------------------------------------------------------------------------------- Encrypt

View File

@ -12,7 +12,7 @@
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.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
@ -61,7 +61,7 @@ public interface SymmetricEncryptor {
* @return 加密后的Hex
*/
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
*/
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
*/
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
*/
default String encryptHex(final InputStream data) {
return HexUtil.encodeHexStr(encrypt(data));
return HexUtil.encodeStr(encrypt(data));
}
/**

View File

@ -12,7 +12,7 @@
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.text.StrUtil;
import org.dromara.hutool.core.array.ArrayUtil;
@ -190,7 +190,7 @@ public class RSATest {
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
final byte[] aByte = HexUtil.decodeHex(a);
final byte[] aByte = HexUtil.decode(a);
final byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
Assertions.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.UTF_8));
@ -212,7 +212,7 @@ public class RSATest {
//jdk原生加密
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
final String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
final String result1 = HexUtil.encodeStr(cipher.doFinal(finalData));
//hutool加密
final RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);

View File

@ -12,7 +12,7 @@
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.text.StrUtil;
import org.dromara.hutool.core.util.ByteUtil;
@ -53,8 +53,8 @@ public class SM2Test {
// OBJECT IDENTIFIER 1.2.156.10197.1.301
final String OID = "06082A811CCF5501822D";
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
Assertions.assertTrue(HexUtil.encodeHexStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
Assertions.assertTrue(HexUtil.encodeHexStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
Assertions.assertTrue(HexUtil.encodeStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
Assertions.assertTrue(HexUtil.encodeStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
}
@Test
@ -140,7 +140,7 @@ public class SM2Test {
final SM2 sm2 = new SM2(null, publicKeyHex);
sm2.usePlainEncoding();
final boolean verify = sm2.verify(dataBytes, HexUtil.decodeHex(signHex));
final boolean verify = sm2.verify(dataBytes, HexUtil.decode(signHex));
Assertions.assertTrue(verify);
}
@ -161,8 +161,8 @@ public class SM2Test {
final SM2 sm2 = SmUtil.sm2();
final String sign = sm2.signHex(HexUtil.encodeHexStr(content));
final boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
final String sign = sm2.signHex(HexUtil.encodeStr(content));
final boolean verify = sm2.verifyHex(HexUtil.encodeStr(content), sign);
Assertions.assertTrue(verify);
}
@ -186,8 +186,8 @@ public class SM2Test {
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
final SM2 sm2 = new SM2(//
HexUtil.encodeHexStr(pair.getPrivate().getEncoded()), //
HexUtil.encodeHexStr(pair.getPublic().getEncoded())//
HexUtil.encodeStr(pair.getPrivate().getEncoded()), //
HexUtil.encodeStr(pair.getPublic().getEncoded())//
);
final byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
@ -200,12 +200,12 @@ public class SM2Test {
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
final PublicKey publicKey = pair.getPublic();
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 PublicKey Hexdecode = KeyUtil.decodeECPoint(encodeHex, 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.encodeHexStr(publicKey.getEncoded()), HexUtil.encodeHexStr(B64decode.getEncoded()));
Assertions.assertEquals(HexUtil.encodeStr(publicKey.getEncoded()), HexUtil.encodeStr(Hexdecode.getEncoded()));
Assertions.assertEquals(HexUtil.encodeStr(publicKey.getEncoded()), HexUtil.encodeStr(B64decode.getEncoded()));
}
@Test

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.crypto.symmetric;
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.crypto.KeyUtil;
import org.dromara.hutool.crypto.Mode;
@ -77,22 +77,22 @@ public class AESTest {
public void encryptPKCS7Test2() {
// 构建
final AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
HexUtil.decode("0102030405060708090a0b0c0d0e0f10"));
// ------------------------------------------------------------------------
// 加密数据为16进制字符串
final String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
final String encryptHex = aes.encryptHex(HexUtil.decode("16c5"));
// 加密后的Hex
Assertions.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
// 加密数据为16进制字符串
final String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
final String encryptHex2 = aes.encryptBase64(HexUtil.decode("16c5"));
// 加密后的Base64
Assertions.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
// 解密
Assertions.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
Assertions.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt(HexUtil.encodeHexStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
Assertions.assertEquals("16c5", HexUtil.encodeStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
Assertions.assertEquals("16c5", HexUtil.encodeStr(aes.decrypt(HexUtil.encodeStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.array.ArrayUtil;
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.io.IORuntimeException;
import org.dromara.hutool.core.lang.mutable.MutableEntry;