reduce crypto++ code by doing our own file writing (simpler)

This commit is contained in:
Glenn Maynard
2005-05-19 21:21:37 +00:00
parent f701c3f261
commit d406068148
3 changed files with 38 additions and 65 deletions
-37
View File
@@ -119,43 +119,6 @@ unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigne
return 0;
}
void RageFileSink::IsolatedInitialize(const NameValuePairs &parameters)
{
const char *fileName;
if( !parameters.GetValue("OutputFileName", fileName) )
ASSERT(0);
if( !m_file.Open( fileName, RageFile::WRITE ) ) // trucates existing data
throw OpenErr(fileName);
}
bool RageFileSink::IsolatedFlush(bool hardFlush, bool blocking)
{
if (!m_file.IsOpen())
throw Err("FileSink: output stream not opened");
if( m_file.Flush() == -1 )
throw WriteErr( m_file );
return false;
}
unsigned int RageFileSink::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
{
if (!m_file.IsOpen())
throw Err("FileSink: output stream not opened");
if( m_file.Write((const char *)inString, length) == -1 )
throw WriteErr( m_file );
if (messageEnd)
if( m_file.Flush() == -1 )
throw WriteErr( m_file );
return 0;
}
/*
* (c) 2001-2004 Chris Danford
* All rights reserved.
-25
View File
@@ -55,31 +55,6 @@ public:
: SourceTemplate<RageFileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputFileName", filename)("InputBinaryMode", binary));}
};
//! .
class RageFileSink : public Sink
{
public:
class Err : public Exception
{
public:
Err(const std::string &s) : Exception(IO_ERROR, s) {}
};
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
struct WriteErr : public Err { WriteErr(const RageFile &f) : Err("RageFileSink(" + f.GetPath() + "): write error: " + f.GetError()) {}};
RageFileSink() {}
RageFileSink(const char *filename, bool binary=true)
{IsolatedInitialize(MakeParameters("OutputFileName", filename)("OutputBinaryMode", binary));}
void IsolatedInitialize(const NameValuePairs &parameters);
unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
bool IsolatedFlush(bool hardFlush, bool blocking);
private:
RageFile m_file;
};
#endif
/*
+38 -3
View File
@@ -61,25 +61,56 @@ CryptManager::~CryptManager()
}
static bool WriteFile( CString sFile, CString sBuf )
{
RageFile output;
if( !output.Open(sFile, RageFile::WRITE) )
{
LOG->Warn( "WriteFile: opening %s failed: %s", sFile.c_str(), output.GetError().c_str() );
return false;
}
if( output.Write(sBuf) == -1 || output.Flush() == -1 )
{
LOG->Warn( "WriteFile: writing %s failed: %s", sFile.c_str(), output.GetError().c_str() );
output.Close();
FILEMAN->Remove( sFile );
return false;
}
return true;
}
void CryptManager::GenerateRSAKey( unsigned int keyLength, CString privFilename, CString pubFilename, CString seed )
{
ASSERT( PREFSMAN->m_bSignProfileData );
CString sPubKey, sPrivKey;
try
{
NonblockingRng rng;
RSASSA_PKCS1v15_SHA_Signer priv(rng, keyLength);
RageFileSink privFile(privFilename);
StringSink privFile( sPrivKey );
priv.DEREncode(privFile);
privFile.MessageEnd();
RSASSA_PKCS1v15_SHA_Verifier pub(priv);
RageFileSink pubFile(pubFilename);
StringSink pubFile( sPubKey );
pub.DEREncode(pubFile);
pubFile.MessageEnd();
} catch( const CryptoPP::Exception &s ) {
LOG->Warn( "GenerateRSAKey failed: %s", s.what() );
return;
}
if( !WriteFile(pubFilename, sPubKey) )
return;
if( !WriteFile(privFilename, sPrivKey) )
{
FILEMAN->Remove( privFilename );
return;
}
}
@@ -104,15 +135,19 @@ void CryptManager::SignFileToFile( CString sPath, CString sSignatureFile )
return;
}
CString sSignature;
try {
RageFileSource privFile(sPrivFilename, true);
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
NonblockingRng rng;
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new RageFileSink(sSignatureFile)));
RageFileSource f(sMessageFilename, true, new SignerFilter(rng, priv, new StringSink(sSignature)));
} catch( const CryptoPP::Exception &s ) {
LOG->Warn( "SignFileToFile failed: %s", s.what() );
return;
}
WriteFile( sSignatureFile, sSignature );
}
bool CryptManager::VerifyFileWithFile( CString sPath, CString sSignatureFile )