bool SharedStoreStats::snapshot(const char *filename, std::string& keySample) { std::ofstream out(filename); if (out.fail()) { return false; } char nkeySample[MAX_KEY_LEN + 1]; if (keySample != "") { normalizeKey(keySample.c_str(), nkeySample, MAX_KEY_LEN); nkeySample[MAX_KEY_LEN] = '\0'; } ReadLock l(s_rwlock); StatsMap::iterator iter; time_t now = time(nullptr); for (iter = s_detailMap.begin(); iter != s_detailMap.end(); ++iter) { assert(!iter->second->isGroup); if (keySample != "") { char nkey[MAX_KEY_LEN + 1]; normalizeKey(iter->first, nkey, MAX_KEY_LEN); nkey[MAX_KEY_LEN] = '\0'; if (strcmp(nkey, nkeySample) != 0) continue; } if (!iter->second->isValid) continue; out << "{"; writeEntryStr(out, "KeyName", iter->first); writeEntryInt(out, "TotalSize", iter->second->totalSize); writeEntryInt(out, "Prime", (int64)iter->second->isPrime); writeEntryInt(out, "TTL", iter->second->ttl); writeEntryInt(out, "KeySize", iter->second->keySize); writeEntryInt(out, "DataSize", iter->second->var.dataTotalSize); writeEntryInt(out, "StoreCount", iter->second->storeCount); writeEntryInt(out, "SinceLastStore", now - iter->second->lastStoreTime); writeEntryInt(out, "DeleteCount", iter->second->deleteCount); if (iter->second->deleteCount > 0) { writeEntryInt(out, "SinceLastDelete", now - iter->second->lastDeleteTime); } if (RuntimeOption::EnableAPCFetchStats) { writeEntryInt(out, "FetchCount", iter->second->fetchCount); if (iter->second->fetchCount > 0) { writeEntryInt(out, "SinceLastFetch", now - iter->second->lastFetchTime); } } writeEntryInt(out, "VariantCount", iter->second->var.variantCount); writeEntryInt(out, "UserDataSize", iter->second->var.dataSize, true); out << "}\n"; } out.close(); return true; }
inline bool decodeAgile(std::string& decData, const std::string& encryptedPackage, const EncryptionInfo& info, const std::string& pass, std::string& secretKey) { const CipherParam& keyData = info.keyData; const CipherParam& encryptedKey = info.encryptedKey; if (secretKey.empty()) { if (!getAgileSecretKey(secretKey, info, pass)) return false; if (putSecretKeyInstance()) { printf("secretKey = "); ms::dump(secretKey, false); } if (!VerifyIntegrity(encryptedPackage, keyData, secretKey, keyData.saltValue, info.encryptedHmacKey, info.encryptedHmacValue)) { printf("warning : mac err : data may be broken\n"); // return false; } } std::string encData; const uint64_t decodeSize = GetEncodedData(encData, encryptedPackage); // decode normalizeKey(secretKey, encryptedKey.keyBits / 8); DecContent(decData, encData, encryptedKey, secretKey, keyData.saltValue); decData.resize(decodeSize); return true; }
// // Encryption // void Caesar::encrypt() { Crypto::encrypt(); // incrementing encryption level m_encryptionLevel++; m_key = normalizeKey(m_key); /// get the range int len = m_phrase.length(); // // Process each character // for (int idx = 0; idx < len; ++idx) { // // Out of range characters are just transfered to // the ciphertext // if (m_range.in(m_phrase.at(idx))!=true) { //phrase().at(idx) = m_range[idx]; ///src[idx] continue; } // incrementing conversions m_numConversions++; // // Determine the alphabet index of the plaintext character // char c = m_phrase[idx] - m_range.min(); // // Add the key to the alphabet index to get the ciphertext // character. Handle the wrap-around past the last valid // character. // c += m_key % m_range.numCharacters(); c %= m_range.numCharacters(); m_phrase[idx] = c + m_range.min(); } }
void SharedStoreStats::onDelete(const StringData *key, const SharedVariant *var, bool replace, bool noTTL) { char normalizedKey[MAX_KEY_LEN + 1]; if (RuntimeOption::EnableAPCSizeGroup) { normalizeKey(key->data(), normalizedKey, MAX_KEY_LEN); normalizedKey[MAX_KEY_LEN] = '\0'; } SharedValueProfile svpTemp; // Calculate size of the variant svpTemp.calcInd(key, var); ReadLock l(s_rwlock); if (RuntimeOption::EnableAPCSizeDetail && !replace) { StatsMap::const_accessor cacc; if (s_detailMap.find(cacc, (char*)key->data())) { SharedValueProfile *svp = cacc->second; assert(svp->isValid); svp->isValid = false; svp->deleteCount++; svp->lastDeleteTime = time(nullptr); } } if (RuntimeOption::EnableAPCSizeGroup) { StatsMap::const_accessor cacc; if (s_statsMap.find(cacc, normalizedKey)) { SharedValueProfile *group = cacc->second; group->removeFromGroup(&svpTemp); if (noTTL) { group->sizeNoTTL -= svpTemp.totalSize; } } } }
inline bool encode_in( std::string& encryptedPackage, EncryptionInfo& info, const std::string& data, cybozu::crypto::Cipher::Name cipherName, cybozu::crypto::Hash::Name hashName, int spinCount, const std::string& pass, const std::string& masterKey) { if (spinCount > 10000000) throw cybozu::Exception("ms:encode_in:too large spinCount") << spinCount; CipherParam& keyData = info.keyData; CipherParam& encryptedKey = info.encryptedKey; keyData.setByName(cipherName, hashName); encryptedKey.setByName(cipherName, hashName); info.spinCount = spinCount; std::string& iv = encryptedKey.saltValue; FillRand(iv, encryptedKey.saltSize); #ifdef SAME_KEY puts("QQQ defined SAME_KEY QQQ"); iv = fromHex("F4994F9B2DCD5E0E84BC6386D4523D2C"); #endif const std::string pwHash = hashPassword(encryptedKey.hashName, iv, pass, spinCount); const std::string skey1 = generateKey(encryptedKey, pwHash, blkKey_VerifierHashInput); const std::string skey2 = generateKey(encryptedKey, pwHash, blkKey_encryptedVerifierHashValue); const std::string skey3 = generateKey(encryptedKey, pwHash, blkKey_encryptedKeyValue); std::string verifierHashInput; FillRand(verifierHashInput, encryptedKey.saltSize); #ifdef SAME_KEY verifierHashInput = fromHex("FEDAECD950F9E82C47CADA29B7837C6D"); #endif verifierHashInput.resize(RoundUp(verifierHashInput.size(), encryptedKey.blockSize)); info.encryptedVerifierHashInput = cipher(encryptedKey.cipherName, verifierHashInput, skey1, iv, cybozu::crypto::Cipher::Encoding); std::string hashedVerifier = cybozu::crypto::Hash::digest(encryptedKey.hashName, verifierHashInput); hashedVerifier.resize(RoundUp(hashedVerifier.size(), encryptedKey.blockSize)); info.encryptedVerifierHashValue = cipher(encryptedKey.cipherName, hashedVerifier, skey2, iv, cybozu::crypto::Cipher::Encoding); std::string secretKey; FillRand(secretKey, encryptedKey.saltSize); #ifdef SAME_KEY secretKey = fromHex("BF44FBB51BE1E88BF130156E117E7900"); #endif if (!masterKey.empty()) { secretKey = masterKey; } normalizeKey(secretKey, encryptedKey.keyBits / 8); info.encryptedKeyValue = cipher(encryptedKey.cipherName, secretKey, skey3, iv, cybozu::crypto::Cipher::Encoding); FillRand(keyData.saltValue, keyData.saltSize); #ifdef SAME_KEY keyData.saltValue = fromHex("C49AAAEE99004C6B017EE5CD11B86729"); #endif EncContent(encryptedPackage, data, encryptedKey, secretKey, keyData.saltValue); GenerateIntegrityParameter(info.encryptedHmacKey, info.encryptedHmacValue, encryptedPackage, keyData, secretKey, keyData.saltValue); return true; }
void SharedStoreStats::onStore(const StringData *key, const SharedVariant *var, int64 ttl, bool prime) { char normalizedKey[MAX_KEY_LEN + 1]; SharedValueProfile *svpInd; svpInd = new SharedValueProfile(key->data()); svpInd->calcInd(key, var); svpInd->ttl = ttl > 0 && ttl < 48*3600 ? ttl : 48*3600; if (RuntimeOption::EnableAPCSizeGroup) { // Here so that it is out of critical section normalizeKey(key->data(), normalizedKey, MAX_KEY_LEN); normalizedKey[MAX_KEY_LEN] = '\0'; } ReadLock l(s_rwlock); if (RuntimeOption::EnableAPCSizeGroup) { SharedValueProfile *group; StatsMap::const_accessor cacc; StatsMap::accessor acc; if (s_statsMap.find(cacc, normalizedKey)) { group = cacc->second; } else { cacc.release(); group = new SharedValueProfile(normalizedKey); if (s_statsMap.insert(acc, group->key)) { group->isGroup = true; group->keyCount = 0; acc->second = group; } else { // already there delete group; group = acc->second; } } group->addToGroup(svpInd); if (ttl == 0) { group->sizeNoTTL += svpInd->totalSize; } } if (RuntimeOption::EnableAPCSizeDetail) { StatsMap::accessor acc; if (s_detailMap.insert(acc, svpInd->key)) { acc->second = svpInd; if (prime) { svpInd->isPrime = true; } } else { SharedValueProfile *existing = acc->second; // update size but keep other stats existing->totalSize = svpInd->totalSize; existing->keySize = svpInd->keySize; existing->var = svpInd->var; delete svpInd; svpInd = existing; } svpInd->isValid = true; svpInd->storeCount++; svpInd->lastStoreTime = time(nullptr); } else { delete svpInd; } }