bool CryptoECDSA::VerifyData(SecureBinaryData const & binMessage, 
                             SecureBinaryData const & binSignature,
                             BTC_PUBKEY const & cppPubKey)
                            
{


   static CryptoPP::SHA256  sha256;
   static CryptoPP::AutoSeededRandomPool prng;

   assert(cppPubKey.Validate(prng, 3));

   // We execute the first SHA256 op, here.  Next one is done by Verifier
   SecureBinaryData hashVal(32);
   sha256.CalculateDigest(hashVal.getPtr(), 
                          binMessage.getPtr(), 
                          binMessage.getSize());

   // Verifying message 
   BTC_VERIFIER verifier(cppPubKey); 
   return verifier.VerifyMessage((const byte*)hashVal.getPtr(), 
                                              hashVal.getSize(),
                                 (const byte*)binSignature.getPtr(), 
                                              binSignature.getSize());
}
Beispiel #2
0
void Secp256k1PP::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytes& io_cipher)
{
	// interop w/go ecies implementation
	auto r = KeyPair::create();
	Secret z;
	ecdh::agree(r.sec(), _k, z);
	auto key = eciesKDF(z, bytes(), 32);
	bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
	bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
	CryptoPP::SHA256 ctx;
	ctx.Update(mKeyMaterial.data(), mKeyMaterial.size());
	bytes mKey(32);
	ctx.Final(mKey.data());
	
	bytes cipherText = encryptSymNoAuth(SecureFixedHash<16>(eKey), h128(), bytesConstRef(&io_cipher));
	if (cipherText.empty())
		return;

	bytes msg(1 + Public::size + h128::size + cipherText.size() + 32);
	msg[0] = 0x04;
	r.pub().ref().copyTo(bytesRef(&msg).cropped(1, Public::size));
	bytesRef msgCipherRef = bytesRef(&msg).cropped(1 + Public::size + h128::size, cipherText.size());
	bytesConstRef(&cipherText).copyTo(msgCipherRef);
	
	// tag message
	CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
	bytesConstRef cipherWithIV = bytesRef(&msg).cropped(1 + Public::size, h128::size + cipherText.size());
	hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
	hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
	hmacctx.Final(msg.data() + 1 + Public::size + cipherWithIV.size());
	
	io_cipher.resize(msg.size());
	io_cipher.swap(msg);
}
Beispiel #3
0
int main(int argv, char** argc)
{
    std::fstream fs1(argc[1], std::ios::in | std::ios::binary);

    CryptoPP::SHA256 hash;

    if (argv > 1){
        if (fs1.is_open())
        {

            size_t block_size = 0;
            byte digest[CryptoPP::SHA256::DIGESTSIZE];
            fs1.seekg(0, fs1.end);
            int digest_size = 0;
            int pos = fs1.tellg();
            while(pos > 0){

                block_size = (pos - 1) % BLOCK_SIZE + 1;
                pos -= block_size;
                fs1.seekg(pos);

                byte buf[block_size + digest_size];\
                fs1.read(reinterpret_cast<char*>(buf), block_size);

                for (int i = 0; i <  digest_size; i++)
                    buf[block_size + i] = digest[i];

                hash.CalculateDigest(digest, buf, block_size+digest_size);

                digest_size = CryptoPP::SHA256::DIGESTSIZE;
            }

            for (int i = 0; i < CryptoPP::SHA256::DIGESTSIZE; i++)
                std::cout << std::hex << (int)digest[i] << std::dec;
            std::cout << std::endl;
        }
        else
        {
            std::cout << "cannot open file " << argc[1];
        }
    }else{
        byte digest[CryptoPP::SHA256::DIGESTSIZE];
        byte test[2] = {1, 1};
        hash.CalculateDigest(digest, test, 2);
        for (int i = 0; i < CryptoPP::SHA256::DIGESTSIZE; i++)
            std::cout << std::hex << (int)digest[i] << std::dec;
    }
    std::cout << std::endl;
    return 0;
}
bool Secp256k1PP::decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytes& io_text)
{

	// interop w/go ecies implementation
	
	// io_cipher[0] must be 2, 3, or 4, else invalidpublickey
	if (io_text.empty() || io_text[0] < 2 || io_text[0] > 4)
		// invalid message: publickey
		return false;
	
	if (io_text.size() < (1 + Public::size + h128::size + 1 + h256::size))
		// invalid message: length
		return false;

	Secret z;
	if (!ecdh::agree(_k, *(Public*)(io_text.data() + 1), z))
		return false;  // Invalid pubkey or seckey.
	auto key = ecies::kdf(z, bytes(), 64);
	bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
	bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
	bytes mKey(32);
	CryptoPP::SHA256 ctx;
	ctx.Update(mKeyMaterial.data(), mKeyMaterial.size());
	ctx.Final(mKey.data());
	
	bytes plain;
	size_t cipherLen = io_text.size() - 1 - Public::size - h128::size - h256::size;
	bytesConstRef cipherWithIV(io_text.data() + 1 + Public::size, h128::size + cipherLen);
	bytesConstRef cipherIV = cipherWithIV.cropped(0, h128::size);
	bytesConstRef cipherNoIV = cipherWithIV.cropped(h128::size, cipherLen);
	bytesConstRef msgMac(cipherNoIV.data() + cipherLen, h256::size);
	h128 iv(cipherIV.toBytes());
	
	// verify tag
	CryptoPP::HMAC<CryptoPP::SHA256> hmacctx(mKey.data(), mKey.size());
	hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
	hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
	h256 mac;
	hmacctx.Final(mac.data());
	for (unsigned i = 0; i < h256::size; i++)
		if (mac[i] != msgMac[i])
			return false;
	
	plain = decryptSymNoAuth(SecureFixedHash<16>(eKey), iv, cipherNoIV).makeInsecure();
	io_text.resize(plain.size());
	io_text.swap(plain);
	
	return true;
}
Beispiel #5
0
bytes Secp256k1PP::eciesKDF(Secret const& _z, bytes _s1, unsigned kdByteLen)
{
	auto reps = ((kdByteLen + 7) * 8) / (CryptoPP::SHA256::BLOCKSIZE * 8);
	// SEC/ISO/Shoup specify counter size SHOULD be equivalent
	// to size of hash output, however, it also notes that
	// the 4 bytes is okay. NIST specifies 4 bytes.
	bytes ctr({0, 0, 0, 1});
	bytes k;
	CryptoPP::SHA256 ctx;
	for (unsigned i = 0; i <= reps; i++)
	{
		ctx.Update(ctr.data(), ctr.size());
		ctx.Update(_z.data(), Secret::size);
		ctx.Update(_s1.data(), _s1.size());
		// append hash to k
		bytes digest(32);
		ctx.Final(digest.data());
		ctx.Restart();
		
		k.reserve(k.size() + h256::size);
		move(digest.begin(), digest.end(), back_inserter(k));
		
		if (++ctr[3] || ++ctr[2] || ++ctr[1] || ++ctr[0])
			continue;
	}
	
	k.resize(kdByteLen);
	return k;
}
Beispiel #6
0
TEST(AccountTest, TestCreateAccount)
{
    if (settings::instance().cassandraSeeds.size() == 0) return;
    
    const char* const USERNAME = "******";
    const char* const PASSWORD = "******";
    
    user_account::create(USERNAME, PASSWORD, 1);
    
    
    unsigned char hashResult[CryptoPP::SHA256::DIGESTSIZE];
    CryptoPP::SHA256 sha;
    sha.CalculateDigest(&hashResult[0], (unsigned char*)USERNAME, strlen(USERNAME));
    
    std::string nameHash = util::hex_encode(hashResult, CryptoPP::SHA256::DIGESTSIZE);
    
    std::mutex m;
    std::condition_variable cv;
    
    user_account foundAcct;
    bool returned = false;
    bool ufound = false;
    
    user_account::find(nameHash, [&] (bool found, user_account acct) {
        std::lock_guard<std::mutex> lk(m);
        ufound = found;
        returned = true;
        foundAcct = acct;
        cv.notify_one();
    });
    
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, [&] {return returned == true;});
    
    ASSERT_EQ(true, ufound);
    ASSERT_EQ(1, foundAcct.user_level());
    ASSERT_EQ(USERNAME, foundAcct.username());
}
SecureBinaryData CryptoECDSA::SignData(SecureBinaryData const & binToSign, 
                                       BTC_PRIVKEY const & cppPrivKey)
{

   // We trick the Crypto++ ECDSA module by passing it a single-hashed
   // message, it will do the second hash before it signs it.  This is 
   // exactly what we need.
   static CryptoPP::SHA256  sha256;
   static BTC_PRNG prng;

   // Execute the first sha256 op -- the signer will do the other one
   SecureBinaryData hashVal(32);
   sha256.CalculateDigest(hashVal.getPtr(), 
                          binToSign.getPtr(), 
                          binToSign.getSize());

   string signature;
   BTC_SIGNER signer(cppPrivKey);
   CryptoPP::StringSource(
               hashVal.toBinStr(), true, new CryptoPP::SignerFilter(
               prng, signer, new CryptoPP::StringSink(signature))); 
  
   return SecureBinaryData(signature);
}
Beispiel #8
0
    void CEncrypt::CreateUserKey()
	{
        CryptoPP::RandomPool prng;

        CryptoPP::SecByteBlock salt(16);
        CryptoPP::OS_GenerateRandomBlock(false, salt, salt.size());
        prng.IncorporateEntropy(salt, salt.size());

		memcpy(m_anUserKey + 32, salt.data(), salt.size());

        CryptoPP::SHA256 hash;

        hash.Update( (unsigned char*) impl->m_sUserPassword.c_str(), impl->m_sUserPassword.length());
        hash.Update( m_anUserKey + 32, 8);

        CryptoPP::SecByteBlock pHashData(hash.DigestSize());
        hash.Final(pHashData);

        if (MakeFileKey3(impl->m_sUserPassword, pHashData.data(), pHashData.size()))
        {
            memcpy(m_anUserKey, pHashData.data(), pHashData.size());

            hash.Update( (unsigned char*) impl->m_sUserPassword.c_str(), impl->m_sUserPassword.length());
            hash.Update( m_anUserKey + 40, 8);

            CryptoPP::SecByteBlock pHashKeyData(hash.DigestSize());
            hash.Final(pHashKeyData);

            MakeFileKey3(impl->m_sUserPassword, pHashKeyData.data(), pHashKeyData.size());
            unsigned char empty[16] = {};

            CryptoPP::AES::Encryption aesEncryption(pHashKeyData.data(), pHashKeyData.size());
            CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, empty);

            CryptoPP::StreamTransformationFilter stfEncryption(cbcEncryption, new CryptoPP::ArraySink( m_anUserEncryptKey, 32), CryptoPP::StreamTransformationFilter::NO_PADDING );
            stfEncryption.Put2(impl->m_anEncryptionKey, 32, 1, true);
            stfEncryption.MessageEnd();
        }
	}
Beispiel #9
0
TEST(AccountTest, TestAccountAuthorized)
{
    if (settings::instance().cassandraSeeds.size() == 0) return;
    
    const char* const USERNAME = "******";
    const char* const PASSWORD = "******";
    
    user_account::create(USERNAME, PASSWORD, 1);
    
    
    unsigned char hashResult[CryptoPP::SHA256::DIGESTSIZE];
    CryptoPP::SHA256 sha;
    sha.CalculateDigest(&hashResult[0], (unsigned char*)USERNAME, strlen(USERNAME));
    
    std::string nameHash = util::hex_encode(hashResult, CryptoPP::SHA256::DIGESTSIZE);
    
    sha.Restart();
    sha.CalculateDigest(&hashResult[0], (unsigned char*)PASSWORD, strlen(PASSWORD));
    
    std::string pwHash = util::hex_encode(hashResult, CryptoPP::SHA256::DIGESTSIZE);
    
    std::mutex m;
    std::condition_variable cv;
    
    auto challengeBytes = util::random_bytes(1024);
    auto pwHashAndChallenge = pwHash + challengeBytes;
    
    sha.Restart();
    sha.CalculateDigest(&hashResult[0], (unsigned char*)pwHashAndChallenge.c_str(), pwHashAndChallenge.length());
    
    auto challengeResponse = util::hex_encode(hashResult, CryptoPP::SHA256::DIGESTSIZE);
    
    bool uauthd = false;
    bool returned = false;
    
    user_account::is_authorized(nameHash, challengeBytes, challengeResponse, [&] (bool authd) {
        std::lock_guard<std::mutex> lk(m);
        returned = true;
        uauthd = authd;
        cv.notify_one();
    });
    
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, [&] {return returned == true;});
    
    ASSERT_EQ(true, uauthd);
}
Beispiel #10
0
void CPropChkSum::OnOK() 
{
	UpdateData();

	CPropertyPage::OnOK();
	if(m_szFileName == _T("")) {
		AfxMessageBox(_T("Please select the file."));
		return ;
	}

	HANDLE hFile = CreateFile(m_szFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
	if (hFile == INVALID_HANDLE_VALUE){
		CString str = m_szFileName + _T(" doesn't exist!");
		AfxMessageBox(str);
		return ;
	}

	DWORD dwFileSize = GetFileSize(hFile, NULL);
	HANDLE hFileMap = CreateFileMapping(
						hFile, // 如果这值为INVALID_HANDLE_VALUE,是合法的,上步一定测试啊
						NULL,    // 默认安全性
						PAGE_READWRITE,    // 可读
						0, // 2个32位数示1个64位数,最大文件字节数,
						 // 高字节,文件大小小于4G时,高字节永远为0
						0,//dwFileSize, // 此为低字节,也就是最主要的参数,如果为0,取文件真实大小
						NULL);
	if (hFileMap == NULL)
	{
		CString str = _T("Mapping ") + m_szFileName + _T(" failed!");
		AfxMessageBox(str);
		CloseHandle(hFile);
		return ;
	}

	PVOID pvFile = MapViewOfFile( //pvFile就是得到的指针,用它来直接操作文件
						hFileMap, 
						FILE_MAP_WRITE,     // 可读
						0,      // 文件指针头位置 高字节
						0, // 文件指针头位置 低字节 必为分配粒度的整倍数,windows的粒度为64K
						0);    // 要映射的文件尾,如果为0,则从指针头到真实文件尾
	if (pvFile == NULL)	{
		CString str = _T("MapViewOfFile ") + m_szFileName + _T(" failed, ERROR:!");
		str.Format(_T("MapViewOfFile %s failed, ERROR:%d!"), m_szFileName, GetLastError());
		AfxMessageBox(str);
		CloseHandle(hFileMap);
		CloseHandle(hFile);
		return ;
	}
	// 步骤4: 像操作内存一样操作文件,演示功能把整个文件倒序
	char *pData = (char*)pvFile;

	boost::crc_16_type crc16t;
	crc16t.process_bytes(pData, dwFileSize);
	unsigned short crc16 = crc16t.checksum();
	m_strCRC16.Format(_T("%04X"), crc16);

	boost::crc_ccitt_type crc_ccitt;
	crc_ccitt.process_bytes(pData, dwFileSize);
	crc16 = crc_ccitt.checksum();
	m_strCRC_CCITT.Format(_T("%04X"), crc16);

	boost::crc_xmodem_type  crc_xmdm;
	crc_xmdm.process_bytes(pData, dwFileSize);
	crc16 = crc_xmdm.checksum();
	m_strCRC32C.Format(_T("%04X"), crc16);

	boost::crc_32_type crc32t;
	crc32t.process_bytes(pData, dwFileSize);
	unsigned long crc32 = crc32t.checksum();
	m_strCRC32.Format(_T("%04X"), crc32);

	enum {
		MessageDigest_MD5 = 0,
		MessageDigest_SHA,
		MessageDigest_SHA1,
		MessageDigest_SHA224,
		MessageDigest_SHA256,
		MessageDigest_SHA384,
		MessageDigest_SHA3_224,
		MessageDigest_SHA3_256,
		MessageDigest_SHA3_384,
		MessageDigest_SHA3_512
	};
	
	CString strMessageDigest;
	unsigned char sha_md[120] = {0};
	int index = 0;

	if(m_mdList.GetCheck(MessageDigest_MD5)) {
		CryptoPP::Weak1::MD5 md5;
		md5.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15]
		);
		
		m_mdList.SetItemText(MessageDigest_MD5, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA)) {
		CryptoPP::SHA sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 
			sha_md[16], sha_md[17], sha_md[18], sha_md[19]
			);
		m_mdList.SetItemText(MessageDigest_SHA, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA1)) {
		CryptoPP::SHA1 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA1: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 
			sha_md[16], sha_md[17], sha_md[18], sha_md[19]
			);
		m_mdList.SetItemText(MessageDigest_SHA1, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA224)) {
		CryptoPP::SHA224 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA224: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 
			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27]
			);
		m_mdList.SetItemText(MessageDigest_SHA224, 0, strMessageDigest);
	}
	
	if(m_mdList.GetCheck(MessageDigest_SHA256)) {
		CryptoPP::SHA256 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA256: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 
			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27],sha_md[28], sha_md[29], sha_md[30], sha_md[31]
			);
		m_mdList.SetItemText(MessageDigest_SHA256, 0, strMessageDigest);
	}
	
	if(m_mdList.GetCheck(MessageDigest_SHA384)) {
		CryptoPP::SHA384 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA384: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 

			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27],sha_md[28], sha_md[29], sha_md[30], sha_md[31],
			
			sha_md[32], sha_md[33], sha_md[34], sha_md[35], sha_md[36], sha_md[37], sha_md[38], sha_md[39],
			sha_md[40], sha_md[41], sha_md[42], sha_md[43],sha_md[44], sha_md[45], sha_md[46], sha_md[47]
			);
		m_mdList.SetItemText(MessageDigest_SHA384, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA3_224)) {
		CryptoPP::SHA3_224 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA3-224: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 

			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27]
			);
		m_mdList.SetItemText(MessageDigest_SHA3_224, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA3_256)) {
		CryptoPP::SHA3_256 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA3-256: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 
			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27],sha_md[28], sha_md[29], sha_md[30], sha_md[31]
			);
		m_mdList.SetItemText(MessageDigest_SHA3_256, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA3_384)) {
		CryptoPP::SHA3_384 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA3-384: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 

			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27],sha_md[28], sha_md[29], sha_md[30], sha_md[31],
			
			sha_md[32], sha_md[33], sha_md[34], sha_md[35], sha_md[36], sha_md[37], sha_md[38], sha_md[39],
			sha_md[40], sha_md[41], sha_md[42], sha_md[43],sha_md[44], sha_md[45], sha_md[46], sha_md[47]
			);
		m_mdList.SetItemText(MessageDigest_SHA3_384, 0, strMessageDigest);
	}

	if(m_mdList.GetCheck(MessageDigest_SHA3_512)) {
		CryptoPP::SHA3_512 sha;
		sha.CalculateDigest((byte*)&sha_md, (unsigned char *)pData, dwFileSize);
		strMessageDigest.Format( 
			"SHA3-512: %02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X"
			"%02X%02X%02X%02X%02X%02X%02X%02X",
			sha_md[0], sha_md[1], sha_md[2], sha_md[3],sha_md[4], sha_md[5], sha_md[6], sha_md[7], 
			sha_md[8], sha_md[9], sha_md[10], sha_md[11],sha_md[12], sha_md[13], sha_md[14], sha_md[15], 

			sha_md[16], sha_md[17], sha_md[18], sha_md[19], sha_md[20], sha_md[21], sha_md[22], sha_md[23],
			sha_md[24], sha_md[25], sha_md[26], sha_md[27],sha_md[28], sha_md[29], sha_md[30], sha_md[31],
			
			sha_md[32], sha_md[33], sha_md[34], sha_md[35], sha_md[36], sha_md[37], sha_md[38], sha_md[39],
			sha_md[40], sha_md[41], sha_md[42], sha_md[43],sha_md[44], sha_md[45], sha_md[46], sha_md[47],
			
			sha_md[48], sha_md[49], sha_md[50], sha_md[51], sha_md[52], sha_md[53], sha_md[54], sha_md[55],
			sha_md[56], sha_md[57], sha_md[58], sha_md[59],sha_md[60], sha_md[61], sha_md[62], sha_md[63]
			);
		m_mdList.SetItemText(MessageDigest_SHA3_512, 0, strMessageDigest);
	}

	
	//self define crc mode.
	if(m_strPoly != _T("") && m_dwDataWidth) {
		char *lptmp = NULL;
		const size_t bits = (size_t)m_dwDataWidth;
		const unsigned long poly = strtoul(m_strPoly, &lptmp, 16);
		const unsigned long init = strtoul(m_strInitValue, &lptmp, 16);
		const unsigned long out  = strtoul(m_strOut, &lptmp, 16);
		const bool inf = m_bInReflect?true:false;
		const bool outf = m_bOutReflect?true:false;
		unsigned long rem = 0;

		if(!((poly == ULONG_MAX && errno == ERANGE) || *lptmp != 0)) {
	//		boost::crc_optimal<bits, poly, init, out, inf, ino> crc_self;
			switch (bits) {
#define CALC_CRC_BITS(nbits)	\
			case nbits:  {		\
				boost::crc_basic<nbits> crc(poly, init, out, inf, outf);\
				crc.process_bytes((unsigned char *)pData, dwFileSize);	\
				rem = crc.checksum();	\
				break;					\
				}
			CALC_CRC_BITS(1);	CALC_CRC_BITS(2);	CALC_CRC_BITS(3);
			CALC_CRC_BITS(4);	CALC_CRC_BITS(5);	CALC_CRC_BITS(6);
			CALC_CRC_BITS(7);	CALC_CRC_BITS(8);	CALC_CRC_BITS(9);
			CALC_CRC_BITS(10);	CALC_CRC_BITS(11);	CALC_CRC_BITS(12);
			CALC_CRC_BITS(13);	CALC_CRC_BITS(14);	CALC_CRC_BITS(15);
			CALC_CRC_BITS(16);	CALC_CRC_BITS(17);	CALC_CRC_BITS(18);
			CALC_CRC_BITS(19);	CALC_CRC_BITS(20);	CALC_CRC_BITS(21);
			CALC_CRC_BITS(22);	CALC_CRC_BITS(23);	CALC_CRC_BITS(24);
			CALC_CRC_BITS(25);	CALC_CRC_BITS(26);	CALC_CRC_BITS(27);
			CALC_CRC_BITS(28);	CALC_CRC_BITS(29);	CALC_CRC_BITS(30);
			CALC_CRC_BITS(31);	CALC_CRC_BITS(32);/*	CALC_CRC_BITS(33);
			CALC_CRC_BITS(34);	CALC_CRC_BITS(35);	CALC_CRC_BITS(36);
			CALC_CRC_BITS(37);	CALC_CRC_BITS(38);	CALC_CRC_BITS(39);
			CALC_CRC_BITS(40);	CALC_CRC_BITS(41);	CALC_CRC_BITS(42);
			CALC_CRC_BITS(43);	CALC_CRC_BITS(44);	CALC_CRC_BITS(45);
			CALC_CRC_BITS(46);	CALC_CRC_BITS(47);	CALC_CRC_BITS(48);
			CALC_CRC_BITS(49);	CALC_CRC_BITS(50);	CALC_CRC_BITS(51);
			CALC_CRC_BITS(52);	CALC_CRC_BITS(53);	CALC_CRC_BITS(54);
			CALC_CRC_BITS(55);	CALC_CRC_BITS(56);	CALC_CRC_BITS(57);
			CALC_CRC_BITS(58);	CALC_CRC_BITS(59);	CALC_CRC_BITS(60);
			CALC_CRC_BITS(61);	CALC_CRC_BITS(62);	CALC_CRC_BITS(63);
			CALC_CRC_BITS(64);	CALC_CRC_BITS(65);	CALC_CRC_BITS(66);
			CALC_CRC_BITS(67);	CALC_CRC_BITS(68);	CALC_CRC_BITS(69);*/
			default: break;
			}

			m_strSelfCRC.Format(_T("%%0%dX"), bits / 8);
			strMessageDigest.Format(m_strSelfCRC, rem);
			m_strSelfCRC = strMessageDigest;
#undef CALC_CRC_BITS
		}
	}

done:
	UpdateData(FALSE);
	UnmapViewOfFile(pvFile);
	CloseHandle(hFileMap);
	CloseHandle(hFile);
	return ;
}
Beispiel #11
0
void sha256(byte* digest, string arg)
{
    CryptoPP::SHA256 hashfun;
    hashfun.CalculateDigest(digest, (byte*) arg.c_str(), arg.length());
}
bool CCryptoModulus::InitCrypto(BYTE *temp, DWORD length)
{
    int hashid = 0;
    BYTE digest[2048];


    memset(digest, 0, sizeof(digest));


    if (length >= 10)
    {
        hashid = temp[4] ^ (temp[3] | (temp[1] ^ temp[0]) < temp[2] % 3);
    }
    else
    {
        hashid = temp[0];
    }


    hashid = (hashid % 11) - 1;


    switch (hashid)
    {
        case 0:
        {
            CryptoPP::SHA256 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[0] % 10;
            this->InitCrypto(this->m_algorithm, digest, 32);
        }
        break;


        case 2:
        {
            CryptoPP::SHA512 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[1] % 10;
            this->InitCrypto(this->m_algorithm, digest, 64);
        }
        break;
        
        case 3:
        {
            CryptoPP::SHA384 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[2] % 10;
            this->InitCrypto(this->m_algorithm, digest, 48);
        }
        break;


        case 4:
        {
            CryptoPP::Weak1::MD4 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[3] % 10;
            this->InitCrypto(this->m_algorithm, digest, 16);
        }
        break;


        case 5:
        {
            CryptoPP::Weak1::MD5 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[4] % 10;
            this->InitCrypto(this->m_algorithm, digest, 16);
        }
        break;


        case 6:
        {
            CryptoPP::RIPEMD160 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[5] % 10;
            this->InitCrypto(this->m_algorithm, digest, 20);
        }
        break;


        case 7:
        {
            CryptoPP::RIPEMD320 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[6] % 10;
            this->InitCrypto(this->m_algorithm, digest, 40);
        }
        break;


        case 8:
        {
            CryptoPP::RIPEMD128 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[7] % 10;
            this->InitCrypto(this->m_algorithm, digest, 16);
        }
        break;


        case 9:
        {
            CryptoPP::RIPEMD256 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[8] % 10;
            this->InitCrypto(this->m_algorithm, digest, 32);
        }
        break;


        default:
        {
            CryptoPP::Weak1::MD5 hash;
            hash.Update(temp, length);
            hash.Final(digest);


            this->m_algorithm = digest[9] % 10;
            this->InitCrypto(this->m_algorithm, digest, 16);
        }
        break;
    }


    return true;
}
Beispiel #13
-1
std::string transformToSHA256(std::string plainText, bool upperCase)
{
	// Crypto++ SHA256 object
	CryptoPP::SHA256 hash;

	// Use native byte instead of casting chars
	byte digest[CryptoPP::SHA256::DIGESTSIZE];

	// Do the actual calculation, require a byte value so we need a cast
	hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());

	// Crypto++ HexEncoder object
	CryptoPP::HexEncoder encoder;

	// Our output
	std::string output;

	// Drop internal hex encoder and use this, returns uppercase by default
	encoder.Attach(new CryptoPP::StringSink(output));
	encoder.Put(digest, sizeof(digest));
	encoder.MessageEnd();

	// Make sure we want uppercase
	if(upperCase)
		return output;

	// Convert to lowercase if needed
	return asLowerCaseString(output);
}