Created
July 12, 2022 06:01
-
-
Save scoroberts/77cc2d5c28fa2faeb1626d8eb39ec8c8 to your computer and use it in GitHub Desktop.
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
//update of https://gist.github.com/scoroberts/a60d61a2cc3afba1e8813b338ecd1501 | |
@Grab(group='com.google.guava', module='guava', version='31.1-jre') | |
@Grab(group='commons-codec', module='commons-codec', version='1.15') | |
@Grab(group='bouncycastle', module='bcprov-jdk15', version='140') | |
@Grab('software.amazon.cryptools:AmazonCorrettoCryptoProvider:1.+:linux-x86_64') | |
import groovy.transform.CompileStatic | |
import java.security.MessageDigest | |
import java.nio.charset.StandardCharsets | |
import com.google.common.hash.Hashing | |
import org.apache.commons.codec.digest.DigestUtils | |
import org.bouncycastle.crypto.digests.SHA256Digest | |
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256 | |
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider | |
import javax.crypto.Cipher | |
String testStr = "lwkjt23uy45pojsdf;lnwo45y23po5i;lknwe;lknasdflnqw3uo5" | |
byte[] test = testStr.getBytes() | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
// com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider.install() | |
// if (Cipher.getInstance("AES/GCM/NoPadding").getProvider().getName().equals(AmazonCorrettoCryptoProvider.PROVIDER_NAME)) { | |
// println "Installed Corretto" | |
// } | |
// println ("provider: ${Cipher.getInstance("AES/GCM/NoPadding").getProvider().getName()}") | |
// AmazonCorrettoCryptoProvider.INSTANCE.assertHealthy(); | |
println digest.hashCode() | |
@CompileStatic | |
String bytesToHex(byte[] hash) { | |
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); | |
} | |
return hexString.toString(); | |
} | |
@CompileStatic | |
byte[] hashJava(byte[] bytes, MessageDigest digest){ | |
return digest.digest(bytes); | |
} | |
@CompileStatic | |
byte[] guavaHash(byte[] bytes){ | |
Hashing.sha256().hashBytes(bytes).asBytes() | |
} | |
@CompileStatic | |
byte[] apacheHash(byte[] bytes){ | |
new DigestUtils(SHA_256).digest(bytes) | |
} | |
@CompileStatic | |
byte[] bouncyHash(byte[] keyByteArray, SHA256Digest digest){ | |
byte[] output = new byte[digest.getDigestSize()]; | |
digest.update(keyByteArray, 0, keyByteArray.length); | |
digest.doFinal(output, 0); | |
return output | |
} | |
long iterations = 100_0000 | |
println "Hashing ${iterations} iterations of SHA-256" | |
//warm up, throw away | |
digest = MessageDigest.getInstance("SHA-256"); | |
for (int x=0; x<1000; x++) hashJava(test, digest) | |
long start = System.currentTimeMillis() | |
for (int x=0; x<iterations; x++) hashJava(test, digest) | |
long end = System.currentTimeMillis() | |
println ("time java: ${end-start}\t\t${iterations*1000/(end-start)} hashes/sec") | |
for (int x=0; x<1000; x++) apacheHash(test) | |
start = System.currentTimeMillis() | |
for (int x=0; x<iterations; x++) apacheHash(test) | |
end = System.currentTimeMillis() | |
println ("time apache: ${end-start}\t\t${iterations*1000/(end-start)} hashes/sec") | |
for (int x=0; x<1000; x++) guavaHash(test) | |
start = System.currentTimeMillis() | |
for (int x=0; x<iterations; x++) guavaHash(test) | |
end = System.currentTimeMillis() | |
println ("time guava: ${end-start}\t\t${iterations*1000/(end-start)} hashes/sec") | |
SHA256Digest digest2 = new SHA256Digest(); | |
for (int x=0; x<1000; x++) bouncyHash(test, digest2) | |
start = System.currentTimeMillis() | |
for (int x=0; x<iterations; x++) bouncyHash(test, digest2) | |
end = System.currentTimeMillis() | |
println ("time bouncy: ${end-start}\t\t${iterations*1000/(end-start)} hashes/sec") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated from https://gist.github.com/scoroberts/a60d61a2cc3afba1e8813b338ecd1501