/* Print errors to stderr if occured. * Returns true if error occured and false in normally case. */ bool get_file_hash(u256_t hash, const char * filepath) { reader_state rs; u256_t message; if (open_reader(&rs, filepath)) { print_error(stderr, rs.err_msg); return rs.err; } hasher_state hs; init_hasher(&hs); do { int cnt = read_u256(&rs, message); if (cnt < 1) { if (!rs.err && rs.eof) { break; } // Ignore possible close errors. close_reader(&rs); print_error(stderr, rs.err_msg); return rs.err; } make_hasher_step(&hs, message, cnt); } while (!rs.eof); // As defined in GOST R 34.11-94. if (rs.filesize == 0) { make_hasher_step(&hs, message, 0u); } if (close_reader(&rs)) { print_error(stderr, rs.err_msg); return rs.err; } get_hash(hash, &hs); return rs.err; }
int main (int argc, char **argv) { git_index *index = NULL; pthread_t updateThread, hashThread; int rc, difficulty; void *status; hash_args args; timing_info timing; git_oid curr_commit; reset_timing(&timing); pthread_mutex_init(&commit_mutex, NULL); pthread_mutex_init(&update_mutex, NULL); push_commit = NULL; difficulty = init_args(&args); init_git(&index); init_hasher(difficulty); check_updates(); reset_hard(); puts("Starting update thread"); rc = pthread_create(&updateThread, NULL, check_updates_worker, NULL); if (rc){ printf("ERROR creating update thread %d\n", rc); exit(-1); } signal (SIGINT, int_handler); while(!stop){ start_timing(&timing); args.found = 0; time_point(&timing); pthread_mutex_lock(&commit_mutex); pthread_mutex_lock(&update_mutex); if(updated){ reset_hard(); updated = 0; push_commit = NULL; } pthread_mutex_unlock(&update_mutex); pthread_mutex_unlock(&commit_mutex); time_point(&timing); puts("Preparing index"); prepare_index(index, args.msg); time_point(&timing); puts("Starting brute force thread"); rc = pthread_create(&hashThread, NULL, force_hash, &args); time_point(&timing); if (rc){ printf("ERROR creating hash thread %d\n", rc); stop = 1; } else { pthread_join(hashThread, &status); } time_point(&timing); if(!stop && !updated && args.found){ puts("Found one!"); while(push_commit){ usleep(10); } time_point(&timing); if(!stop && !updated){ pthread_mutex_lock(&commit_mutex); commit_result(args.msg, &curr_commit); push_commit = &curr_commit; pthread_mutex_unlock(&commit_mutex); } } else { puts("Reset while looking for a hash"); time_point(&timing); } time_point(&timing); print_timing(&timing); } pthread_join(updateThread, &status); free_hasher(); free(args.msg); git_index_free(index); git_repository_free(repo); git_threads_shutdown(); return 0; }
int main(int argc, char **argv){ int i, j; char unused_stop = 0; int iters = 10; hash_args args; char msg[BUFFER_LENGTH]; unsigned char hash[SHA_DIGEST_LENGTH]; char hex_hash[SHA_DIGEST_LENGTH*2]; char hex_difficulty[SHA_DIGEST_LENGTH*2] = "00000008ffffffffffffffffffffffffffffffff"; unsigned char difficulty; struct timeval start, end, diff; unsigned long total = 0, curr; // Fill in some deterministic data for(i = 0; i < BUFFER_LENGTH; i++){ msg[i] = i % 128; } pad_message(msg, COMMIT_LENGTH, BUFFER_LENGTH); args.stop = &unused_stop; args.msg = (char*)msg; if(argc > 1){ iters = atoi(argv[1]); } if(argc > 2){ memcpy(hex_difficulty, argv[2], SHA_DIGEST_LENGTH*2); } difficulty = parse_difficulty(hex_difficulty); printf("Starting benchmark with difficulty %02x\n", difficulty); init_hasher(difficulty); for(i = 0; i < iters; i++){ memset(msg, 0, COMMIT_LENGTH); *((int*)(&(msg[0]))) = i; args.found = 0; cudaProfilerStart(); gettimeofday(&start, NULL); force_hash(&args); gettimeofday(&end, NULL); cudaProfilerStop(); timersub(&end, &start, &diff); curr = diff.tv_sec * 1000 + diff.tv_usec / 1000; total += curr; SHA1(msg, COMMIT_LENGTH, hash); for(j=0; j < 20; j++){ sprintf(&hex_hash[j*2], "%02x", hash[j] & 0xff); } if(memcmp(hex_hash, hex_difficulty, SHA_DIGEST_LENGTH*2) > 0){ printf("Msg:"); for(j = 0; j < BUFFER_LENGTH; j++){ printf("%02x", msg[j] & 0xff); } printf("\nBad hash: %.40s\n", hex_hash); exit(1); } else { printf("Successful run in %ld ms: %.40s\n", curr, hex_hash); } if(!args.found){ puts("Failed to find a hash!"); exit(1); } printf("\n"); } printf("\n%ld ms per iteration (%d iters, %.40s difficulty)\n", total / iters, iters, hex_difficulty); free_hasher(); exit(0); }