static khint_t hash_key(key_data_t key) { int i = 0; khint_t hash; if (key.single) { unsigned char sig[12]; memcpy(sig + i, &key.this_ref, 4); i += 4; memcpy(sig + i, &key.this_coord, 4); i += 4; memcpy(sig + i, &key.orientation, 4); i += 4; hash = do_hash(sig, i); } else { unsigned char sig[24]; memcpy(sig + i, &key.this_ref, 4); i += 4; memcpy(sig + i, &key.this_coord, 4); i += 4; memcpy(sig + i, &key.other_ref, 4); i += 4; memcpy(sig + i, &key.other_coord, 4); i += 4; memcpy(sig + i, &key.leftmost, 4); i += 4; memcpy(sig + i, &key.orientation, 4); i += 4; hash = do_hash(sig, i); } return hash; }
int st_find_or_add(st_table *table, char *key, char ***slot) { int hash_val; st_table_entry *newEntry, *ptr, **last; hash_val = do_hash(key, table); FIND_ENTRY(table, hash_val, key, ptr, last); if (ptr == NULL) { if (table->num_entries / table->num_bins >= table->max_density) { if (rehash(table) == ST_OUT_OF_MEM) { return ST_OUT_OF_MEM; } hash_val = do_hash(key, table); } newEntry = ABC_ALLOC(st_table_entry, 1); if (newEntry == NULL) { return ST_OUT_OF_MEM; } newEntry->key = key; newEntry->record = (char *) 0; newEntry->next = table->bins[hash_val]; table->bins[hash_val] = newEntry; table->num_entries++; if (slot != NULL) *slot = &newEntry->record; return 0; } else { if (slot != NULL) *slot = &ptr->record; return 1; } }
int st_insert(st_table *table, const char *key, char *value) { int hash_val; st_table_entry *newEntry; st_table_entry *ptr, **last; hash_val = do_hash(key, table); FIND_ENTRY(table, hash_val, key, ptr, last); if (ptr == NULL) { if (table->num_entries/table->num_bins >= table->max_density) { if (rehash(table) == ST_OUT_OF_MEM) { return ST_OUT_OF_MEM; } hash_val = do_hash(key, table); } newEntry = ABC_ALLOC(st_table_entry, 1); if (newEntry == NULL) { return ST_OUT_OF_MEM; } newEntry->key = (char *)key; newEntry->record = value; newEntry->next = table->bins[hash_val]; table->bins[hash_val] = newEntry; table->num_entries++; return 0; } else { ptr->record = value; return 1; } }
void undo( letter_t c, keyword_t i ) /**********************************/ { keyword_t index; unsigned first_weight; unsigned last_weight; /* every keyword that had a full hash value calculated because of the weight of the character specified must deregister its hash value position */ first_weight = first_scale * weights[c]; last_weight = last_scale * weights[c]; for( ; i > 0; --i ) { if( first[i] == c ) { --done[i]; if( done[i] == 1 ) { // 2 -> 1 transition index = do_hash( hash[i] ); used[index] = NULL_KEYWORD; } hash[i] -= first_weight; } if( last[i] == c ) { --done[i]; if( done[i] == 1 ) { // 2 -> 1 transition index = do_hash( hash[i] ); used[index] = NULL_KEYWORD; } hash[i] -= last_weight; } } }
extern int sha1_hash(t_hash * hashout, unsigned int size, void const * datain) { t_uint32 tmp[64 + 16]; unsigned char const * data; unsigned int inc; unsigned int orgSize; if (!hashout || !*hashout) { eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashout"); return -1; } if (size > 0 && !datain) { eventlog(eventlog_level_error, __FUNCTION__, "got NULL datain with size=%u", size); return -1; } hash_init(hashout); orgSize = size; data = (const unsigned char*)datain; while (size > 0) { if (size >= 64) inc = 64; else inc = size; if (size >= 64) { hash_set_16(tmp, data, inc, do_sha1_hash); do_hash(hashout, tmp, do_sha1_hash); } else if (size > 55){ hash_set_16(tmp, data, inc, do_sha1_hash); do_hash(hashout, tmp, do_sha1_hash); // now use blizz variant as we only wanna fill in zeros hash_set_16(tmp, data, 0, do_blizzard_hash); hash_set_length(tmp, orgSize); do_hash(hashout, tmp, do_sha1_hash); } else{ hash_set_16(tmp, data, inc, do_sha1_hash); hash_set_length(tmp, orgSize); do_hash(hashout, tmp, do_sha1_hash); } data += inc; size -= inc; } return 0; }
void dump_weights( unsigned first_index, unsigned last_index ) /************************************************************/ { int bad_mask_hash; unsigned mask; char *prt_c; letter_t c; keyword_t i; unsigned h; outfile = fopen( get_gh_filename( "weights" ), "w" ); dump_common_defs( first_index, last_index ); bad_mask_hash = 0; for( mask = 1; mask != 0; mask <<= 1 ) { if( mask >= hashsize ) { for( i = 1; i <= num_keywords; ++i ) { h = hash[i]; h &= mask - 1; if( mask != hashsize ) { if( h >= hashsize ) { h -= hashsize; } } if( h != do_hash(hash[i]) ) { output( " %s: %u+%u*%u+%u*%u=%u mod %u = %u (%u)\n", tokens[i], init_hash[i], first_scale, weights[first[i]], last_scale, weights[last[i]], hash[i], hashsize, do_hash( hash[i] ), h ); bad_mask_hash = 1; //break; } } break; } } if( bad_mask_hash ) { output( "code will use '%%' operator\n" ); } else { fprintf( outfile, "#define KEYWORD_HASH_MASK 0x%0x\n", mask - 1 ); if( mask != hashsize ) { fprintf( outfile, "#define KEYWORD_HASH_EXTRA\n" ); } } fputc( '\n', outfile ); for( c = LETTER_MIN; c <= LETTER_MAX; ++c ) { prt_c = make_define( c ); fprintf( outfile, "#define W_%s ", prt_c ); fprintf( outfile, "%2d\n", weights[c]); } fclose( outfile ); }
UniqueID::UniqueID() { time_t m_time; int m_pid; char userName[255]; char hostName[255]; int ip = 3434; time(&(m_time)); UniqueID::basedata += m_time; m_pid = _getpid(); strcpy(userName,getenv(ENVSTRING)); WSADATA wsaData; if(WSAStartup(MAKEWORD( 2, 2),&wsaData) == 0) { gethostname(hostName,255); HOSTENT * host = gethostbyname(hostName); memcpy((void*)&ip, host->h_addr, 4); } else { strcpy(hostName,"ERROR_SOCKET"); } #ifdef WIN32 MEMORYSTATUS lpBuffer; // memory status structure GlobalMemoryStatus(&lpBuffer); int paso = (lpBuffer.dwAvailVirtual >> 8) * GenRandom() + GenRandom(); #else int paso = GenRandom() * GenRandom(); #endif WIN32 char* temp = uid; sprintf(temp,"%04.4x-",m_pid); temp += 5; sprintf(temp,"%08.8x-",m_time); temp += 9; sprintf(temp,"%08.8x",do_hash(userName) + ip); temp += 8; sprintf(temp,"%08.8x-",do_hash(hostName) + ip); temp += 9; sprintf(temp,"%08.8x",paso); UniqueID::basedata++; }
int os_uuid(lua_State* L) { char uuid[38]; unsigned char bytes[16]; /* If a name argument is supplied, build the UUID from that. For speed we * are using a simple DBJ2 hashing function; if this isn't sufficient we * can switch to a full RFC 4122 §4.3 implementation later. */ const char* name = luaL_optstring(L, 1, NULL); if (name != NULL) { add(bytes, 0, do_hash(name, 0)); add(bytes, 4, do_hash(name, 'L')); add(bytes, 8, do_hash(name, 'u')); add(bytes, 12, do_hash(name, 'a')); } /* If no name is supplied, try to build one properly */ else { #if PLATFORM_WINDOWS CoCreateGuid((GUID*)bytes); #elif PLATFORM_OS2 int i; for (i = 0; i < 16; i++) bytes[i] = (unsigned char)gethrtime(); #else int result; /* not sure how to get a UUID here, so I fake it */ FILE* rnd = fopen("/dev/urandom", "rb"); result = fread(bytes, 16, 1, rnd); fclose(rnd); if (!result) { return 0; } #endif } sprintf(uuid, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]); lua_pushstring(L, uuid); return 1; }
boolean hash_ok( void ) /*********************/ { keyword_t i,j,h; for( i = 1; i <= num_keywords; ++i ) { h = do_hash( hash[i] ); for( j = i + 1; j <= num_keywords; ++j ) { if( h == do_hash( hash[j] ) ) { return( FALSE ); } } } return( TRUE ); }
bool hash_ok( void ) /*********************/ { keyword_t i,j,h; for( i = 1; i <= num_keywords; ++i ) { h = do_hash( hash[i] ); for( j = i + 1; j <= num_keywords; ++j ) { if( h == do_hash( hash[j] ) ) { return( false ); } } } return( true ); }
int st_get_key(st_table *table, register st_data_t key, st_data_t *result) { unsigned int hash_val, bin_pos; register st_table_entry *ptr; if (table->entries_packed) { int i; for (i = 0; i < table->num_entries; i++) { if ((st_data_t)table->bins[i*2] == key) { if (result !=0) *result = (st_data_t)table->bins[i*2]; return 1; } } return 0; } hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); if (ptr == 0) { return 0; } else { if (result != 0) *result = ptr->key; return 1; } }
int st_lookup(st_table *table, register st_data_t key, st_data_t *value) { unsigned int hash_val, bin_pos; register st_table_entry *ptr; if (table->entries_packed) { int i; for (i = 0; i < table->num_entries; i++) { if ((st_data_t)table->bins[i*2] == key) { if (value !=0) *value = (st_data_t)table->bins[i*2+1]; return 1; } } return 0; } hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); if (ptr == 0) { return 0; } else { if (value != 0) *value = ptr->record; return 1; } }
/* * Scans throught the section list, and if found * return it. Otherwise, return NULL. */ ConfigSection* Config::find_section(const char* section) { E_ASSERT(section != NULL); int slen = strlen(section); unsigned int hh = do_hash(section, slen); // check if we have cached section if (cached && cached->shash == hh && (strncmp(cached->sname, section, cached->snamelen) == 0)) { #ifdef CONFIG_INTERNAL printf("Found %s cached\n", cached->sname); #endif return cached; } SectionListIter it = section_list.begin(), it_end = section_list.end(); for (; it != it_end; ++it) { ConfigSection *cs = *it; if (cs->shash == hh && (strncmp(cs->sname, section, cs->snamelen) == 0)) { #ifdef CONFIG_INTERNAL printf("XXX found: %s\n", cs->sname); #endif cached = cs; return cs; } } return NULL; }
/* * Add new key/value, but first check * do we alread have and key, so if * we do, just update value */ void ConfigSection::add_entry(const char* key, const char* value) { E_ASSERT(key != NULL); E_ASSERT(value != NULL); ConfigEntry* e = find_entry(key); if (!e) { e = new ConfigEntry; e->keylen = strlen(key); e->valuelen = strlen(value); e->key = strdup(key); e->value = strdup(value); // hash the key e->hash = do_hash(e->key, e->keylen); E_ASSERT(e->key != NULL); E_ASSERT(e->value != NULL); #ifdef CONFIG_INTERNAL printf("Adding: |%s| = |%s| hash = %i section = %s\n", e->key, e->value, e->hash, sname); #endif entry_list.push_back(e); } else { #ifdef CONFIG_INTERNAL printf("!!! found duplicate for %s\n", e->key); #endif free(e->value); e->valuelen = strlen(value); e->value = strdup(value); E_ASSERT(e->value != NULL); } }
/* * ConfigSection methods */ ConfigSection::ConfigSection(const char* n) { E_ASSERT(n != NULL); snamelen = strlen(n); sname = strdup(n); shash = do_hash(sname, snamelen); }
int st_insert2(register st_table *table, register st_data_t key, st_data_t value, st_data_t (*func)(st_data_t)) { st_index_t hash_val; register st_index_t bin_pos; register st_table_entry *ptr; hash_val = do_hash(key, table); if (table->entries_packed) { st_index_t i = find_packed_index(table, hash_val, key); if (i < table->real_entries) { PVAL_SET(table, i, value); return 1; } key = (*func)(key); add_packed_direct(table, key, value, hash_val); return 0; } FIND_ENTRY(table, ptr, hash_val, bin_pos); if (ptr == 0) { key = (*func)(key); add_direct(table, key, value, hash_val, bin_pos); return 0; } else { ptr->record = value; return 1; } }
/* * Add new key/value, but first check * do we alread have and key, so if * we do, just update value */ void ConfigSection::add_entry(const char* key, const char* value) { E_ASSERT(key != NULL); E_ASSERT(value != NULL); ConfigEntry* e = find_entry(key); if (!e) { e = new ConfigEntry; e->keylen = strlen(key); e->valuelen = strlen(value); e->key = strdup(key); e->value = strdup(value); e->hash = do_hash(e->key, e->keylen); E_ASSERT(e->key != NULL); E_ASSERT(e->value != NULL); entry_list.push_back(e); } else { free(e->value); e->valuelen = strlen(value); e->value = strdup(value); E_ASSERT(e->value != NULL); } }
int string_hash(lua_State* L) { const char* str = luaL_checkstring(L, 1); int seed = (int)luaL_optinteger(L, 2, 0); lua_pushinteger(L, do_hash(str, seed)); return 1; }
int st_get_key(st_table *table, register st_data_t key, st_data_t *result) { st_index_t hash_val; register st_table_entry *ptr; hash_val = do_hash(key, table); if (table->entries_packed) { st_index_t i = find_packed_index(table, hash_val, key); if (i < table->real_entries) { if (result != 0) *result = PKEY(table, i); return 1; } return 0; } ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins)); if (ptr == 0) { return 0; } else { if (result != 0) *result = ptr->key; return 1; } }
int st_lookup(st_table *table, register st_data_t key, st_data_t *value) { st_index_t hash_val; register st_table_entry *ptr; hash_val = do_hash(key, table); if (table->entries_packed) { st_index_t i = find_packed_index(table, hash_val, key); if (i < table->real_entries) { if (value != 0) *value = PVAL(table, i); return 1; } return 0; } ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins)); if (ptr == 0) { return 0; } else { if (value != 0) *value = ptr->record; return 1; } }
int main(int argc, char **argv) { const char *algo = "md5,sha1"; /* default hashing algorithm */ int c, rad = 0, quit = 0, bsize = 0; RIO *io; while ((c = getopt (argc, argv, "rva:s:b:Bhf:t:")) != -1) { switch (c) { case 'r': rad = 1; break; case 'a': algo = optarg; break; case 'B': incremental = 0; break; case 'b': bsize = (int)r_num_math (NULL, optarg); break; case 's': { ut64 algobit = r_hash_name_to_bits (algo); RHash *ctx = r_hash_new (R_TRUE, algobit); from = 0; to = strlen (optarg); do_hash_internal (ctx, //0, strlen (optarg), algobit, (const ut8*) optarg, strlen (optarg), 0, 1); r_hash_free (ctx); quit = R_TRUE; } break; case 'f': from = r_num_math (NULL, optarg); break; case 't': to = r_num_math (NULL, optarg); break; case 'v': printf ("rahash2 v"R2_VERSION"\n"); return 0; case 'h': return do_help (0); } } if (quit) return 0; if (optind>=argc) return do_help (1); io = r_io_new (); if (!r_io_open (io, argv[optind], 0, 0)) { eprintf ("Cannot open '%s'\n", argv[optind]); return 1; } return do_hash (algo, io, bsize, rad); }
void hash_table_insert(HashTable *ht, char* key, size_t key_len, Var* value) { /* size = 53 (0 .. 52 ), num_elements = 53 ( 0 .. 52 ); */ /* how would num_elements be > than size ? */ if(ht->num_elements > ht->size) { hash_table_do_resize(ht); } uint32_t hashed_value = do_hash(key, key_len); size_t hashed_index = hashed_value % ht->size; Bucket* b = ht->buckets[hashed_index]; while(b != NULL) { /* Update the value if a key is found */ if((b->hash == hashed_value) && (b->key_len == key_len) && (memcmp(b->key, key, key_len) == 0)) { b->value = value; return; } b = b->next; } b = malloc(sizeof(Bucket)); if(b == NULL) { fprintf(stderr, "%s\n", strerror(errno)); exit(1); } b->key = malloc(key_len + 1); memcpy(b->key, key, key_len + 1); b->key_len = key_len; b->hash = hashed_value; b->value = value; b->next = ht->buckets[hashed_index]; ht->buckets[hashed_index] = b; ht->num_elements++; }
void cocoa_st_add_direct(cocoa_st_table *table, cocoa_st_data_t key, cocoa_st_data_t value) { unsigned int hash_val, bin_pos; hash_val = do_hash(key, table); bin_pos = hash_val % table->num_bins; ADD_DIRECT(table, key, value, hash_val, bin_pos); }
void st_add_direct(st_table *table, const char *key, const void *value) { unsigned int hash_val, bin_pos; hash_val = do_hash(key, table); bin_pos = hash_val % table->num_bins; ADD_DIRECT(table, key, value, hash_val, bin_pos); }
/** * TODO: We need to support more hash algs * @fekek: ECRYPTFS_MAX_KEY_BYTES bytes of allocated memory * * @passphrase A NULL-terminated char array * * @salt A salt * * @passphrase_sig An allocated char array into which the generated * signature is written; PASSWORD_SIG_SIZE bytes should be allocated * */ int generate_passphrase_sig(char *passphrase_sig, char *fekek, char *salt, char *passphrase) { char salt_and_passphrase[ECRYPTFS_MAX_PASSPHRASE_BYTES + ECRYPTFS_SALT_SIZE]; int passphrase_size; #ifdef ENABLE_NSS int alg = SEC_OID_SHA512; #else int alg = GCRY_MD_SHA512; #endif /* #ifdef ENABLE_NSS */ int dig_len = SHA512_DIGEST_LENGTH; char buf[SHA512_DIGEST_LENGTH]; int hash_iterations = ECRYPTFS_DEFAULT_NUM_HASH_ITERATIONS; int rc = 0; passphrase_size = strlen(passphrase); if (passphrase_size > ECRYPTFS_MAX_PASSPHRASE_BYTES) { passphrase_sig = NULL; syslog(LOG_ERR, "Passphrase too large (%d bytes)\n", passphrase_size); return -EINVAL; } memcpy(salt_and_passphrase, salt, ECRYPTFS_SALT_SIZE); memcpy((salt_and_passphrase + ECRYPTFS_SALT_SIZE), passphrase, passphrase_size); if ((rc = do_hash(salt_and_passphrase, (ECRYPTFS_SALT_SIZE + passphrase_size), buf, alg))) { return rc; } hash_iterations--; while (hash_iterations--) { if ((rc = do_hash(buf, dig_len, buf, alg))) { return rc; } } memcpy(fekek, buf, ECRYPTFS_MAX_KEY_BYTES); if ((rc = do_hash(buf, dig_len, buf, alg))) { return rc; } to_hex(passphrase_sig, buf, ECRYPTFS_SIG_SIZE); return 0; }
BAREBOX_CMD_END #endif /* CMD_CMD_SHA224SUM */ #ifdef CONFIG_CMD_SHA256SUM static int do_sha256(int argc, char *argv[]) { return do_hash("sha256", argc, argv); }
BAREBOX_CMD_END #endif /* CMD_CMD_SHA384SUM */ #ifdef CONFIG_CMD_SHA512SUM static int do_sha512(int argc, char *argv[]) { return do_hash("sha512", argc, argv); }
BAREBOX_CMD_END #endif /* CMD_CMD_MD5SUM */ #ifdef CONFIG_CMD_SHA1SUM static int do_sha1(int argc, char *argv[]) { return do_hash("sha1", argc, argv); }
void init_ordered( void ) /***********************/ { keyword_t i, j; for( i = 1; i <= num_keywords; ++i ) { j = do_hash( hash[i] ); ordered[j + 1] = i; } }
bool PHPBB3Password::check_hash(string password, string hash) { if (hash.length() == 34){ if (do_hash(password, hash).compare(hash) == 0) return true; return false; } else { if (md5(password).compare(hash) == 0) return true; return false; } }