示例#1
0
string Bucket::toString(bool isRecordDataInt)
{
	string byteString("(td:");
	byteString += Utility::intToString(dispersionNumber) + ")";
	byteString += "\n";
	ListIterator<Record*> it= RecordList.getIterator();
	while(it.hasNext()){
		Record* record = it.next();
		byteString += "\t\t";
		byteString += record->getKey()->toString();
		byteString += " | ";

		if (isRecordDataInt == true)
		{
			byteString += Utility::intToString(record->getData()->readAsInt(sizeof(int)*1));
			byteString += " | ";
			byteString += Utility::intToString(record->getData()->readAsInt(sizeof(int)*0));
		}
		else
			byteString += record->getData()->toString();

		byteString += "\n";
	}
	return byteString;
}
示例#2
0
//
// Generate MD5 hash from the given string
//
std::wstring FlickrUploader::CalculateMD5Hash(const std::wstring& buffer)
{
#define NT_SUCCESS(Status)          (((NTSTATUS)(Status)) >= 0)
#define STATUS_UNSUCCESSFUL         ((NTSTATUS)0xC0000001L)

    // Convert wstring to string
    std::string byteString(buffer.begin(), buffer.end());

    // Open an algorithm handle
    BCRYPT_ALG_HANDLE algorithm = nullptr;
    NTSTATUS status = BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_MD5_ALGORITHM, nullptr, 0);

    // Calculate the size of the buffer to hold the hash object
    unsigned long dataSize = 0;
    unsigned long hashObjectSize = 0;
    if (NT_SUCCESS(status))
    {
        status = BCryptGetProperty(algorithm, BCRYPT_OBJECT_LENGTH, (unsigned char*)&hashObjectSize, sizeof(unsigned long), &dataSize, 0);
    }
    // Allocate the hash object on the heap
    unsigned char* hashObject = nullptr;
    if (NT_SUCCESS(status))
    {
        hashObject = (unsigned char*) HeapAlloc(GetProcessHeap (), 0, hashObjectSize);
        if (nullptr == hashObject)
        {
            status = STATUS_UNSUCCESSFUL;
        }
    }
    // Calculate the length of the hash
    unsigned long  hashSize = 0;
    if (NT_SUCCESS(status))
    {
        status = BCryptGetProperty(algorithm, BCRYPT_HASH_LENGTH, (unsigned char*)&hashSize, sizeof(unsigned long), &dataSize, 0);
    }
    // Allocate the hash buffer on the heap
    unsigned char* hash = nullptr;
    if (NT_SUCCESS(status))
    {
        hash = (unsigned char*)HeapAlloc (GetProcessHeap(), 0, hashSize);

        if (nullptr == hash)
        {
            status = STATUS_UNSUCCESSFUL;
        }
    }
    // Create a hash
    BCRYPT_HASH_HANDLE cryptHash = nullptr;
    if (NT_SUCCESS(status))
    {
        status = BCryptCreateHash(algorithm, &cryptHash, hashObject, hashObjectSize, nullptr, 0, 0);
    }
    // Hash data
    if (NT_SUCCESS(status))
    {
        status = BCryptHashData(cryptHash, (unsigned char*)byteString.c_str(), static_cast<unsigned long>(byteString.length()), 0);
    }
    // Close the hash and get hash data
    if (NT_SUCCESS(status))
    {
        status = BCryptFinishHash(cryptHash, hash, hashSize, 0);
    }

    std::wstring resultString;
    // If no issues, then copy the bytes to the output string 
    if (NT_SUCCESS(status))
    {
        std::wostringstream hexString;
        for (unsigned short i = 0; i < hashSize; i++)
        {
            hexString << std::setfill(L'0') << std::setw(2) << std::hex << hash[i];
        }

        resultString =  hexString.str();
    }

    // Cleanup
    if(algorithm)
    {
        BCryptCloseAlgorithmProvider(algorithm, 0);
    }

    if (cryptHash)
    {
        BCryptDestroyHash(cryptHash);
    }

    if(hashObject)
    {
        HeapFree(GetProcessHeap(), 0, hashObject);
    }

    if(hash)
    {
        HeapFree(GetProcessHeap(), 0, hash);
    }
    return resultString;
}