bool BackupSystem::file_has_changed(version_number_t &dst, FilishFso &new_file){ dst = invalid_version_number; auto path = new_file.get_mapped_path(); FileSystemObject *old_fso = nullptr; for (auto &fso : this->old_objects){ auto found = fso->find(path); if (!found || found->is_directoryish()) continue; old_fso = found; break; } if (!old_fso){ new_file.compute_hash(); return true; } zekvok_assert(old_fso); auto old_file = static_cast<FilishFso *>(old_fso); auto criterium = this->get_change_criterium(new_file); bool ret; switch (criterium){ case ChangeCriterium::ArchiveFlag: ret = new_file.get_archive_flag(); break; case ChangeCriterium::Size: ret = new_file.get_size() != old_file->get_size(); break; case ChangeCriterium::Date: ret = new_file.get_modification_time() != old_file->get_modification_time(); break; case ChangeCriterium::Hash: ret = compare_hashes(new_file, *old_file); break; case ChangeCriterium::HashAuto: ret = this->file_has_changed(new_file, *old_file); break; default: throw InvalidSwitchVariableException(); } if (!ret){ auto &hash = old_file->get_hash(); if (hash.valid) new_file.set_hash(hash.digest); auto v = old_file->get_latest_version(); new_file.set_latest_version(v); new_file.set_stream_id(old_file->get_stream_id()); dst = v; } return ret; }
int main(int argc, char **argv) { /* option_reader o; bool devQuery = false, benchmark = false; std::string target_word; o.add("deviceQuery", devQuery, option_reader::flag); o.add("benchmark", benchmark, option_reader::flag); o.add("search", target_word, option_reader::optparam); o.add("benchmark-iters", niters, option_reader::optparam); if(!o.process(argc, argv)) { std::cerr << "Usage: " << o.cmdline_usage(argv[0]) << "\n"; return -1; } if(devQuery) { return deviceQuery(); } */ // Load plaintext dictionary std::vector<std::string> ptext; std::cerr << "Loading words from stdin ...\n"; std::string word; while(std::cin >> word) { ptext.push_back(word); } std::cerr << "Loaded " << ptext.size() << " words.\n\n"; // Do search/calculation std::vector<md5hash> hashes_cpu, hashes_gpu; // Compute hashes cuda_compute_md5s(hashes_gpu, ptext); cpu_compute_md5s(hashes_cpu, ptext); // Verify the answers compare_hashes(hashes_gpu, hashes_cpu, ptext); return 0; }
int check_in_whitelist(unsigned char* sha1){ char line[256]; char fpath[100]; sprintf(fpath,"/etc/netcop/whitelist"); FILE* f_whitelist = fopen(fpath, "r"); if (!f_whitelist) { printf("unable to open whitelist file\n"); return -1; } int ret_val = 1; while(!feof(f_whitelist)){ if(fgets(line, 256, f_whitelist)){ if(!compare_hashes(sha1, line)){ ret_val = 0; break; } } } log_to_file(msg); return ret_val; }
int main(int argc, char **argv) { int i, ret, c, rad = 0, bsize = 0, numblocks = 0, ule = 0, b64mode = 0; const char *algo = "sha256"; /* default hashing algorithm */ const char *seed = NULL; char *hashstr = NULL; const char *compareStr = NULL; ut8 *compareBin = NULL; int hashstr_len = -1; int hashstr_hex = 0; ut64 algobit; RHash *ctx; RIO *io; while ((c = getopt (argc, argv, "jdDrvea:i:S:s:x:b:nBhf:t:kLqc:")) != -1) { switch (c) { case 'q': quiet = 1; break; case 'i': if (!optarg) return 1; iterations = atoi (optarg); if (iterations<0) { eprintf ("error: -i argument must be positive\n"); return 1; } break; case 'j': rad = 'j'; break; case 'S': seed = optarg; break; case 'n': numblocks = 1; break; case 'd': b64mode = 1; break; case 'D': b64mode = 2; break; case 'L': algolist (); return 0; case 'e': ule = 1; break; case 'r': rad = 1; break; case 'k': rad = 2; break; case 'a': algo = optarg; break; case 'B': incremental = 0; break; case 'b': bsize = (int)r_num_math (NULL, optarg); break; case 'f': from = r_num_math (NULL, optarg); break; case 't': to = 1+r_num_math (NULL, optarg); break; case 'v': return blob_version ("rahash2"); case 'h': return do_help (0); case 's': setHashString (optarg, 0); break; case 'x': setHashString (optarg, 1); break; case 'c': compareStr = optarg; break; default: eprintf ("rahash2: Unknown flag\n"); return 1; } } if (compareStr) { int compareBin_len; if (bsize && !incremental) { eprintf ("rahash2: Option -c incompatible with -b and -B options.\n"); return 1; } if (b64mode) { eprintf ("rahash2: Option -c incompatible with -d or -D options.\n"); return 1; } algobit = r_hash_name_to_bits(algo); // if algobit represents a single algorithm then it's a power of 2 if (!is_power_of_two (algobit)) { eprintf ("rahash2: Option -c incompatible with multiple algorithms in -a.\n"); return 1; } compareBin = malloc ((strlen (compareStr) + 1) * 2); if (!compareBin) return 1; compareBin_len = r_hex_str2bin (compareStr, compareBin); if (compareBin_len < 1) { eprintf ("rahash2: Invalid -c hex hash\n"); free (compareBin); return 1; } else if (compareBin_len != r_hash_size (algobit)) { eprintf ( "rahash2: Given -c hash has %d bytes but the selected algorithm returns %d bytes.\n", compareBin_len, r_hash_size(algobit)); free (compareBin); return 1; } } if ((st64)from>=0 && (st64)to<0) { to = 0; // end of file } if (from || to) { if (to && from>=to) { eprintf ("Invalid -f or -t offsets\n"); return 1; } } do_hash_seed (seed); if (hashstr) { #define INSIZE 32768 ret = 0; if (!strcmp (hashstr, "-")) { int res = 0; hashstr = malloc (INSIZE); if (!hashstr) return 1; res = fread ((void*)hashstr, 1, INSIZE-1, stdin); if (res<1) res = 0; hashstr[res] = '\0'; hashstr_len = res; } if (hashstr_hex) { ut8 *out = malloc ((strlen (hashstr)+1)*2); hashstr_len = r_hex_str2bin (hashstr, out); if (hashstr_len<1) { eprintf ("Invalid hex string\n"); free (out); return 1; } hashstr = (char *)out; /* out memleaks here, hashstr can't be freed */ } else { hashstr_len = strlen (hashstr); } if (from) { if (from>=hashstr_len) { eprintf ("Invalid -f.\n"); return 1; } } if (to) { if (to>hashstr_len) { eprintf ("Invalid -t.\n"); return 1; } } else { to = hashstr_len; } hashstr = hashstr + from; hashstr_len = to - from; hashstr[hashstr_len] = '\0'; hashstr_len = r_str_unescape (hashstr); switch (b64mode) { case 1: // encode { char *out = malloc (((hashstr_len+1)*4)/3); if (out) { r_base64_encode (out, (const ut8*)hashstr, hashstr_len); printf ("%s\n", out); fflush (stdout); free (out); } } break; case 2: // decode { ut8 *out = malloc (INSIZE); if (out) { int outlen = r_base64_decode (out, (const char *)hashstr, hashstr_len); write (1, out, outlen); free (out); } } break; default: { char *str = (char *)hashstr; int strsz = hashstr_len; if (_s) { // alloc/concat/resize str = malloc (strsz + s.len); if (s.prefix) { memcpy (str, s.buf, s.len); memcpy (str+s.len, hashstr, hashstr_len); } else { memcpy (str, hashstr, hashstr_len); memcpy (str+strsz, s.buf, s.len); } strsz += s.len; str[strsz] = 0; } algobit = r_hash_name_to_bits (algo); for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; ctx = r_hash_new (R_TRUE, hashbit); from = 0; to = strsz; do_hash_internal (ctx, hashbit, (const ut8*)str, strsz, rad, 1, ule); compare_hashes (ctx, compareBin, r_hash_size (algobit), &ret); r_hash_free (ctx); } } if (_s) { free (str); free (s.buf); } } } return ret; } if (optind>=argc) return do_help (1); if (numblocks) { bsize = -bsize; } else if (bsize<0) { eprintf ("rahash2: Invalid block size\n"); return 1; } io = r_io_new (); for (ret=0, i=optind; i<argc; i++) { switch (b64mode) { case 1: // encode { int binlen; char *out; ut8 *bin = (ut8*)r_file_slurp (argv[i], &binlen); if (!bin) { eprintf ("Cannot open file\n"); continue; } out = malloc (((binlen+1)*4)/3); if (out) { r_base64_encode (out, bin, binlen); printf ("%s\n", out); fflush (stdout); free (out); } free (bin); } break; case 2: // decode { int binlen, outlen; ut8 *out, *bin = (ut8*)r_file_slurp (argv[i], &binlen); if (!bin) { eprintf ("Cannot open file\n"); continue; } out = malloc (binlen+1); if (out) { outlen = r_base64_decode (out, (const char*)bin, binlen); write (1, out, outlen); free (out); } free (bin); } break; default: if (!strcmp (argv[i], "-")) { int sz = 0; ut8 *buf = (ut8*)r_stdin_slurp (&sz); char *uri = r_str_newf ("malloc://%d", sz); if (sz>0) { if (!r_io_open_nomap (io, uri, 0, 0)) { eprintf ("rahash2: Cannot open malloc://1024\n"); return 1; } r_io_pwrite (io, 0, buf, sz); } free (uri); } else { if (r_file_is_directory (argv[i])) { eprintf ("rahash2: Cannot hash directories\n"); return 1; } if (!r_io_open_nomap (io, argv[i], 0, 0)) { eprintf ("rahash2: Cannot open '%s'\n", argv[i]); return 1; } } ret |= do_hash (argv[i], algo, io, bsize, rad, ule, compareBin); } } free (hashstr); r_io_free (io); return ret; }
static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int rad, int ule, const ut8 *compare) { ut64 j, fsize, algobit = r_hash_name_to_bits (algo); RHash *ctx; ut8 *buf; int ret = 0; int i, first = 1; if (algobit == R_HASH_NONE) { eprintf ("rahash2: Invalid hashing algorithm specified\n"); return 1; } fsize = r_io_size (io); if (fsize <1) { eprintf ("rahash2: Invalid file size\n"); return 1; } if (bsize<0) bsize = fsize / -bsize; if (bsize == 0 || bsize > fsize) bsize = fsize; if (to == 0LL) to = fsize; if (from>to) { eprintf ("rahash2: Invalid -f -t range\n"); return 1; } if (fsize == -1LL) { eprintf ("rahash2: Unknown file size\n"); return 1; } buf = malloc (bsize+1); if (!buf) return 1; ctx = r_hash_new (R_TRUE, algobit); if (rad == 'j') printf ("["); if (incremental) { for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); if (rad == 'j') { if (first) { first = 0; } else { printf (","); } } if (s.buf && s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } for (j=from; j<to; j+=bsize) { int len = ((j+bsize)>to)? (to-j): bsize; r_io_pread (io, j, buf, len); do_hash_internal (ctx, hashbit, buf, len, rad, 0, ule); } if (s.buf && !s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } r_hash_do_end (ctx, i); if (iterations>0) r_hash_do_spice (ctx, i, iterations, _s); if (!*r_hash_name (i)) continue; if (!quiet && rad != 'j') printf ("%s: ", file); do_hash_print (ctx, i, dlen, rad, ule); } } if (_s) free (_s->buf); } else { /* iterate over all algorithm bits */ if (s.buf) eprintf ("Warning: Seed ignored on per-block hashing.\n"); for (i=1; i<0x800000; i<<=1) { ut64 f, t, ofrom, oto; if (algobit & i) { int hashbit = i & algobit; ofrom = from; oto = to; f = from; t = to; for (j=f; j<t; j+=bsize) { int nsize = (j+bsize<fsize)? bsize: (fsize-j); r_io_pread (io, j, buf, bsize); from = j; to = j+bsize; if (to>fsize) to = fsize; do_hash_internal (ctx, hashbit, buf, nsize, rad, 1, ule); } from = ofrom; to = oto; } } } if (rad == 'j') printf ("]\n"); compare_hashes (ctx, compare, r_hash_size (algobit), &ret); r_hash_free (ctx); free (buf); return ret; }
static void *worker(void *blks) { // State variables uint64_t lowesthash[8]; pthread_mutex_lock(&mutex); memcpy(lowesthash, global_lowest_hash, sizeof(lowesthash)); pthread_mutex_unlock(&mutex); uint8_t (*blocks)[NUM_CH][8] = (uint8_t (*)[NUM_CH][8])blks; for (long i = 0; ; i++) { // Accumulate status if (i >= iters_per_accumulate) { pthread_mutex_lock(&mutex); total_iterations += i; pthread_mutex_unlock(&mutex); i = 0; } // Do hashing uint64_t hashes[8][NUM_CH]; memcpy(hashes, INITIAL_STATES, sizeof(hashes)); sha512_compress_dual(hashes, blocks); // Compare with lowest hash if (hashes[0][0] <= lowesthash[0] || hashes[0][1] <= lowesthash[0]) { // Assumes NUM_CH = 2 pthread_mutex_lock(&mutex); for (int ch = 0; ch < NUM_CH; ch++) { if (compare_hashes(hashes, ch, global_lowest_hash) < 0) { char message[MSG_LEN + 1]; get_message(blocks, ch, message); fprintf(stdout, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 " %s\n", hashes[0][ch], hashes[1][ch], hashes[2][ch], hashes[3][ch], hashes[4][ch], hashes[5][ch], hashes[6][ch], hashes[7][ch], message); if (prev_print_type == 1) fprintf(stderr, " "); fprintf(stderr, "%016" PRIx64 "%016" PRIx64 "... %s\n", hashes[0][ch], hashes[1][ch], message); fflush(stdout); fflush(stderr); for (int j = 0; j < 8; j++) global_lowest_hash[j] = hashes[j][ch]; prev_print_type = 0; } } for (int j = 0; j < 8; j++) lowesthash[j] = global_lowest_hash[j]; pthread_mutex_unlock(&mutex); } // Increment messages int j; for (j = MSG_LEN - 1; j >= 0 && get_byte(blocks, j, 0) >= 'z'; j--) { for (int ch = 0; ch < NUM_CH; ch++) set_byte(blocks, j, ch, 'a'); } if (j < 0) break; for (int ch = 0; ch < NUM_CH; ch++) set_byte(blocks, j, ch, get_byte(blocks, j, ch) + 1); } pthread_mutex_lock(&mutex); finished_threads++; pthread_mutex_unlock(&mutex); return NULL; }