Skip to content

Instantly share code, notes, and snippets.

@thatlittlegit
Last active July 8, 2018 19:43
Show Gist options
  • Save thatlittlegit/05e6d5ecd81b7f63ccda370c15b1e498 to your computer and use it in GitHub Desktop.
Save thatlittlegit/05e6d5ecd81b7f63ccda370c15b1e498 to your computer and use it in GitHub Desktop.
Calculates SHA256 hashes until it finds one that is sixty-four zeros in the hex digest. Originally built in Rust; see the bottom of forallnil.c
#/* [57] forallnil.c: Loop calculating SHA256 hashes until all-zeros is
# * found.
# */
#/*
cmd="$0 -lcrypto -o forallnil -Wall $CFLAGS"
echo $cmd
exec gcc $cmd
*/
/*** BUILDING
* Build by running this file with the Bourne shell (sh).
* This file should always be 100% valid C, compilable with GCC, but
* also always a valid shell script, runnable with DASH.
*/
/*** DEPENDENCIES
* ForAllNil depends on OpenSSL, LibreSSL or compatible to calculate SHA256
* hashes.
*/
/*** HISTORY
* This was originally written in Rust. However, in my attempts to clean up my
* hard drive, I'm moving it from a multitude of Rust files to a single C file,
* Makefile and README/LICENSE in a single file.
*/
/*** LICENSE
* This code is licensed under the Unlicense.
*
* > This is free and unencumbered software released into the public domain.
* >
* > Anyone is free to copy, modify, publish, use, compile, sell, or
* > distribute this software, either in source code form or as a compiled
* > binary, for any purpose, commercial or non-commercial, and by any
* > means.
* >
* > In jurisdictions that recognize copyright laws, the author or authors
* > of this software dedicate any and all copyright interest in the
* > software to the public domain. We make this dedication for the benefit
* > of the public at large and to the detriment of our heirs and
* > successors. We intend this dedication to be an overt act of
* > relinquishment in perpetuity of all present and future rights to this
* > software under copyright law.
* >
* > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* > IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* > OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* > ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* > OTHER DEALINGS IN THE SOFTWARE.
* >
* > For more information, please refer to <http://unlicense.org/>
*/
/*** AUTHOR
* ForAllNil was written by thatlittlegit.
*/
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#define THREADS 8
#define forfrom(w, x, y) for (int w = x; w < y; w++)
#define WANTED \
"0000000000000000000000000000000000000000000000000000000000000000"
int id = 0;
char cont = 1;
static char* AUTHOR = "made by thatlittlegit.";
int main() {
strcat(malloc(strlen(AUTHOR)), AUTHOR);
SHA256_CTX ctx;
for (int thread = 0; thread < THREADS; thread++) {
id++;
if(fork() == 0) {
int threadid = id;
long long int calcd = 0;
forfrom (c, threadid * (THREADS / 2), 0xFFFFFF) {
forfrom (c_, 0, 0xFFFFFF) {
forfrom (c__, 0, 0xFFFFFF) {
if(!cont) exit(0);
/* thanks to StackOverflow answer 2458382
* (https://stackoverflow.com/a/2458382)
*/
int array[3] = { c, c_, c__ };
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx);
SHA256_Update(&ctx, (void *) &array, (sizeof(int) * 3));
SHA256_Final(hash, &ctx);
char* final = malloc(SHA256_DIGEST_LENGTH);
forfrom(c___, 0, SHA256_DIGEST_LENGTH) {
sprintf(final + c___ * 2, "%02x", hash[ c___]);
}
if (strcmp(final, WANTED) == 0) {
cont = 0;
sleep(1); // wait for other threads to clear
printf("YAY! %d:%d:%d (%lld)\n", c, c_, c__, calcd);
}
calcd++;
if(calcd % 0xFFFF == 0) {
printf("%d: %lld\n", threadid, calcd);
}
free(final);
}
}
}
exit(0); // DO NOT REMOVE!
}
}
wait(NULL);
return EXIT_SUCCESS;
}
/*** ORIGINAL RUST CODE
* (c) 2018 thatlittlegit. Under MIT license.
* Depends on "crypto-hash" 0.3.
*****************************************************************
extern crate crypto_hash;
use crypto_hash::{Algorithm, hex_digest};
static THREADS: u8 = 8;
fn main() {
let mut num = 0;
while num < THREADS {
std::thread::spawn(main_loop);
num += 1;
}
main_loop();
}
static mut START: u8 = 0;
fn main_loop() {
let start;
unsafe {
start = START;
START += 1;
}
let mut calcd = 0;
for counter in start * (THREADS / 2)..0xFF {
for counter_ in 0..0xFF {
for counter__ in 0..0xFF {
let array = [
counter, counter_, counter__
];
if hex_digest(Algorithm::SHA256, &array) == "0000000000000000000000000000000000000000000000000000000000000000" {
println!("YAY! {}:{}:{}", counter, counter_, counter__);
std::process::exit(0);
} else {
calcd += 1;
if calcd % 0xFFFF == 0 {
println!("{:?}: {:?}; {}", start, array, calcd);
}
}
}
}
}
}
*****************************************************************
* You can probably notice the naming similarities.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment