Hmac::Hmac(HCRYPTPROV cryptProv, ALG_ID algId, const ByteVector& keyMaterial) :cryptProv(cryptProv), algId(algId) { ByteVector key(keyMaterial); if (key.size() > CryptoProxy::HASH_ALG_BLOCKSIZE) { CryptoHash keyHash(cryptProv, algId, key); keyHash.getValue(key); assert((algId == CALG_SHA1 && key.size() == 20) || (algId == CALG_MD5 && key.size() == 16)); } iPad = key; iPad.append(0, CryptoProxy::HASH_ALG_BLOCKSIZE - static_cast<int>(key.size())); oPad = key; oPad.append(0, CryptoProxy::HASH_ALG_BLOCKSIZE - static_cast<int>(key.size())); for (size_t i =0; i < CryptoProxy::HASH_ALG_BLOCKSIZE; ++i) { iPad[i] ^= 0x36; oPad[i] ^= 0x5c; } }
void initCodeBufs( comcryptBuf *cbuf, const unsigned char *key, unsigned keyLen, unsigned char laEnable, unsigned char sigSeqEnable) { unsigned ct; unsigned qval; unsigned char khash = (unsigned char)keyHash(key, keyLen); cbuf->nybbleDex = khash; if(laEnable) { memset(cbuf->lookAhead, 0, LOOKAHEAD_SIZE); } laprintf(("initing queue and lookahead\n")); for(ct=0; ct<QLEN; ct++) { /* * New queue init 23 Dec - init from khash */ unsigned short sbyte = ct ^ khash; qval = (sbyte << 8) | ct; cbuf->queue[ct] = qval; if(laEnable) { markInQueue(cbuf, qval, 1); } } // note cbuf->nybbleDex = khash on return... cbuf->f1 = F1_DEFAULT; cbuf->f2 = F2_DEFAULT; cbuf->jmatchThresh = THRESH_2LEVEL_JMATCH_DEF; cbuf->minByteCode = THRESH_2LEVEL_NUMBYTECODES_DEF; if(sigSeqEnable) { initSigSequence(cbuf, key, keyLen); } }
void GeoAccumulator::getPointsFor(const BSONObj& key, const BSONObj& obj, vector<BSONObj> &locsForNode, bool allPoints) { // Find all the location objects from the keys vector<BSONObj> locs; _accessMethod->getKeys(obj, allPoints ? locsForNode : locs); ++_pointsLoaded; if (allPoints) return; if (locs.size() == 1){ locsForNode.push_back(locs[0]); return; } // Find the particular location we want GeoHash keyHash(key.firstElement(), _converter->getBits()); for(vector< BSONObj >::iterator i = locs.begin(); i != locs.end(); ++i) { // Ignore all locations not hashed to the key's hash, since we may see // those later if(_converter->hash(*i) != keyHash) continue; locsForNode.push_back(*i); } }
/** * get hash code from a key * * @param key: * @return : * @throws * */ virtual unsigned int hash(const void* key, int partitionNumber) const { boost::hash<KeyType> keyHash; return (unsigned int)keyHash(*(KeyType*)key) % partitionNumber; };
bool handleKeys(arguments* args, ThreefishKey_t* cipher_context, MacCtx_t* mac_context) { pdebug("handleKeys()\n"); //sanity check if (args == NULL || cipher_context == NULL || mac_context == NULL) { return false; } const uint64_t block_byte_size = (uint64_t)args->state_size/8; uint64_t* cipher_key = NULL; uint64_t* mac_key = NULL; //no hash password (bad idea) if (args->hash == false && args->hash_from_file == false) { cipher_key = noHashKey(args->password, args->pw_length, args->state_size); } //no hash from file (bad idea) else if (args->hash == false && args->hash_from_file == true) { cipher_key = noHashBlockFromFile(args->key_file, args->state_size); } //hash user entered password to be hashed else if (args->hash == true && args->hash_from_file == false) { cipher_key = (uint64_t*)keyHash(args->password, args->pw_length, args->state_size); } //hash user entered password from file else if (args->hash == true && args->hash_from_file == true) { printf("Hashing key from file... "); cipher_key = hashKeyFromFile(args->key_file, args->state_size); printf("Done\n"); } if (cipher_key == NULL) { return false; } //failure //Generate IV if we are encrypting if (args->encrypt == true) { if (genIV(args) == false) { return false; } //can't continue without an IV } else //and get it from the first part of the file if we are decrypting { if (getIV(args) == false) { return false; } } //stretch the key with scrypt if (args->hash == true && args->legacy_hash == false) { printf("Stretching key this may take a bit... "); if (kdf_scrypt((uint8_t*)cipher_key, (size_t)(args->state_size/8), (uint8_t*)args->iv, (size_t)(args->state_size/8), SCRYPT_N, SCRYPT_R, SCRYPT_P, (uint8_t*)cipher_key, (size_t)(args->state_size/8) ) != 0) { return false; } //scrypt failed printf("Done\n"); } //generate the mac key from the cipher_key mac_key = (uint64_t*)keyHash((uint8_t*)cipher_key, block_byte_size, args->state_size); //initialize the key structure for the cipher key threefishSetKey(cipher_context, (ThreefishSize_t)args->state_size, cipher_key, threefizer_tweak); //initialize the mac context and undelying skein structures InitMacCtx(args, mac_context, mac_key); //free allocated resources if (cipher_key != NULL) { free(cipher_key); } if (mac_key != NULL) { free(mac_key); } return true; }
size_t DiskMultiMap::hashKey(const std::string str){ hash<std::string> keyHash; return keyHash(str); }