Compare commits
2 Commits
42f0212a40
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e3e9f81f33 | |||
| 3153afad5a |
@ -4,6 +4,7 @@ import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@ -102,4 +103,42 @@ public class Decoder {
|
||||
}
|
||||
return URLDecoder.decode(url, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 16 进制字符串转字节数据
|
||||
*
|
||||
* @param hex 16 进制字符串
|
||||
* @return 字节数据
|
||||
* @throws UnsupportedEncodingException 不支持的编码
|
||||
*/
|
||||
public static byte[] hex(String hex) throws UnsupportedEncodingException {
|
||||
final char[] c = hex.toCharArray();
|
||||
final byte[] b = new byte[c.length >> 1];
|
||||
|
||||
final int len = c.length;
|
||||
if ((len & 0x01) != 0) {
|
||||
throw new UnsupportedEncodingException("Odd number of characters.");
|
||||
}
|
||||
|
||||
final int outLen = len >> 1;
|
||||
if (c.length < outLen) {
|
||||
throw new UnsupportedEncodingException("Output array is not large enough to accommodate decoded data.");
|
||||
}
|
||||
for (int i = 0, j = 0; j < len; i++) {
|
||||
int f = toDigit(c[j], j) << 4;
|
||||
j++;
|
||||
f = f | toDigit(c[j], j);
|
||||
j++;
|
||||
b[i] = (byte) (f & 0xFF);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static int toDigit(final char ch, final int index) throws UnsupportedEncodingException {
|
||||
final int digit = Character.digit(ch, 16);
|
||||
if (digit == -1) {
|
||||
throw new UnsupportedEncodingException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ public class Digest {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
return Encoder.hex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha1 digest");
|
||||
}
|
||||
@ -72,7 +72,7 @@ public class Digest {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
return Encoder.hex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha256 digest");
|
||||
}
|
||||
|
||||
@ -159,4 +159,20 @@ public class Encoder {
|
||||
throw new TimiException(TimiCode.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字节数据转 16 进制字符串
|
||||
*
|
||||
* @param bytes 字节数据
|
||||
* @return 16 进制字符串
|
||||
*/
|
||||
public static String hex(byte[] bytes) {
|
||||
final int l = bytes.length;
|
||||
final char[] c = new char[l << 1];
|
||||
for (int i = 0, j = 0; i < l; i++) {
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4];
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[0x0F & bytes[i]];
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,14 +2,12 @@ package com.imyeyu.utils;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@ -20,63 +18,6 @@ public class Text {
|
||||
/** 十六进制小写 */
|
||||
public static char[] HEX_DIGITS_LOWER = "0123456789abcdef".toCharArray();
|
||||
|
||||
/** 十六进制大写 */
|
||||
public static char[] HEX_DIGITS_UPPER = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
/**
|
||||
* 字节数据转 16 进制字符串
|
||||
*
|
||||
* @param bytes 字节数据
|
||||
* @return 16 进制字符串
|
||||
*/
|
||||
public static String byteToHex(byte[] bytes) {
|
||||
final int l = bytes.length;
|
||||
final char[] c = new char[l << 1];
|
||||
for (int i = 0, j = 0; i < l; i++) {
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4];
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[0x0F & bytes[i]];
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 16 进制字符串转字节数据
|
||||
*
|
||||
* @param hex 16 进制字符串
|
||||
* @return 字节数据
|
||||
* @throws UnsupportedEncodingException 不支持的编码
|
||||
*/
|
||||
public static byte[] hexToByte(String hex) throws UnsupportedEncodingException {
|
||||
final char[] c = hex.toCharArray();
|
||||
final byte[] b = new byte[c.length >> 1];
|
||||
|
||||
final int len = c.length;
|
||||
if ((len & 0x01) != 0) {
|
||||
throw new UnsupportedEncodingException("Odd number of characters.");
|
||||
}
|
||||
|
||||
final int outLen = len >> 1;
|
||||
if (c.length < outLen) {
|
||||
throw new UnsupportedEncodingException("Output array is not large enough to accommodate decoded data.");
|
||||
}
|
||||
for (int i = 0, j = 0; j < len; i++) {
|
||||
int f = toDigit(c[j], j) << 4;
|
||||
j++;
|
||||
f = f | toDigit(c[j], j);
|
||||
j++;
|
||||
b[i] = (byte) (f & 0xFF);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static int toDigit(final char ch, final int index) throws UnsupportedEncodingException {
|
||||
final int digit = Character.digit(ch, 16);
|
||||
if (digit == -1) {
|
||||
throw new UnsupportedEncodingException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为半角字符
|
||||
*
|
||||
|
||||
18
src/test/java/test/TestAES.java
Normal file
18
src/test/java/test/TestAES.java
Normal file
@ -0,0 +1,18 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.utils.Encoder;
|
||||
import com.imyeyu.utils.Encryptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-12-28 11:44
|
||||
*/
|
||||
public class TestAES {
|
||||
|
||||
@Test
|
||||
public void testGenerateAESKey() {
|
||||
byte[] bytes = Encryptor.aesKey(256);
|
||||
System.out.println(Encoder.hex(bytes));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user