PRNG: Read input using C++
In the old code, cppcheck complained about throwing in the destructor, but like, seriously? Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
committed by
Keith Winstein
parent
14ef590220
commit
ecdd2dd648
+5
-17
@@ -34,10 +34,8 @@
|
|||||||
#define PRNG_HPP
|
#define PRNG_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
@@ -51,32 +49,22 @@ using namespace Crypto;
|
|||||||
|
|
||||||
class PRNG {
|
class PRNG {
|
||||||
private:
|
private:
|
||||||
FILE *randfile;
|
std::ifstream randfile;
|
||||||
|
|
||||||
/* unimplemented to satisfy -Weffc++ */
|
/* unimplemented to satisfy -Weffc++ */
|
||||||
PRNG( const PRNG & );
|
PRNG( const PRNG & );
|
||||||
PRNG & operator=( const PRNG & );
|
PRNG & operator=( const PRNG & );
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PRNG() : randfile( fopen( rdev, "rb" ) )
|
PRNG() : randfile( rdev, std::ifstream::in | std::ifstream::binary ) {}
|
||||||
{
|
|
||||||
if ( randfile == NULL ) {
|
|
||||||
throw CryptoException( std::string( rdev ) + ": " + strerror( errno ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~PRNG() {
|
|
||||||
if ( 0 != fclose( randfile ) ) {
|
|
||||||
throw CryptoException( std::string( rdev ) + ": " + strerror( errno ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void fill( void *dest, size_t size ) {
|
void fill( void *dest, size_t size ) {
|
||||||
if ( 0 == size ) {
|
if ( 0 == size ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 1 != fread( dest, size, 1, randfile ) ) {
|
randfile.read( static_cast<char *>( dest ), size );
|
||||||
|
if ( !randfile ) {
|
||||||
throw CryptoException( "Could not read from " + std::string( rdev ) );
|
throw CryptoException( "Could not read from " + std::string( rdev ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user