Skip to content

Instantly share code, notes, and snippets.

@erdbeerschnitzel
Created August 26, 2012 11:23
Show Gist options
  • Save erdbeerschnitzel/3477712 to your computer and use it in GitHub Desktop.
Save erdbeerschnitzel/3477712 to your computer and use it in GitHub Desktop.
Java MD5 Performance Test
import java.io.UnsupportedEncodingException;
import java.security.*;
public class md5 {
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException{
for(int a = 0; a < 10; a++){
long creationDate = System.currentTimeMillis();
for(int i = 0; i< 100000; i++){
byte[] bytesOfMessage = ("This is Dart!" + i).getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
StringBuffer sb = new StringBuffer();
// can be removed
for (int ii = 0; ii< thedigest.length; ++ii) {
sb.append(Integer.toHexString((thedigest[ii] & 0xFF) | 0x100).substring(1,3));
}
//System.out.println(sb.toString());
}
System.out.println(System.currentTimeMillis() - creationDate);
}
}
}
@jcaesar
Copy link

jcaesar commented Jan 18, 2021

This happened to pop up through Google, so just a small warning: this spends more time in toHexString than in digest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment