如何根据 md5、sha-1 和 sha-256 将 hexString 转换为实际字符串
How to convert hexString to actual String with respect to md5,sha-1 and sha-256
我有一个 class 根据为 md5,sha1 sha 256 命名的值将字符串转换为 hexString。
如何根据这些安全算法将 hexString 转换为 String。
MD5 哈希:06c219e5bc8378f3a8a3f83b4b7e4649
SHA-1 哈希:e9fe51f94eadabf54dbf2fbbd57188b9abee436e
SHA-256 哈希:652c7dc687d98c9889304ed2e408c74b611e86a40caa51c4b43f1dd5913c5cd0
实际字符串:mysecret
下面是我的代码片段。
public class HashGenerator {
private HashGenerator() {
}
public static String generateMD5(String message) throws HashGenerationException {
return hashString(message, "MD5");
}
public static String generateSHA1(String message) throws HashGenerationException {
return hashString(message, "SHA-1");
}
public static String generateSHA256(String message) throws HashGenerationException {
return hashString(message, "SHA-256");
}
public static String convertFromMD5(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "MD5");
}
public static String convertFromSHA1(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "SHA-1");
}
public static String convertFromSHA256(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "SHA-256");
}
private static String hashString(String message, String algorithm)
throws HashGenerationException {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] hashedBytes = digest.digest(message.getBytes("UTF-8"));
return convertByteArrayToHexString(hashedBytes);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
throw new HashGenerationException(
"Could not generate hash from String", ex);
}
}
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arrayBytes.length; i++) {
stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
return stringBuffer.toString();
}
public static String hexStringtoByteArray(String str, String algorithm)
{
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; i++)
{
bytes[i] = (byte) Integer
.parseInt(str.substring(2 * i, 2 * i + 2), 16);
}
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] hashedBytes = digest.digest(bytes);
return new String(hashedBytes);
} catch (Exception ex) {
throw new HashGenerationException(
"Could not generate hash from String", ex);
}
}
}
一旦使用 hexStringtoByteArray
从十六进制字符串中获得字节数组,就可以使用 new String(bytes, "UTF-8")
创建字符串,其中 bytes 是方法中的字节数组。通过在创建字符串时指定 UTF-8,您可以获得十六进制以外的字符。
因此新方法如下:
public String hexStringtoByteArray(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(str.substring(2 * i, 2 * i + 2), 16);
}
return new String(bytes, "UTF-8");
}
我有一个 class 根据为 md5,sha1 sha 256 命名的值将字符串转换为 hexString。
如何根据这些安全算法将 hexString 转换为 String。
MD5 哈希:06c219e5bc8378f3a8a3f83b4b7e4649 SHA-1 哈希:e9fe51f94eadabf54dbf2fbbd57188b9abee436e SHA-256 哈希:652c7dc687d98c9889304ed2e408c74b611e86a40caa51c4b43f1dd5913c5cd0 实际字符串:mysecret
下面是我的代码片段。
public class HashGenerator {
private HashGenerator() {
}
public static String generateMD5(String message) throws HashGenerationException {
return hashString(message, "MD5");
}
public static String generateSHA1(String message) throws HashGenerationException {
return hashString(message, "SHA-1");
}
public static String generateSHA256(String message) throws HashGenerationException {
return hashString(message, "SHA-256");
}
public static String convertFromMD5(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "MD5");
}
public static String convertFromSHA1(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "SHA-1");
}
public static String convertFromSHA256(String message) throws HashGenerationException{
return hexStringtoByteArray(message, "SHA-256");
}
private static String hashString(String message, String algorithm)
throws HashGenerationException {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] hashedBytes = digest.digest(message.getBytes("UTF-8"));
return convertByteArrayToHexString(hashedBytes);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
throw new HashGenerationException(
"Could not generate hash from String", ex);
}
}
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arrayBytes.length; i++) {
stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
return stringBuffer.toString();
}
public static String hexStringtoByteArray(String str, String algorithm)
{
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; i++)
{
bytes[i] = (byte) Integer
.parseInt(str.substring(2 * i, 2 * i + 2), 16);
}
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] hashedBytes = digest.digest(bytes);
return new String(hashedBytes);
} catch (Exception ex) {
throw new HashGenerationException(
"Could not generate hash from String", ex);
}
}
}
一旦使用 hexStringtoByteArray
从十六进制字符串中获得字节数组,就可以使用 new String(bytes, "UTF-8")
创建字符串,其中 bytes 是方法中的字节数组。通过在创建字符串时指定 UTF-8,您可以获得十六进制以外的字符。
因此新方法如下:
public String hexStringtoByteArray(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(str.substring(2 * i, 2 * i + 2), 16);
}
return new String(bytes, "UTF-8");
}