cmCryptoHash: Provide factory "New" method

Construct a cmCryptoHash subclass instance based on the name of the
desired hash algorithm.
This commit is contained in:
Brad King 2011-11-16 10:04:41 -05:00
parent 46ab0561fc
commit 293a7f4e2a
3 changed files with 23 additions and 13 deletions

View File

@ -14,6 +14,25 @@
#include <cmsys/MD5.h>
#include "cm_sha2.h"
//----------------------------------------------------------------------------
cmsys::auto_ptr<cmCryptoHash> cmCryptoHash::New(const char* algo)
{
if(strcmp(algo,"MD5") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashMD5); }
else if(strcmp(algo,"SHA1") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA1); }
else if(strcmp(algo,"SHA224") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA224); }
else if(strcmp(algo,"SHA256") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA256); }
else if(strcmp(algo,"SHA384") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA384); }
else if(strcmp(algo,"SHA512") == 0)
{ return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA512); }
else
{ return cmsys::auto_ptr<cmCryptoHash>(0); }
}
//----------------------------------------------------------------------------
std::string cmCryptoHash::HashString(const char* input)
{

View File

@ -14,9 +14,12 @@
#include "cmStandardIncludes.h"
#include <cmsys/auto_ptr.hxx>
class cmCryptoHash
{
public:
static cmsys::auto_ptr<cmCryptoHash> New(const char* algo);
std::string HashString(const char* input);
std::string HashFile(const char* file);
protected:

View File

@ -360,19 +360,7 @@ bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args)
return false;
}
cmsys::auto_ptr<cmCryptoHash> hash;
if(args[0] == "MD5")
{ hash.reset(new cmCryptoHashMD5); }
else if(args[0] == "SHA1")
{ hash.reset(new cmCryptoHashSHA1); }
else if(args[0] == "SHA224")
{ hash.reset(new cmCryptoHashSHA224); }
else if(args[0] == "SHA256")
{ hash.reset(new cmCryptoHashSHA256); }
else if(args[0] == "SHA384")
{ hash.reset(new cmCryptoHashSHA384); }
else if(args[0] == "SHA512")
{ hash.reset(new cmCryptoHashSHA512); }
cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
if(hash.get())
{
std::string out = hash->HashFile(args[1].c_str());