move Text hex to Encoder/Decoder
This commit is contained in:
@ -4,6 +4,7 @@ import com.imyeyu.java.bean.timi.TimiCode;
|
|||||||
import com.imyeyu.java.bean.timi.TimiException;
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
@ -102,4 +103,42 @@ public class Decoder {
|
|||||||
}
|
}
|
||||||
return URLDecoder.decode(url, StandardCharsets.UTF_8);
|
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 {
|
try {
|
||||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||||
sha.update(bytes);
|
sha.update(bytes);
|
||||||
return Text.byteToHex(sha.digest());
|
return Encoder.hex(sha.digest());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
throw new UnsupportedOperationException("unsupported sha1 digest");
|
throw new UnsupportedOperationException("unsupported sha1 digest");
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ public class Digest {
|
|||||||
try {
|
try {
|
||||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||||
sha.update(bytes);
|
sha.update(bytes);
|
||||||
return Text.byteToHex(sha.digest());
|
return Encoder.hex(sha.digest());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
throw new UnsupportedOperationException("unsupported sha256 digest");
|
throw new UnsupportedOperationException("unsupported sha256 digest");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -159,4 +159,20 @@ public class Encoder {
|
|||||||
throw new TimiException(TimiCode.ERROR, e.getMessage());
|
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 com.imyeyu.java.TimiJava;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
@ -20,63 +18,6 @@ public class Text {
|
|||||||
/** 十六进制小写 */
|
/** 十六进制小写 */
|
||||||
public static char[] HEX_DIGITS_LOWER = "0123456789abcdef".toCharArray();
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否为半角字符
|
* 是否为半角字符
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user