split out CryptManager::Sign

This commit is contained in:
Glenn Maynard
2007-01-26 04:22:31 +00:00
parent a85eb727c2
commit 8c6fe59973
2 changed files with 20 additions and 11 deletions
+19 -11
View File
@@ -268,7 +268,6 @@ void CryptManager::SignFileToFile( RString sPath, RString sSignatureFile )
ASSERT( PREFSMAN->m_bSignProfileData ); ASSERT( PREFSMAN->m_bSignProfileData );
RString sPrivFilename = PRIVATE_KEY_PATH; RString sPrivFilename = PRIVATE_KEY_PATH;
RString sMessageFilename = sPath;
if( sSignatureFile.empty() ) if( sSignatureFile.empty() )
sSignatureFile = sPath + SIGNATURE_APPEND; sSignatureFile = sPath + SIGNATURE_APPEND;
@@ -276,29 +275,38 @@ void CryptManager::SignFileToFile( RString sPath, RString sSignatureFile )
if( !GetFileContents(sPrivFilename, sPrivKey) ) if( !GetFileContents(sPrivFilename, sPrivKey) )
return; return;
if( !IsAFile(sMessageFilename) ) RString sSignature;
{ if( !Sign(sPath, sSignature, sPrivKey) )
LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sMessageFilename.c_str() );
return; return;
WriteFile( sSignatureFile, sSignature );
}
bool CryptManager::Sign( RString sPath, RString &sSignatureOut, RString sPrivKey )
{
if( !IsAFile(sPath) )
{
LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sPath.c_str() );
return false;
} }
RageFile file; RageFile file;
if( !file.Open(sMessageFilename) ) if( !file.Open(sPath) )
{ {
LOG->Warn( "SignFileToFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() ); LOG->Warn( "SignFileToFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return; return false;
} }
RSAKeyWrapper key; RSAKeyWrapper key;
if( !key.Load(sPrivKey) ) if( !key.Load(sPrivKey) )
return; return false;
int iHash = register_hash( &sha1_desc ); int iHash = register_hash( &sha1_desc );
ASSERT( iHash >= 0 ); ASSERT( iHash >= 0 );
unsigned char buf_hash[20]; unsigned char buf_hash[20];
if( !HashFile(file, buf_hash, iHash) ) if( !HashFile(file, buf_hash, iHash) )
return; return false;
unsigned char signature[256]; unsigned char signature[256];
unsigned long signature_len = sizeof(signature); unsigned long signature_len = sizeof(signature);
@@ -311,11 +319,11 @@ void CryptManager::SignFileToFile( RString sPath, RString sSignatureFile )
if( iRet != CRYPT_OK ) if( iRet != CRYPT_OK )
{ {
LOG->Warn( "SignFileToFile error: %s", error_to_string(iRet) ); LOG->Warn( "SignFileToFile error: %s", error_to_string(iRet) );
return; return false;
} }
RString sSignature( (const char *) signature, signature_len ); sSignatureOut.assign( (const char *) signature, signature_len );
WriteFile( sSignatureFile, sSignature ); return true;
} }
bool CryptManager::VerifyFileWithFile( RString sPath, RString sSignatureFile ) bool CryptManager::VerifyFileWithFile( RString sPath, RString sSignatureFile )
+1
View File
@@ -13,6 +13,7 @@ public:
static void GenerateRSAKey( unsigned int keyLength, RString privFilename, RString pubFilename ); static void GenerateRSAKey( unsigned int keyLength, RString privFilename, RString pubFilename );
static void SignFileToFile( RString sPath, RString sSignatureFile = "" ); static void SignFileToFile( RString sPath, RString sSignatureFile = "" );
static bool Sign( RString sPath, RString &sSignatureOut, RString sPrivateKey );
static bool VerifyFileWithFile( RString sPath, RString sSignatureFile = "" ); static bool VerifyFileWithFile( RString sPath, RString sSignatureFile = "" );
static bool VerifyFileWithFile( RString sPath, RString sSignatureFile, RString sPublicKeyFile ); static bool VerifyFileWithFile( RString sPath, RString sSignatureFile, RString sPublicKeyFile );
static bool Verify( RString sPath, RString sSignature, RString sPublicKey ); static bool Verify( RString sPath, RString sSignature, RString sPublicKey );