void testRC4() { bt::SHA1Hash dkey = randomKey(); bt::SHA1Hash ekey = randomKey(); mse::RC4Encryptor a(dkey,ekey); mse::RC4Encryptor b(ekey,dkey); bt::Uint8 tmp[1024]; for (int i = 0;i < 1000;i++) { memset(tmp,0,1024); bt::Uint8 data[1024]; for (int j = 0;j < 1024;j++) data[j] = qrand() % 0xFF; memcpy(tmp,data,1024); a.encryptReplace(data,1024); b.decrypt(data,1024); QVERIFY(memcmp(tmp,data,1024) == 0); } }
TIMED_TEST(MapTests, randomKeyTest_Map, TEST_TIMEOUT_DEFAULT) { Map<std::string, int> counts; int RUNS = 200; std::initializer_list<std::string> list {"a", "b", "c", "d", "e", "f"}; Map<std::string, int> map; map["a"] = 50; map["b"] = 40; map["c"] = 30; map["d"] = 20; map["e"] = 10; map["f"] = 0; for (int i = 0; i < RUNS; i++) { std::string s = randomKey(map); counts[s]++; } for (const std::string& s : list) { assertTrue("must choose " + s + " sometimes", counts[s] > 0); } }
void hmac_MD5(char *fileName, int numOfIte) { //Code for HMAC_MD5 FILE *fin; // Names of the files used for HMAC_MD5 size_t hash_size = gcry_md_get_algo_dlen(GCRY_MD_MD5); size_t fl_rd_v; //FIle Reading variable double HMAC_MD5[100]; //Array stroing Time for each Encryption and Decryption clock_t start_HMD5, end_HMD5; //Defining Clock Functions variables to track time //gcrypt version check void grcrypt_init(){ if (!gcry_check_version (GCRYPT_VERSION)) { printf("LibGrycpt version doesn't match\n"); exit(-1); } } int j,k; //To run loop int bytes; //Scanning file BytebyByte char *key; char *buf_HMD5; gcry_md_hd_t handle_MD5; gcry_md_open(&handle_MD5,GCRY_MD_MD5,GCRY_MD_FLAG_HMAC|GCRY_MD_FLAG_SECURE); for(j=0; j<numOfIte;j++){ //Getting the value of key key = randomKey(32); printf("HMAC MD5 key for Iteration No %d is : %s ",j+1, key); //printf("The Key %s \n", key); printf("\n\n"); gcry_md_setkey(handle_MD5, key, strlen(key)); fin = fopen(fileName, "rb"); fseek(fin,0,SEEK_END); long int fileSize = ftell(fin); //fseek(fin,0,SEEK_SET); //printf("The file size is : %ld\n",fileSize); buf_HMD5 = malloc(sizeof(char) *fileSize); unsigned char *lenDig_HMD5 = NULL; start_HMD5 = clock(); //Clock for HMAC_MD5 Starts int bytes = fread(buf_HMD5, sizeof(char), fileSize-1, fin); gcry_md_write(handle_MD5, buf_HMD5, fl_rd_v);; gcry_md_final(handle_MD5); lenDig_HMD5 = gcry_md_read(handle_MD5, GCRY_MD_MD5); //message digest length, "int algo = 0" int i; printf("The Hash Generated using HMAC MD5 is: \n"); for(i=0;i<strlen(lenDig_HMD5);i++) { printf("%x",lenDig_HMD5[i]); } printf("\n"); /*while(fl_rd_v = fread(buf_HMD5, 1, fileSize-1, fin)) { gcry_md_write(handle_MD5, buf, fl_rd_v); if() }*/ end_HMD5 = clock(); //printf("End of Encrypption Iteration no %d using AES128 CBC Mode at: %ld \n ", j,end_EA128); double total_HMD5 = (double)(end_HMD5-start_HMD5)/CLOCKS_PER_SEC*1000000000; printf("\nTotal time taken for Hash Generation : %.2lf nano-seconds \n", total_HMD5); printf("--------------------------------------------------------------------------------\n"); HMAC_MD5[j] = total_HMD5; //} } gcry_md_close(handle_MD5); free(buf_HMD5); double Total_HMD5_Time=0.0; for(k=0;k<numOfIte;k++) { Total_HMD5_Time = Total_HMD5_Time + HMAC_MD5[k]; } printf("Total HASH Time (HMAC_MD5) for %d iterations is: %.2lf nano-seconds \n\n",k, Total_HMD5_Time); double meanHMD5_time = Total_HMD5_Time/numOfIte; printf("Mean Hash Time for (HMAC_MD5) %d iterations is: %.2lf nano-seconds \n\n",k, meanHMD5_time); double medianHMD5 = calculateMedian(HMAC_MD5, numOfIte); printf("The Median Hash Time for (HMAC_MD5) after %d Iterations is: %.2lf nano-seconds \n\n",numOfIte, medianHMD5); }
int main( int argc, const char** argv) { try { if (argc < 3) { throw std::runtime_error( "missing arguments <nof inserts> <nof queries>"); } unsigned int nofInserts; unsigned int nofQueries; try { nofInserts = strus::utils::toint( argv[1]); nofQueries = strus::utils::toint( argv[2]); } catch (const std::exception& e) { throw std::runtime_error( std::string("bad values for arguments <nof inserts> <nof queries> (2 non negative integers expected): ") + e.what()); } initRand(); typedef strus::StringMap<strus::Index> TestMap; TestMap testmap; conotrie::CompactNodeTrie origmap; std::vector<std::string> keyar; std::size_t ii = 0; for (; ii<nofInserts; ++ii) { std::string key; do { key = randomKey( 32); } while (testmap.find(key) != testmap.end()); testmap[ key] = ii+1; keyar.push_back( key); } testmap.clear(); std::clock_t start; double duration; start = std::clock(); for (ii=0; ii<nofInserts; ++ii) { testmap[ keyar[ ii]] = ii+1; } duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cerr << "inserted " << nofInserts << " keys in STL map in " << doubleToString(duration) << " seconds" << std::endl; start = std::clock(); for (ii=0; ii<nofInserts; ++ii) { origmap.set( keyar[ ii].c_str(), ii+1); } duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cerr << "inserted " << nofInserts << " keys in variable size node tree in " << doubleToString(duration) << " seconds" << std::endl; start = std::clock(); for (ii=0; ii<nofQueries; ++ii) { unsigned int keyidx = RANDINT(0,nofInserts); TestMap::const_iterator mi = testmap.find( keyar[ keyidx]); if (mi == testmap.end() || mi->second != (strus::Index)keyidx+1) { throw std::logic_error("TESTMAP LOOKUP FAILED"); } } duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cerr << "queried boost unordered map with " << nofQueries << " random selected keys in " << doubleToString(duration) << " seconds" << std::endl; start = std::clock(); for (ii=0; ii<nofQueries; ++ii) { unsigned int keyidx = RANDINT(0,nofInserts); conotrie::CompactNodeTrie::NodeData val; if (!origmap.get( keyar[ keyidx].c_str(), val) || val != keyidx+1) { throw std::runtime_error( "VARIABLE SIZE NODE LOOKUP FAILED"); } } duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cerr << "queried variable size node tree " << nofQueries << " random selected keys in " << doubleToString(duration) << " seconds" << std::endl; TestMap::const_iterator ti = testmap.begin(), te = testmap.end(); for (; ti != te; ++ti) { conotrie::CompactNodeTrie::NodeData val; if (!origmap.get( ti->first, val)) { throw std::runtime_error( std::string( "inserted key '") + ti->first + "' disapeared in variable size node tree"); } } conotrie::CompactNodeTrie::const_iterator oi = origmap.begin(), oe = origmap.end(); std::size_t oidx = 0; for (; oi != oe; ++oi,++oidx) { TestMap::const_iterator ti = testmap.find( oi.key()); if (ti == testmap.end()) { throw std::runtime_error( std::string( "non existing key '") + oi.key() + "' found in variable size node tree"); } else if (ti->second != (strus::Index)oi.data()) { std::ostringstream dt; dt << ti->second << " != " << oi.data(); throw std::runtime_error( std::string( "inserted key '") + oi.key() + "' has not the expected value (" + dt.str() + ")"); } } if (oidx != nofInserts) { std::ostringstream dt; dt << oidx << " != " << nofInserts; throw std::runtime_error( std::string( "number of inserts in variable size node tree does not match (") + dt.str() + ")"); } std::cerr << "checked inserted content in maps. OK" << std::endl; return 0; } catch (const std::exception& err) { std::cerr << "EXCEPTION " << err.what() << std::endl; } return -1; }