Example #1
0
/*
*********************************************************************************************
	描述:解密函数,可以对密文进行解密。
	输入:	
		pData	要密文数据的首地址。
		d_len	数据的长度
		pPwd	密码
		p_len	密码长度
	输出:
		字符类型	解密后的数据首地址,等于data
*********************************************************************************************
*/
char* CEncrypt::Right ( char *pData , int d_len , const char *pPwd , int p_len )
{
	if ( pData && pPwd && d_len > 0 && p_len > 0 ){
		
		char *pKey = new char[p_len];

		if ( pKey ){

			memcpy( pKey, pPwd, p_len );	// 因为pPwd是不可写的,所以要申请一个pKey代替pPwd进行计算

			GenKey( pKey, p_len );	// 将密码转换成钥匙
			
			CVirtualArray vArray( pData, d_len, pKey, p_len );	// 将pData与pKey用vArray连接成一个虚拟数组

			for ( int j = p_len ; j > 0 ; j -- ){	// 解密算法...
				for ( int i = d_len + p_len - 1 ; ( i - j ) >= 0 ; i -- ){
					vArray [i-j] ^= vArray [i] ;
				}
			}

			memset( pKey , 0 , p_len );	// 销毁钥匙
			delete [] pKey;
		}
	}
	return	pData ;
}
Example #2
0
void Widget::on_encryptFile_clicked()
{
    HCRYPTPROV cryptProv;
    HCRYPTKEY hKey;
    QByteArray key;
    QFile keyV("key.dat");
    DWORD keyBlobLen = 0;
    QDataStream writeToFileFrom(&keyV);
    if (keyV.exists())
        keyV.remove();
    keyV.open(QIODevice::WriteOnly);

    if (!CryptAcquireContext(&cryptProv, NULL, MS_DEF_RSA_SCHANNEL_PROV, PROV_RSA_SCHANNEL, CRYPT_VERIFYCONTEXT))
    {
        QMessageBox::critical(0, "Ошибка", "Получение контекста криптопровайдера. " + GetErrorString(GetLastError()), QMessageBox::Ok);
        return;
    }
    hKey = GenKey(cryptProv, ui->keyVal->text(), &key, keyBlobLen, ui->randKeyVal->isChecked());
    writeToFileFrom << (quint32)keyBlobLen;
    keyV.write(key);
    keyV.close();
    key.clear();
    EncodingFile(fName, hKey);
    QMessageBox::information(0, "Процесс завершён", "Файл успешно зашифрован", QMessageBox::Ok);
}
TEST(MergedSSTableReader, Lookup) {
    SSTableWriteOption option;
    std::string prefix = "/tmp/test_merged_look_up";
    option.set_path(prefix);
    option.set_compress_type(CompressType_kSnappy);
    option.set_sharding_policy("FingerprintSharding");

    const int kNumShard = 5;
    FingerprintSharding sharding;
    sharding.SetShardingNumber(kNumShard);
    ShardingSSTableWriter builder(kNumShard, option);
    for (int i = 0; i < kTestNum; ++i) {
        std::string key = GenKey(i, kMaxLength);
        std::string value = GenValue(i, kMaxLength);
        builder.AddOrDie(key, value);
    }
    LOG(INFO)<< "before flush";
    builder.Flush();
    LOG(INFO)<< "after flush";

    MergedSSTableReader merged_sstable;
    std::vector<std::string> files;
    for (int i = 1; i < kNumShard; ++i) {
        std::string path = ShardingSSTableWriter::GetShardingPath(prefix, i, kNumShard);
        VLOG(2) << path;
        files.push_back(path);
    }

    LOG(INFO)<< "begin to open merged sstable!";
    ASSERT_TRUE(merged_sstable.Open(files, SSTableReader::ON_DISK, false));
    LOG(INFO)<< "open done!";

    for (int i = 0; i < kTestNum; ++i) {
        std::string key = GenKey(i, kMaxLength);
        std::string value = GenValue(i, kMaxLength);
        int idx = sharding.Shard(key);
        std::string value_result;
        if (idx != 0) {
            ASSERT_TRUE(merged_sstable.Lookup(key, &value_result))<< "key: " << key;
            ASSERT_EQ(value, value_result);
        } else {
            ASSERT_FALSE(merged_sstable.Lookup(key, &value_result));
        }
    }
    LOG(INFO)<< "done!";
}
Example #4
0
int CreateKey(char * keyFile, DWORD keyLen) {

strncpy(sDefaultGenKey,keyFile, MAX_LEN);

GenKey(keyFile, keyLen);

return 0;

}
Example #5
0
int Database :: RebuildOneIndex(const uint64_t llInstanceID, const std::string & sFileID)
{
    string sKey = GenKey(llInstanceID);

    leveldb::WriteOptions oLevelDBWriteOptions;
    oLevelDBWriteOptions.sync = false;

    leveldb::Status oStatus = m_poLevelDB->Put(oLevelDBWriteOptions, sKey, sFileID);
    if (!oStatus.ok())
    {
        BP->GetLogStorageBP()->LevelDBPutFail();
        PLG1Err("LevelDB.Put fail, instanceid %lu valuelen %zu", llInstanceID, sFileID.size());
        return -1;
    }

    return 0;
}
Example #6
0
int Database :: Del(const WriteOptions & oWriteOptions, const uint64_t llInstanceID)
{
    if (!m_bHasInit)
    {
        PLG1Err("no init yet");
        return -1;
    }

    string sKey = GenKey(llInstanceID);

    if (OtherUtils::FastRand() % 100 < 1)
    {
        //no need to del vfile every times.
        string sFileID;
        leveldb::Status oStatus = m_poLevelDB->Get(leveldb::ReadOptions(), sKey, &sFileID);
        if (!oStatus.ok())
        {
            if (oStatus.IsNotFound())
            {
                PLG1Debug("LevelDB.Get not found, instanceid %lu", llInstanceID);
                return 0;
            }
            
            PLG1Err("LevelDB.Get fail, instanceid %lu", llInstanceID);
            return -1;
        }

        int ret = m_poValueStore->Del(sFileID, llInstanceID);
        if (ret != 0)
        {
            return ret;
        }
    }

    leveldb::WriteOptions oLevelDBWriteOptions;
    oLevelDBWriteOptions.sync = oWriteOptions.bSync;
    
    leveldb::Status oStatus = m_poLevelDB->Delete(oLevelDBWriteOptions, sKey);
    if (!oStatus.ok())
    {
        PLG1Err("LevelDB.Delete fail, instanceid %lu", llInstanceID);
        return -1;
    }

    return 0;
}
Example #7
0
int Database :: PutToLevelDB(const bool bSync, const uint64_t llInstanceID, const std::string & sValue)
{
    string sKey = GenKey(llInstanceID);

    leveldb::WriteOptions oLevelDBWriteOptions;
    oLevelDBWriteOptions.sync = bSync;

    m_oTimeStat.Point();

    leveldb::Status oStatus = m_poLevelDB->Put(oLevelDBWriteOptions, sKey, sValue);
    if (!oStatus.ok())
    {
        BP->GetLogStorageBP()->LevelDBPutFail();
        PLG1Err("LevelDB.Put fail, instanceid %lu valuelen %zu", llInstanceID, sValue.size());
        return -1;
    }

    BP->GetLogStorageBP()->LevelDBPutOK(m_oTimeStat.Point());

    return 0;
}
Example #8
0
int Database :: GetFromLevelDB(const uint64_t llInstanceID, std::string & sValue)
{
    string sKey = GenKey(llInstanceID);
    
    leveldb::Status oStatus = m_poLevelDB->Get(leveldb::ReadOptions(), sKey, &sValue);
    if (!oStatus.ok())
    {
        if (oStatus.IsNotFound())
        {
            BP->GetLogStorageBP()->LevelDBGetNotExist();
            PLG1Debug("LevelDB.Get not found, instanceid %lu", llInstanceID);
            return 1;
        }
        
        BP->GetLogStorageBP()->LevelDBGetFail();
        PLG1Err("LevelDB.Get fail, instanceid %lu", llInstanceID);
        return -1;
    }

    return 0;
}
Example #9
0
int Database :: GetMaxInstanceIDFileID(std::string & sFileID, uint64_t & llInstanceID)
{
    uint64_t llMaxInstanceID = 0;
    int ret = GetMaxInstanceID(llMaxInstanceID);
    if (ret != 0 && ret != 1)
    {
        return ret;
    }

    if (ret == 1)
    {
        sFileID = "";
        return 0;
    }

    string sKey = GenKey(llMaxInstanceID);
    
    leveldb::Status oStatus = m_poLevelDB->Get(leveldb::ReadOptions(), sKey, &sFileID);
    if (!oStatus.ok())
    {
        if (oStatus.IsNotFound())
        {
            BP->GetLogStorageBP()->LevelDBGetNotExist();
            //PLG1Err("LevelDB.Get not found %s", sKey.c_str());
            return 1;
        }
        
        BP->GetLogStorageBP()->LevelDBGetFail();
        PLG1Err("LevelDB.Get fail");
        return -1;
    }

    llInstanceID = llMaxInstanceID;

    return 0;
}
Example #10
0
TEST(MergedSSTableReader, ReadMultiFiles) {
    SSTableWriteOption option;
    std::string path = "/tmp/test_single_lzo_disk.sstable";
    option.set_path(path);
    option.set_compress_type(CompressType_kSnappy);
    SingleSSTableWriter builder(option);

    SSTableWriteOption option1;
    std::string path1 = "/tmp/test_unsorted_lzo_disk.sstable";
    option1.set_path(path1);
    option1.set_compress_type(CompressType_kSnappy);
    UnsortedSSTableWriter builder1(option1);

    LOG(INFO)<< "Start build sstable :" << path;
    LOG(INFO)<< "Start build sstable :" << path1;
    for (int i = 0; i < kTestNum; ++i) {
        std::string key = GenKey(i, kMaxLength);
        std::string value = GenValue(i, kMaxLength);
        builder.Add(key, value);
        builder1.Add(key, value);
        if (i % 1000 == 0) {
            builder.AddMetaData(key, value);
            builder1.AddMetaData(key, value);
        }
    }
    LOG(INFO)<< "Start flush ...";
    EXPECT_TRUE(builder.Flush());
    EXPECT_TRUE(builder1.Flush());
    LOG(INFO)<< "Finish flush!";

    // open a sstable
    std::vector<std::string> paths;
    paths.push_back(path);
    paths.push_back(path1);

    MergedSSTableReader sstable;
    if (!sstable.Open(paths, SSTableReader::ON_DISK, true)) {
        LOG(INFO)<< "error open sstable!";
    }
    EXPECT_EQ(sstable.EntryCount(), 2 * kTestNum);
    toft::scoped_ptr<SSTableReader::Iterator> iter(sstable.NewIterator());
    LOG(INFO)<< "start iteration";
    for (int i = 0; i < kTestNum; ++i) {
        std::string key = GenKey(i, kMaxLength);
        std::string value = GenValue(i, kMaxLength);
        iter.reset(sstable.Seek(key));
        EXPECT_TRUE(iter->Valid()) << i;
        EXPECT_EQ(key, iter->key()) << i;
        EXPECT_EQ(key + "_value", iter->value()) << i;
        iter->Next();
        EXPECT_TRUE(iter->Valid()) << i;
        EXPECT_EQ(key, iter->key()) << i;
        EXPECT_EQ(key + "_value", iter->value()) << i;
        iter->Next();
    }
    ASSERT_FALSE(iter->Valid());
    LOG(INFO)<< "finish iteration";

    LOG(INFO)<< "start meta iteration";
    toft::Closure<bool(const std::string &, const std::string &)> *callback =  // NOLINT
                    toft::NewPermanentClosure(TestMetaData);
    sstable.IterateMetaData(callback);
    delete callback;
    LOG(INFO)<< "finish meta iteration";
}