Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created June 21, 2012 16:48
Show Gist options
  • Select an option

  • Save JanPaulEttles/2966947 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/2966947 to your computer and use it in GitHub Desktop.
keystretching
import java.security.MessageDigest;
public class KeyStretching {
public static void main(String[] args)
throws Exception {
String key = "";
String password = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String salt = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//as this is purely an example, I'm going to use sha-256
//you may want to use something less 'old' in production
MessageDigest md = MessageDigest.getInstance("SHA-256");
long start = System.currentTimeMillis();
byte[] byteData;
StringBuilder sb = new StringBuilder(64);
for (int a = 0; a < 100000; a++) {
md.update((key + password + salt).getBytes());
byteData = md.digest();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
key = sb.toString();
sb.delete(0, sb.length());
}
float elapsed = (System.currentTimeMillis() - start) / 1000F;
System.out.println("Time : " + elapsed);
System.out.println("Hex format : " + key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment