Created
October 13, 2022 20:57
-
-
Save jresendiz27/74a39609ea589a73371384586f35b5fd to your computer and use it in GitHub Desktop.
Sha 256 and Sha 512 with UUID salt using Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
private String convertToHex(final byte[] messageDigest) { | |
BigInteger bigint = new BigInteger(1, messageDigest); | |
String hexText = bigint.toString(16); | |
while (hexText.length() < 32) { | |
hexText = "0".concat(hexText); | |
} | |
return hexText; | |
} | |
String salt = UUID.randomUUID() | |
String pass = "hola mundo" | |
MessageDigest md256 = MessageDigest.getInstance("SHA-256"); | |
MessageDigest md512 = MessageDigest.getInstance("SHA-512"); | |
def saltDigest = md256.digest(salt.getBytes(StandardCharsets.UTF_8)); | |
def shaDigest = md256.digest(pass.getBytes(StandardCharsets.UTF_8)); | |
def finalDigest = md512.digest("${convertToHex(saltDigest)}.${convertToHex(shaDigest)}".getBytes(StandardCharsets.UTF_8)) | |
println "Sha salt >>> ${convertToHex(saltDigest)}" | |
println "Sha pass >>> ${convertToHex(shaDigest)}" | |
println "Sha final >>> ${convertToHex(finalDigest)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment