Encrytion and Hash

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
banana
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 19, 2009 8:41 pm
Contact:

Encrytion and Hash

Post by banana » Wed Aug 26, 2009 12:51 am

What are you thinking about two new classe for wxWidget? I'm thinking of an Encrytion and Hash Class based on the cryptopp! The Class mustn't have all features implemented of crytopp! For example an class which can encrypt a string or a Stream with only one function! I think this is one of the things which are needed to make programs with userdatabases or something like this because nobody want to save the user passwords in plain text in a db.

Banana

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Fri Sep 04, 2009 10:26 pm

I allready use Crypto++ with my app ok. But I agree, a wxCrypto class would be great.

banana
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 19, 2009 8:41 pm
Contact:

Post by banana » Sat Sep 05, 2009 9:10 pm

Code: Select all

#ifndef WXHASH_H_
#define WXHASH_H_

#include <wx/wx.h>
#include <wx/stream.h>
#include "cryptopp/cryptlib.h"

using namespace CryptoPP;

enum Hash_Names {
	HASH_SHA1,
	HASH_SHA256,
	HASH_SHA512,
	HASH_SHA224,
	HASH_SHA384,
	HASH_MD2,
	HASH_MD4,
	HASH_MD5,
	HASH_TIGER,
	HASH_WHIRLPOOL,
	HASH_RIPEMD160,
	HASH_RIPEMD320,
	HASH_RIPEMD128,
	HASH_RIPEMD256
};

class wxHash {
private:
	HashTransformation *Hash;
	byte *Digest;
	bool CreateHash();
	char bittohex(unsigned char C1, bool RightNumber);

public:
	wxHash(unsigned int HashName = HASH_SHA256);
	virtual ~wxHash();
	bool FromInputStream(wxInputStream *Input, const unsigned int ReadBytes =
			256);
	bool FromString(wxString &Input);
	byte *GetHash() {
		if (this->CreateHash())
			return this->Digest;
		return NULL;
	}
	wxString GetHexStringHash();
};

#endif /* WXHASH_H_ */

Code: Select all

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1

#include <cryptopp/sha.h>
#include <cryptopp/md2.h>
#include <cryptopp/md4.h>
#include <cryptopp/md5.h>
#include <cryptopp/tiger.h>
#include <cryptopp/whrlpool.h>
#include <cryptopp/ripemd.h>

#include "wxHash.h"


wxHash::wxHash(unsigned int HashName) {
	switch (HashName) {
	case HASH_SHA1:
		this->Hash = new SHA1;
		this->Digest = new byte[CryptoPP::SHA1::DIGESTSIZE];
		break;
	case HASH_SHA256:
		this->Hash = new SHA256;
		this->Digest = new byte[CryptoPP::SHA256::DIGESTSIZE];
		break;
	case HASH_SHA512:
		this->Hash = new SHA512;
		this->Digest = new byte[CryptoPP::SHA512::DIGESTSIZE];
		break;
	case HASH_SHA224:
		this->Hash = new SHA224;
		this->Digest = new byte[CryptoPP::SHA224::DIGESTSIZE];
		break;
	case HASH_SHA384:
		this->Hash = new SHA384;
		this->Digest = new byte[CryptoPP::SHA384::DIGESTSIZE];
		break;
	case HASH_MD2:
		this->Hash = new Weak1::MD2;
		this->Digest = new byte[CryptoPP::Weak1::MD2::DIGESTSIZE];
		break;
	case HASH_MD4:
		this->Hash = new Weak1::MD4;
		this->Digest = new byte[CryptoPP::Weak1::MD4::DIGESTSIZE];
		break;
	case HASH_MD5:
		this->Hash = new Weak1::MD5;
		this->Digest = new byte[CryptoPP::Weak1::MD5::DIGESTSIZE];
		break;
	case HASH_TIGER:
		this->Hash = new Tiger;
		this->Digest = new byte[CryptoPP::Tiger::DIGESTSIZE];
		break;
	case HASH_WHIRLPOOL:
		this->Hash = new Whirlpool;
		this->Digest = new byte[CryptoPP::Whirlpool::DIGESTSIZE];
		break;
	case HASH_RIPEMD160:
		this->Hash = new RIPEMD160;
		this->Digest = new byte[CryptoPP::RIPEMD160::DIGESTSIZE];
		break;
	case HASH_RIPEMD320:
		this->Hash = new RIPEMD320;
		this->Digest = new byte[CryptoPP::RIPEMD320::DIGESTSIZE];
		break;
	case HASH_RIPEMD128:
		this->Hash = new RIPEMD128;
		this->Digest = new byte[CryptoPP::RIPEMD128::DIGESTSIZE];
		break;
	case HASH_RIPEMD256:
		this->Hash = new RIPEMD256;
		this->Digest = new byte[CryptoPP::RIPEMD256::DIGESTSIZE];
		break;
	default:
		this->Hash = new SHA256;
		this->Digest = new byte[CryptoPP::SHA256::DIGESTSIZE];
		break;
	}
}

wxHash::~wxHash() {
	delete this->Hash;
	delete this->Digest;
}

bool wxHash::FromInputStream(wxInputStream *Input, const unsigned int ReadBytes) {
	byte Bytes[ReadBytes];
	while (!Input->Eof()) {
		Input->Read(Bytes, sizeof(Bytes));
		switch (Input->GetLastError()) {
		case wxSTREAM_NO_ERROR:
			this->Hash->Update(Bytes, Input->LastRead());
			break;
		case wxSTREAM_EOF:
			this->Hash->Update(Bytes, Input->LastRead());
			break;
		case wxSTREAM_READ_ERROR:
		case wxSTREAM_WRITE_ERROR:
			return false;
			break;
		default:
			return false;
			break;
		}
	}
	return true;
}

bool wxHash::FromString(wxString &Input) {
	const wxScopedCharBuffer Bytes = Input.To8BitData();
	this->Hash->Update((const byte*) Bytes.data(), Bytes.length());
	return true;
}

bool wxHash::CreateHash() {
	this->Hash->Final(this->Digest);
	return true;
}

wxString wxHash::GetHexStringHash() {
	wxString Output;
	Output.Empty();
	if (!this->CreateHash())
		return Output;
	char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

	for (unsigned int i = 0; i < this->Hash->DigestSize(); i++) {
		Output.Append(hexval[((this->Digest[i] >> 4) & 0xF)]);
		Output.Append(hexval[(this->Digest[i]) & 0x0F]);
	}
	return Output;
}
for example!

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Thu Sep 17, 2009 9:45 am

Here is a simple snip of code I use to encrypt and decrypt text.

Code: Select all

//// wxCrypto.h ////////////////////////

#pragma once
#ifndef WXCRYPTO_H
#define WXCRYPTO_H

//Crypto includes
#include "cryptlib.h"
#include "aes.h"        // AES
#include "modes.h"      // CBC_Mode< >
#include "filters.h"
#include "base32.h"

class wxCrypto
{
public:
	wxCrypto();
	std::string Encrypt(std::string text);
	std::string Decrypt(std::string text);
	
	byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ];
    	byte  iv[ CryptoPP::AES::BLOCKSIZE ];
}

#endif WXCRYPTO_H

Code: Select all

/////// wxCrypto.cpp /////////////////////////////////
wxCrypto::wxCrypto()
{
	//we will just create a simple key in the ctor
	
	// Key and IV setup
    ::memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
    ::memset(  iv, 0x01, CryptoPP::AES::BLOCKSIZE );
}

//////////////////////////////////////////////////
std::string wxCrypto::Encrypt(std::string text)
{
	CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption
        Encryptor( key, sizeof(key), iv );

	std::string EncodedText;
	CryptoPP::StringSource( text, true,
		new CryptoPP::StreamTransformationFilter( Encryptor,
			new CryptoPP::Base32Encoder(
				new CryptoPP::StringSink( EncodedText )
			) // Base32Encoder
		) // StreamTransformationFilter
	); // StringSource

	return EncryptedText;
}

//////////////////////////////////////////////////
std::string wxCrypto::Decrypt(std::string text)
{
    // Decryption
    CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption
        Decryptor( key, sizeof(key), iv );

	std::string RecoveredText;
    CryptoPP::StringSource( text, true,
		new CryptoPP::Base32Decoder(
			new CryptoPP::StreamTransformationFilter( Decryptor,
				new CryptoPP::StringSink( RecoveredText )
			) // StreamTransformationFilter
		) // Base32Encoder
	); // StringSource

	return DecryptedText;
}
[/code]

Post Reply