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

📄 random.cpp

📁 加密解密,安全工具!很有意思的代码
💻 CPP
字号:
/******************************************************************* * *	Copyright (c) 1994-2005 Jetico, Inc., Finland *	All rights reserved. * *	File: sha256random.cpp *	Revision:  *	Created: *	Description: implementation of random generator based on SHA-1  *		Secure Hash Algorithm  * *******************************************************************/#include "random.h"#include <malloc.h>#include <memory.h>/**************************************************************************************\ * Function:  SHA256RandomAllocate *  void **sRandom    pointer to internal structure of random generator; * * Return value: *  If successfull return NO_ERROR.\**************************************************************************************/int SHA256RandomAllocate(void **sRandom);/**************************************************************************************\ * Function:  SHA256RandomFree *  void **sRandom    pointer to internal structure of random generator; * * Return value: *  If successfull return NO_ERROR.\**************************************************************************************/int SHA256RandomFree(void **sRandom);/*************************************************************************************\** Function:  SHA256RandomInitialize*	void *sRandom	- pointer to internal structure of random generator;*\*************************************************************************************/int	SHA256RandomInitialize( void *sRandom );/*************************************************************************************\** Function:  SHA256RandomUpdate*	void *sRandom			- pointer to internal structure of random generator;*	byte   *seedBuffer      - pointer to initialisation buffer;*	size_t  seedBufferSize	- size of the initialization buffer;** Description:*  Use this function to initialize current random state by seedBuffer.*  The seedBufferSize does not limited. State will always padded by SHA256 digest*  algorithm.*\*************************************************************************************/int SHA256RandomRandomize(void *sRandom,                         byte *seedBuffer, size_t seedBufferSize);/*************************************************************************************\** Function: SHA256RandomGenerateBytes (*	void *sRandom		- pointer to internal structure of random generator;*	byte *buffer		- pointer to buffer for new generated random data*	size_t bufferSize	- size in bytes of required random data** Description:*  sRandom structure must be initialized by SHA256RandomInit.*  SHA256RandomGenerate function can by called numerous times.\*************************************************************************************/int SHA256RandomGetRandomBytes(	void *sRandom,	byte *buffer ,	size_t bufferSize);/*************************************************************************************\** Function: SHA256RandomFinal (*	void *sRandom		- pointer to void structure;** Description:*  secure zeroizes sRandom structure ..and deallocate it.\*************************************************************************************/int SHA256RandomFinalize(void *sRandom);typedef struct _SHA256RandomData SHA256RandomData;struct _SHA256RandomData{	void *SHACtx;		/* SHA256 hash generator used */	byte *state;		/* Current state buffer */	size_t stateLength;	/* Usually equals to SHA256_DIGEST_SIZE */	byte *pool;			/* Buffer for random data generated from state by SHACtx*/	size_t poolLength;	/* Usually equals to SHA256_DIGEST_SIZE */	size_t residue;		/* Quantity of unused bytes */};static void SHA256RandomReGenerate(void *sRandom);#define SHA_NO_ERROR 0#define MALLOC_ERROR 1int SHA256RandomAllocate(void **sRandom){	void **SHA_ctx;	SHA256RandomData *sha256Data;	int result;	/* Allocate memory for the rng's structure. */	sha256Data = (SHA256RandomData *) malloc(sizeof (SHA256RandomData));	if ( sha256Data == NULL )		return MALLOC_ERROR;	/* Allocate memory for the rng's pool of random bytes. */	sha256Data->pool = (byte *)malloc(SHA256_DIGEST_SIZE);	if (sha256Data->pool == NULL ) {		free(sha256Data);		return MALLOC_ERROR;	}	sha256Data->poolLength = SHA256_DIGEST_SIZE;	/* Allocate memory for rng's state in rng specific data. */	sha256Data->state = (byte *)malloc(SHA256_DIGEST_SIZE);	if (sha256Data->state == NULL ) {		free(sha256Data->pool);		free(sha256Data);		return MALLOC_ERROR;	}	sha256Data->stateLength=SHA256_DIGEST_SIZE;	/* Allocate the rng's hash algorithm context. */	SHA_ctx=&(sha256Data->SHACtx);	if ((result=SHA256_Allocate(SHA_ctx))!=SHA_NO_ERROR) {		free(sha256Data->state);		free(sha256Data->pool);		free(sha256Data);		return result;	}	*sRandom = sha256Data;	return SHA_NO_ERROR;}int SHA256RandomInitialize( void *sRandom ){	SHA256RandomData *sha256Data = (SHA256RandomData *)sRandom;	memset(sha256Data->pool, 0, sha256Data->poolLength);	memset(sha256Data->state, 0, sha256Data->stateLength);	sha256Data->residue=0;	return SHA_NO_ERROR;}int SHA256RandomFinalize(void *sRandom){	SHA256RandomData *sha256Data = (SHA256RandomData *)sRandom;	memset(sha256Data->pool, 0, sha256Data->poolLength);	memset(sha256Data->state, 0, sha256Data->stateLength);	sha256Data->residue=0;	return SHA_NO_ERROR;}int SHA256RandomFree(void **sRandom){	SHA256RandomData *sha256Data = (SHA256RandomData *)(*sRandom);	SHA256_Free(&(sha256Data->SHACtx));	free(sha256Data->state);	free(sha256Data->pool);	free(sha256Data);	return SHA_NO_ERROR;}/** * Randomizes the random number pool using SHA256 hashing algorithm. * The function can by called many times. * Digest of seed data will accumulated in sha256Data->state. */int SHA256RandomRandomize(void *sRandom, byte *seed, size_t seedLength){	size_t x,i;	SHA256RandomData *sha256Data = (SHA256RandomData *)sRandom;	void *SHA_ctx = sha256Data->SHACtx;	SHA256_Init( SHA_ctx ); 	SHA256_Process(SHA_ctx, seed, seedLength);	SHA256_Final(SHA_ctx,  sha256Data->pool);	/* add digest to state */	for (i = sha256Data->stateLength, x = 0; i-- ; )	{	x += sha256Data->state[i] + sha256Data->pool[i];		sha256Data->state[i] = (byte)x;		x >>= 8;	}	memset(sha256Data->pool, 0, sha256Data->poolLength);	sha256Data->residue = 0;	return SHA_NO_ERROR;}int SHA256RandomGetRandomBytes(void *sRandom, byte *buffer, size_t bufferSize){	SHA256RandomData *sha256Data = (SHA256RandomData *)sRandom;	while (bufferSize > sha256Data->residue)	{		memcpy(			buffer ,			&(sha256Data->pool[sha256Data->poolLength - sha256Data->residue]),			sha256Data->residue );		bufferSize -= sha256Data->residue;		buffer += sha256Data->residue;		SHA256RandomReGenerate(sRandom);	}		memcpy(		buffer,		&(sha256Data->pool[sha256Data->poolLength - sha256Data->residue]),		bufferSize);	sha256Data->residue -= bufferSize;	return SHA_NO_ERROR;}static void SHA256RandomReGenerate(void *sRandom){	SHA256RandomData *sha256Data = (SHA256RandomData *)sRandom;	void *SHA_ctx=sha256Data->SHACtx;	int i;	/* Generate new data */	SHA256_Init( SHA_ctx ); 	SHA256_Process(SHA_ctx, sha256Data->state, sha256Data->stateLength);	/* FinalDigest array must be >= 32 bytes */	SHA256_Final(SHA_ctx, sha256Data->pool);	/* Increment current state */	for (i = sha256Data->stateLength ; i-- ; )		if (sha256Data->state[i]++)			break;	sha256Data->residue = sha256Data->poolLength;}/*************************************************************************************\*	Function : SHA256RandomGenerate*		High level function.*		Allocate and initialize SHA256 random generator;*		Set seed by seedBuffer;*		Generate random sequence of bytes to buffer;*		Free SHA256 random generator.*	Params:*		byte	*byteBuffer ,	- destination buffer for random generation.*		size_t	byteBufferSize,	- size of destnation buffer in bytes.*		byte	*seedBuffer ,	- seed byte stream to initaialize prime random generator.*		size_t	seedBufferSize	- size of seedBuffer in bytes.*  Return value :*		Return SHA_NO_ERROR if successful.\*************************************************************************************/int SHA256RandomGenerate( byte *byteBuffer ,size_t byteBufferSize ,byte *seedBuffer ,size_t seedBufferSize ){	void *sRandom;	int result; 	result = SHA256RandomAllocate( &sRandom );	if ( result != SHA_NO_ERROR )		return result;	result = SHA256RandomInitialize( sRandom );	if ( result == SHA_NO_ERROR )		result = SHA256RandomRandomize( sRandom, seedBuffer, seedBufferSize );	if ( result == SHA_NO_ERROR )		result = SHA256RandomGetRandomBytes( sRandom, byteBuffer, byteBufferSize );		if ( result == SHA_NO_ERROR )		result = SHA256RandomFinalize( sRandom );	SHA256RandomFree( &sRandom );	return result;}

⌨️ 快捷键说明

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