0
MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(base.getBytes("UTF-8"));
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if(hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }

i wanted to convert "hexString" to string that contains only "a-zA-z0-9[!@#$%&*()_+=&|<>?{}[]~""]-" characters.

output may repeat in some cases but it should produce same string every time for same hexString

  • 1
    I thought there was a Generic BaseN Encoder out there, but I can't find it. Take a look at Apache Commons Codec Base64 to see how they did it. https://commons.apache.org/proper/commons-codec/apidocs/src-html/org/apache/commons/codec/binary/Base64.html – Lukas Bradley Aug 07 '17 at 17:12
  • 1
    @LukasBradley You're right. the was an old "internal" one, an "ApacheCommons" implementation and in java8 it became an official JVM class: https://stackoverflow.com/questions/13109588/base64-encoding-in-java – Timothy Truckle Aug 07 '17 at 17:19

0 Answers0