Skip to content

Instantly share code, notes, and snippets.

@0guzhan
Created August 24, 2015 12:29
Show Gist options
  • Save 0guzhan/a72114e53f31b7dfece3 to your computer and use it in GitHub Desktop.
Save 0guzhan/a72114e53f31b7dfece3 to your computer and use it in GitHub Desktop.
Sequential MD5 generator
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Generator {
final protected static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
final static String LINE_SEPARATOR = System.getProperty("line.separator");
final static MessageDigest MD5;
static {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
MD5 = digest;
}
}
public static void main(String[] args) throws IOException {
outputGeneratedMD5("D:\\zero_padded_numbers.txt");
}
private static void outputGeneratedMD5(String output) throws IOException {
long timestamp = System.currentTimeMillis();
FileWriter fileWriter = new FileWriter(output);
for (int i = 0; i < 100000000; i++) {
String source = String.format("%08d", i);
byte[] sourceArray = source.getBytes();
String line = generateMD5(sourceArray) + ":" + source + LINE_SEPARATOR;
fileWriter.append(line);
}
fileWriter.close();
System.out.println("Generation took " + (System.currentTimeMillis() - timestamp) + " ms");
}
private static String generateMD5(byte[] plaintText) {
byte[] digest = MD5.digest(plaintText);
return bytesToHex(digest);
}
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment