Skip to content

Instantly share code, notes, and snippets.

@calebrob6
Created October 14, 2014 20:21
Show Gist options
  • Save calebrob6/70785a95889016a4fe5d to your computer and use it in GitHub Desktop.
Save calebrob6/70785a95889016a4fe5d to your computer and use it in GitHub Desktop.
Java hashing program for the SHA-256 challenge
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
/**
*
* Java CPU SHA-256 Hashing program
*
* Will use as many threads as you have available CPUS, sends good results to the website
*
* @author dcrobins
*
*/
public class DoWork extends Thread {
static volatile int best = 0;
final Random seeder = new Random();
final static String TO = "http://micha.elwillia.ms/attempt";
final static String KEY = "5339b9db84a3c8af60fae0328bc47859";
//final static int n = 10000000;
public static void postHash(String hash){
try {
URL obj = new URL(TO);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
String urlParameters = "attempt="+hash+"&key="+KEY;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* This is the code run inside of each thread that we make
*/
public void run() {
System.out.println("Starting new hashing thread");
Random rand = new Random(seeder.nextLong()); //each thread gets its own random number object seeded from a main Random
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256"); // This is what does the main hashing work for us
} catch (NoSuchAlgorithmException e) {
System.err.print("Weird");
e.printStackTrace();
}
//long startTime = System.currentTimeMillis();
//int numHashed = 0;
//while(numHashed<n){
while (true) { // often means that the loop will go on forever
// this makes a string of 8 random characters between ASCII 48 and 122, see http://www.asciitable.com/ to see what these characters are
byte[] randomString = { (byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48),
(byte) (rand.nextInt(74) + 48) };
//hashes our random string, we get a byte array back that we need to count 0's on
byte[] hash = digest.digest(randomString);
int count = 0;
byte firstMask = (byte) 0xF0; //if we bitwise AND this with a byte it will tell us if the first 4 bits are 0
byte secondMask = (byte) 0x0F; //likewise
for (int i = 0; i < hash.length; i++) {
if ((hash[i] & firstMask) == 0) count++; //found a 0 at the first hex character
else break;
if ((hash[i] & secondMask) == 0) count++; //found a 0 at the second hex character
else break;
}
if (count > best) {
best = count;
try {
String goodString = new String(randomString, "UTF-8"); //we can convert our hack randomString into an actual String like this
System.out.println(String.format("%s\t%d", goodString, count));
postHash(goodString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//numHashed++;
}
//System.out.println(String.format("Thread finished hashing %d iterations in %d milliseconds",n,(System.currentTimeMillis()-startTime)));
}
public static void main(String[] args) {
int numThreads = Runtime.getRuntime().availableProcessors(); //gets the number of threads that we can hash with, using anything greater than this will be worthless
System.out.println(String.format("Doing work on %d threads", numThreads));
for (int i = 0; i < numThreads; i++)
(new DoWork()).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment