⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filters.h

📁 一个DES,RSA,MD5,RC4等加密算法的源码
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef CRYPTOPP_FILTERS_H
#define CRYPTOPP_FILTERS_H

#include "cryptlib.h"
#include "misc.h"
#include "smartptr.h"
#include "queue.h"

NAMESPACE_BEGIN(CryptoPP)

/// provides an implementation of BufferedTransformation's attachment interface
class Filter : virtual public BufferedTransformation
{
public:
	Filter(BufferedTransformation *outQ);

	bool Attachable() {return true;}
	BufferedTransformation *AttachedTransformation() {return m_outQueue.get();}
	const BufferedTransformation *AttachedTransformation() const {return m_outQueue.get();}
	void Detach(BufferedTransformation *newOut = NULL);

protected:
	virtual void NotifyAttachmentChange() {}
	void Insert(Filter *nextFilter);	// insert filter after this one

private:
	void operator=(const Filter &); // assignment not allowed

	member_ptr<BufferedTransformation> m_outQueue;
};

//! .
class TransparentFilter : public Filter
{
public:
	TransparentFilter(BufferedTransformation *outQ=NULL) : Filter(outQ) {}
	void Put(byte inByte) {AttachedTransformation()->Put(inByte);}
	void Put(const byte *inString, unsigned int length) {AttachedTransformation()->Put(inString, length);}
};

//! .
class OpaqueFilter : public Filter
{
public:
	OpaqueFilter(BufferedTransformation *outQ=NULL) : Filter(outQ) {}
	void Put(byte inByte) {}
	void Put(const byte *inString, unsigned int length) {}
};

/*! FilterWithBufferedInput divides up the input stream into
	a first block, a number of middle blocks, and a last block.
	First and last blocks are optional, and middle blocks may
	be a stream instead (i.e. blockSize == 1).
*/
class FilterWithBufferedInput : public Filter
{
public:
	/// firstSize and lastSize may be 0, blockSize must be at least 1
	FilterWithBufferedInput(unsigned int firstSize, unsigned int blockSize, unsigned int lastSize, BufferedTransformation *outQ);
	void Put(byte inByte);
	void Put(const byte *inString, unsigned int length);
	void MessageEnd(int propagation=-1);

	/*! the input buffer may contain more than blockSize bytes if lastSize != 0
		ForceNextPut() forces a call to NextPut() if this is the case 
	*/
	void ForceNextPut();

protected:
	bool DidFirstPut() {return m_firstInputDone;}

	// FirstPut() is called if (firstSize != 0 and totalLength >= firstSize)
	// or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received))
	virtual void FirstPut(const byte *inString) =0;
	// NextPut() is called if totalLength >= firstSize+blockSize+lastSize
	// length parameter is always blockSize unless blockSize == 1
	virtual void NextPut(const byte *inString, unsigned int length) =0;
	// LastPut() is always called
	// if totalLength < firstSize then length == totalLength
	// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
	// else lastSize <= length < lastSize+blockSize
	virtual void LastPut(const byte *inString, unsigned int length) =0;

private:
	class BlockQueue
	{
	public:
		BlockQueue(unsigned int blockSize, unsigned int maxBlocks);
		void ResetQueue(unsigned int blockSize, unsigned int maxBlocks);
		const byte *GetBlock();
		const byte *GetContigousBlocks(unsigned int &numberOfBlocks);
		unsigned int GetAll(byte *outString);
		void Put(const byte *inString, unsigned int length);
		unsigned int CurrentSize() const {return m_size;}
		unsigned int MaxSize() const {return m_buffer.size;}

	private:
		SecByteBlock m_buffer;
		unsigned int m_blockSize, m_maxBlocks, m_size;
		byte *m_begin;
	};

	unsigned int m_firstSize, m_blockSize, m_lastSize;
	bool m_firstInputDone;
	BlockQueue m_queue;
};

//! .
class FilterWithInputQueue : public Filter
{
public:
	FilterWithInputQueue(BufferedTransformation *attachment) : Filter(attachment) {}
	void Put(byte inByte) {m_inQueue.Put(inByte);}
	void Put(const byte *inString, unsigned int length) {m_inQueue.Put(inString, length);}

protected:
	ByteQueue m_inQueue;
};

//! Filter Wrapper for StreamCipher
class StreamCipherFilter : public Filter
{
public:
	StreamCipherFilter(StreamCipher &c,
					   BufferedTransformation *outQueue = NULL)
		: cipher(c), Filter(outQueue) {}

	void Put(byte inByte)
		{AttachedTransformation()->Put(cipher.ProcessByte(inByte));}

	void Put(const byte *inString, unsigned int length);

private:
	StreamCipher &cipher;
};

//! Filter Wrapper for HashModule
class HashFilter : public Filter
{
public:
	HashFilter(HashModule &hm, BufferedTransformation *outQueue = NULL, bool putMessage=false)
		: Filter(outQueue), m_hashModule(hm), m_putMessage(putMessage) {}

	void MessageEnd(int propagation=-1);

	void Put(byte inByte);
	void Put(const byte *inString, unsigned int length);

private:
	HashModule &m_hashModule;
	bool m_putMessage;
};

//! Filter Wrapper for HashModule
class HashVerifier : public FilterWithBufferedInput
{
public:
	class HashVerificationFailed : public BufferedTransformation::Err
	{
	public:
		HashVerificationFailed()
			: BufferedTransformation::Err(DATA_INTEGRITY_CHECK_FAILED, "HashVerifier: message hash not correct") {}
	};

	enum Flags {HASH_AT_BEGIN=1, PUT_MESSAGE=2, PUT_HASH=4, PUT_RESULT=8, THROW_EXCEPTION=16};
	HashVerifier(HashModule &hm, BufferedTransformation *outQueue = NULL, word32 flags = HASH_AT_BEGIN | PUT_RESULT);

	bool GetLastResult() const {return m_verified;}

protected:
	void FirstPut(const byte *inString);
	void NextPut(const byte *inString, unsigned int length);
	void LastPut(const byte *inString, unsigned int length);

private:
	HashModule &m_hashModule;
	word32 m_flags;
	SecByteBlock m_expectedHash;
	bool m_verified;
};

//! Filter Wrapper for PK_Signer
class SignerFilter : public Filter
{
public:
	SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *outQueue = NULL)
		: m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewMessageAccumulator()), Filter(outQueue) {}

	void MessageEnd(int propagation);

	void Put(byte inByte)
		{m_messageAccumulator->Update(&inByte, 1);}

	void Put(const byte *inString, unsigned int length)
		{m_messageAccumulator->Update(inString, length);}

private:
	RandomNumberGenerator &m_rng;
	const PK_Signer &m_signer;
	member_ptr<HashModule> m_messageAccumulator;
};

//! Filter Wrapper for PK_Verifier
class VerifierFilter : public Filter
{
public:
	VerifierFilter(const PK_Verifier &verifier, BufferedTransformation *outQueue = NULL)
		: m_verifier(verifier), m_messageAccumulator(verifier.NewMessageAccumulator())
		, m_signature(verifier.SignatureLength()), Filter(outQueue) {}

	// this function must be called before MessageEnd()
	void PutSignature(const byte *sig);

	void MessageEnd(int propagation);

	void Put(byte inByte)
		{m_messageAccumulator->Update(&inByte, 1);}

	void Put(const byte *inString, unsigned int length)
		{m_messageAccumulator->Update(inString, length);}

private:
	const PK_Verifier &m_verifier;
	member_ptr<HashModule> m_messageAccumulator;
	SecByteBlock m_signature;
};

//! A BufferedTransformation that doesn't produce any retrievable output
class Sink : public BufferedTransformation
{
};

//! .
class BitBucket : public Sink
{
public:
	void Put(byte) {}
	void Put(const byte *, unsigned int) {}
};

extern BitBucket g_bitBucket;

//! Redirect input to another BufferedTransformation without owning it
class Redirector : public Sink
{
public:
	Redirector() : m_target(NULL), m_passSignal(true) {}
	Redirector(BufferedTransformation &target, bool passSignal=true) : m_target(&target), m_passSignal(passSignal) {}

	void Redirect(BufferedTransformation &target) {m_target = &target;}
	void StopRedirect() {m_target = NULL;}
	bool GetPassSignal() const {return m_passSignal;}
	void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}

	void Put(byte b) 
		{if (m_target) m_target->Put(b);}
	void Put(const byte *string, unsigned int len) 
		{if (m_target) m_target->Put(string, len);}
	void Flush(bool completeFlush, int propagation=-1) 
		{if (m_target && m_passSignal) m_target->Flush(completeFlush, propagation);}
	void MessageEnd(int propagation=-1)
		{if (m_target && m_passSignal) m_target->MessageEnd(propagation);}
	void MessageSeriesEnd(int propagation=-1) 
		{if (m_target && m_passSignal) m_target->MessageSeriesEnd(propagation);}

	void ChannelPut(const std::string &channel, byte b) 
		{if (m_target) m_target->ChannelPut(channel, b);}
	void ChannelPut(const std::string &channel, const byte *string, unsigned int len) 
		{if (m_target) m_target->ChannelPut(channel, string, len);}
	void ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1) 
		{if (m_target && m_passSignal) m_target->ChannelFlush(channel, completeFlush, propagation);}
	void ChannelMessageEnd(const std::string &channel, int propagation=-1)
		{if (m_target && m_passSignal) m_target->ChannelMessageEnd(channel, propagation);}
	void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1) 
		{if (m_target && m_passSignal) m_target->ChannelMessageSeriesEnd(channel, propagation);}

private:
	BufferedTransformation *m_target;
	bool m_passSignal;
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -