From 293a7f4e2ae9b458d3efefcfe133d0ad5320a1b2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 16 Nov 2011 10:04:41 -0500 Subject: cmCryptoHash: Provide factory "New" method Construct a cmCryptoHash subclass instance based on the name of the desired hash algorithm. --- Source/cmCryptoHash.cxx | 19 +++++++++++++++++++ Source/cmCryptoHash.h | 3 +++ Source/cmFileCommand.cxx | 14 +------------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Source/cmCryptoHash.cxx b/Source/cmCryptoHash.cxx index 411da58..a1505bd 100644 --- a/Source/cmCryptoHash.cxx +++ b/Source/cmCryptoHash.cxx @@ -15,6 +15,25 @@ #include "cm_sha2.h" //---------------------------------------------------------------------------- +cmsys::auto_ptr cmCryptoHash::New(const char* algo) +{ + if(strcmp(algo,"MD5") == 0) + { return cmsys::auto_ptr(new cmCryptoHashMD5); } + else if(strcmp(algo,"SHA1") == 0) + { return cmsys::auto_ptr(new cmCryptoHashSHA1); } + else if(strcmp(algo,"SHA224") == 0) + { return cmsys::auto_ptr(new cmCryptoHashSHA224); } + else if(strcmp(algo,"SHA256") == 0) + { return cmsys::auto_ptr(new cmCryptoHashSHA256); } + else if(strcmp(algo,"SHA384") == 0) + { return cmsys::auto_ptr(new cmCryptoHashSHA384); } + else if(strcmp(algo,"SHA512") == 0) + { return cmsys::auto_ptr(new cmCryptoHashSHA512); } + else + { return cmsys::auto_ptr(0); } +} + +//---------------------------------------------------------------------------- std::string cmCryptoHash::HashString(const char* input) { this->Initialize(); diff --git a/Source/cmCryptoHash.h b/Source/cmCryptoHash.h index c17104b..0a33365 100644 --- a/Source/cmCryptoHash.h +++ b/Source/cmCryptoHash.h @@ -14,9 +14,12 @@ #include "cmStandardIncludes.h" +#include + class cmCryptoHash { public: + static cmsys::auto_ptr New(const char* algo); std::string HashString(const char* input); std::string HashFile(const char* file); protected: diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 35c743d..bab3116 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -360,19 +360,7 @@ bool cmFileCommand::HandleHashCommand(std::vector const& args) return false; } - cmsys::auto_ptr 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 hash(cmCryptoHash::New(args[0].c_str())); if(hash.get()) { std::string out = hash->HashFile(args[1].c_str()); -- cgit v0.12