Groovy web console

subscribe to the feed Subscribe
to this
site

hashSha256

Published 3 months ago by Patricio Ascencio with tags hash
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
bytesToHex = {
    hash->
    hexString = new StringBuilder(2 * hash.length);
    for (int i = 0; i < hash.length; i++) {
        hex = Integer.toHexString(0xff & hash[i]);         
        if(hex.length() == 1) {             
            hexString.append('0');         
        }         
        hexString.append(hex);     
    }     
    return hexString.toString();
}

originalString = "jidoit"
digest = java.security.MessageDigest.getInstance("SHA-256")
encodedhash = digest.digest(originalString.getBytes("UTF-8"))

bytesToHex(encodedhash)