如何将hexString转换为相对于md5,sha-1和sha-256的实际String

问题描述 投票:-1回答:1

我有一个类将字符串转换为hexString根据为md5,sha1 sha 256命名的值。

如何根据这些安全算法将hex String转换为String。

MD5哈希:06c219e5bc8378f3a8a3f83b4b7e4649 SHA-1哈希:e9fe51f94eadabf54dbf2fbbd57188​​b9abee436e 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);
       }

    }
}
java security hash md5 sha
1个回答
1
投票

使用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"); 
}
© www.soinside.com 2019 - 2024. All rights reserved.