简体   繁体   中英

How to convert hexString to actual String with respect to md5,sha-1 and sha-256

I have a class converting string to hexString according to the value named for md5,sha1 sha 256.

How can I convert hexString to String according to these security algoritm.

MD5 Hash: 06c219e5bc8378f3a8a3f83b4b7e4649 SHA-1 Hash: e9fe51f94eadabf54dbf2fbbd57188b9abee436e SHA-256 Hash: 652c7dc687d98c9889304ed2e408c74b611e86a40caa51c4b43f1dd5913c5cd0 Actual String : mysecret

Here is my code snippet shown below.

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);
       }

    }
}

Once you have a byte array from your hex string using hexStringtoByteArray , you can make a string using new String(bytes, "UTF-8") where bytes is the byte array from your method. By specifying UTF-8 when creating the string you get characters other than hex.

Therefore the new method would be as follows:

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"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM