Beispiel #1
0
void testFactory(vector<char> data, seconds duration, seconds maxTimeToTest, int id)
{
	Puzzle p;
	HardwareSpeedTester hst(1000, maxTimeToTest);
	TimeCapsuleFactory<char> tcf(hst);
	//char rawdata[] = "Hey, I hope it will finish in time.. tho probably not :D ...";
	auto capsule = tcf.createTimeCapsule(p, data, duration);
	capsule.save("capsule_" + to_string(id) + ".dat");

	Capsule<char> capsule2;
	capsule2.load("capsule_" + to_string(id) + ".dat");

	auto crdata = capsule.getCryptedData();
	auto crdata2 = capsule2.getCryptedData();

	assert(capsule.getBase() == capsule2.getBase());
	assert(capsule.getN() == capsule2.getN());
	assert(capsule.getIV() == capsule2.getIV());
	assert(capsule.getCryptedKey() == capsule2.getCryptedKey());
	assert(capsule.getNumberOfOperations() == capsule2.getNumberOfOperations());
	Logger::log("Original crypted data size: " + to_string(crdata.size()));
	Logger::log("Copypasted crypted data size: " + to_string(crdata2.size()));

	string datastr(crdata.begin(), crdata.end());
	string datastr2(crdata2.begin(), crdata2.end());
	Logger::log("Original   crypted data: " + datastr);
	Logger::log("Copypasted crypted data: " + datastr2);

	//cout << capsule.getCryptedData().size() << ", " << capsule2.getCryptedData().size() << endl;
	//assert(capsule.getCryptedData().size() == capsule2.getCryptedData().size());
	//for (size_t i = 0; i < capsule.getCryptedData().size(); ++i)
	//	assert(capsule.getCryptedData()[i] == capsule2.getCryptedData()[i]);

	Puzzle p2(capsule2.getBase());

	auto start = chrono::high_resolution_clock::now();
	auto key = p2.solve(capsule2.getCryptedKey(), capsule2.getNumberOfOperations(), capsule2.getN());
	auto end = chrono::high_resolution_clock::now();

	Logger::log("Time specified to decode: " + to_string(duration.count()) + " seconds");
	Logger::log("Time taken to decode: " + to_string(chrono::duration_cast<seconds>(end - start).count()) + " seconds");
	
	Encryptor<char> cr;
	auto databack = cr.decrypt(capsule2.getCryptedData(), key, capsule2.getIV());
	string tmpdata(data.begin(), data.end());
	string tmpdataback(databack.begin(), databack.end());
	Logger::log("Original  data: " + tmpdata);
	Logger::log("Data decrypted: " + tmpdataback);

	/*char dummychar;
	if (crdata.size() != crdata2.size())
	{
		Logger::log("PARA VAN!");
		cin >> dummychar;
	}*/
}
Beispiel #2
0
/*
 * Look at all the records and parse keys for addresses and private keys
 */
bool WalletUtilityDB::parseKeys(bool dumppriv, std::string masterPass)
{
    DBErrors result = DB_LOAD_OK;
    std::string strType;
    bool first = true;

    try {
        Dbc* pcursor = GetCursor();
        if (!pcursor)
        {
            LogPrintf("Error getting wallet database cursor\n");
            result = DB_CORRUPT;
        }

        if (dumppriv)
        {
            while (result == DB_LOAD_OK && true)
            {
                CDataStream ssKey(SER_DISK, CLIENT_VERSION);
                CDataStream ssValue(SER_DISK, CLIENT_VERSION);
                int result = ReadAtCursor(pcursor, ssKey, ssValue);

                if (result == DB_NOTFOUND) {
                    break;
                }
                else if (result != 0)
                {
                    LogPrintf("Error reading next record from wallet database\n");
                    result = DB_CORRUPT;
                    break;
                }

                ssKey >> strType;
                if (strType == "mkey")
                {
                    updateMasterKeys(ssKey, ssValue);
                }
            }
            pcursor->close();
            pcursor = GetCursor();
        }

        while (result == DB_LOAD_OK && true)
        {
            CDataStream ssKey(SER_DISK, CLIENT_VERSION);
            CDataStream ssValue(SER_DISK, CLIENT_VERSION);
            int ret = ReadAtCursor(pcursor, ssKey, ssValue);

            if (ret == DB_NOTFOUND)
            {
                std::cout << " ]" << std::endl;
                first = true;
                break;
            }
            else if (ret != DB_LOAD_OK)
            {
                LogPrintf("Error reading next record from wallet database\n");
                result = DB_CORRUPT;
                break;
            }

            ssKey >> strType;

            if (strType == "key" || strType == "ckey")
            {
                std::string strAddr = getAddress(ssKey);
                std::string strKey = "";


                if (dumppriv && strType == "key")
                    strKey = getKey(ssKey, ssValue);
                if (dumppriv && strType == "ckey")
                {
                    if (masterPass == "")
                    {
                        std::cout << "Encrypted wallet, please provide a password. See help below" << std::endl;
                        show_help();
                        result = DB_LOAD_FAIL;
                        break;
                    }
                    strKey = getCryptedKey(ssKey, ssValue, masterPass);
                }

                if (strAddr != "")
                {
                    if (first)
                        std::cout << "[ ";
                    else
                        std::cout << ", ";
                }

                if (dumppriv)
                {
                    std::cout << "{\"addr\" : \"" + strAddr + "\", "
                        << "\"pkey\" : \"" + strKey + "\"}"
                        << std::flush;
                }
                else
                {
                    std::cout << "\"" + strAddr + "\"";
                }

                first = false;
            }
        }

        pcursor->close();
    } catch (DbException &e) {
        std::cout << "DBException caught " << e.get_errno() << std::endl;
    } catch (std::exception &e) {
        std::cout << "Exception caught " << std::endl;
    }

    if (result == DB_LOAD_OK)
        return true;
    else
        return false;
}