diff options
author | Brad King <brad.king@kitware.com> | 2011-11-16 01:32:43 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2011-11-16 15:15:44 (GMT) |
commit | 38771d3bdf51b81d46578e0c81ebddbdea0ce3b2 (patch) | |
tree | cb83e9ed3b723144b427a5acf083fdc384953761 /Source | |
parent | 73efd4a5044d2346e14d019197e2ddced3f9b7a8 (diff) | |
download | CMake-38771d3bdf51b81d46578e0c81ebddbdea0ce3b2.zip CMake-38771d3bdf51b81d46578e0c81ebddbdea0ce3b2.tar.gz CMake-38771d3bdf51b81d46578e0c81ebddbdea0ce3b2.tar.bz2 |
Add file(SHA*) commands to compute cryptographic hashes
Add a file() command API for SHA1, SHA224, SHA256, SHA384, and SHA512.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmCryptoHash.cxx | 21 | ||||
-rw-r--r-- | Source/cmCryptoHash.h | 21 | ||||
-rw-r--r-- | Source/cmFileCommand.cxx | 17 | ||||
-rw-r--r-- | Source/cmFileCommand.h | 4 |
4 files changed, 60 insertions, 3 deletions
diff --git a/Source/cmCryptoHash.cxx b/Source/cmCryptoHash.cxx index 3a73398..411da58 100644 --- a/Source/cmCryptoHash.cxx +++ b/Source/cmCryptoHash.cxx @@ -12,6 +12,7 @@ #include "cmCryptoHash.h" #include <cmsys/MD5.h> +#include "cm_sha2.h" //---------------------------------------------------------------------------- std::string cmCryptoHash::HashString(const char* input) @@ -88,3 +89,23 @@ std::string cmCryptoHashMD5::Finalize() cmsysMD5_FinalizeHex(this->MD5, md5out); return std::string(md5out, 32); } + + +#define cmCryptoHash_SHA_CLASS_IMPL(SHA) \ +cmCryptoHash##SHA::cmCryptoHash##SHA(): SHA(new SHA_CTX) {} \ +cmCryptoHash##SHA::~cmCryptoHash##SHA() { delete this->SHA; } \ +void cmCryptoHash##SHA::Initialize() { SHA##_Init(this->SHA); } \ +void cmCryptoHash##SHA::Append(unsigned char const* buf, int sz) \ +{ SHA##_Update(this->SHA, buf, sz); } \ +std::string cmCryptoHash##SHA::Finalize() \ +{ \ + char out[SHA##_DIGEST_STRING_LENGTH]; \ + SHA##_End(this->SHA, out); \ + return std::string(out, SHA##_DIGEST_STRING_LENGTH-1); \ +} + +cmCryptoHash_SHA_CLASS_IMPL(SHA1) +cmCryptoHash_SHA_CLASS_IMPL(SHA224) +cmCryptoHash_SHA_CLASS_IMPL(SHA256) +cmCryptoHash_SHA_CLASS_IMPL(SHA384) +cmCryptoHash_SHA_CLASS_IMPL(SHA512) diff --git a/Source/cmCryptoHash.h b/Source/cmCryptoHash.h index 670624c..c17104b 100644 --- a/Source/cmCryptoHash.h +++ b/Source/cmCryptoHash.h @@ -37,4 +37,25 @@ protected: virtual std::string Finalize(); }; +#define cmCryptoHash_SHA_CLASS_DECL(SHA) \ + class cmCryptoHash##SHA: public cmCryptoHash \ + { \ + union _SHA_CTX* SHA; \ + public: \ + cmCryptoHash##SHA(); \ + ~cmCryptoHash##SHA(); \ + protected: \ + virtual void Initialize(); \ + virtual void Append(unsigned char const* buf, int sz); \ + virtual std::string Finalize(); \ + } + +cmCryptoHash_SHA_CLASS_DECL(SHA1); +cmCryptoHash_SHA_CLASS_DECL(SHA224); +cmCryptoHash_SHA_CLASS_DECL(SHA256); +cmCryptoHash_SHA_CLASS_DECL(SHA384); +cmCryptoHash_SHA_CLASS_DECL(SHA512); + +#undef cmCryptoHash_SHA_CLASS_DECL + #endif diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 32454f5..35c743d 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -85,7 +85,12 @@ bool cmFileCommand { return this->HandleReadCommand(args); } - else if ( subCommand == "MD5" ) + else if ( subCommand == "MD5" || + subCommand == "SHA1" || + subCommand == "SHA224" || + subCommand == "SHA256" || + subCommand == "SHA384" || + subCommand == "SHA512" ) { return this->HandleHashCommand(args); } @@ -358,6 +363,16 @@ bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args) 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); } if(hash.get()) { std::string out = hash->HashFile(args[1].c_str()); diff --git a/Source/cmFileCommand.h b/Source/cmFileCommand.h index dce6478..9e2ed0f 100644 --- a/Source/cmFileCommand.h +++ b/Source/cmFileCommand.h @@ -65,7 +65,7 @@ public: " file(WRITE filename \"message to write\"... )\n" " file(APPEND filename \"message to write\"... )\n" " file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n" - " file(MD5 filename variable)\n" + " file(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512> filename variable)\n" " file(STRINGS filename variable [LIMIT_COUNT num]\n" " [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n" " [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n" @@ -95,7 +95,7 @@ public: "variable. It will start at the given offset and read up to numBytes. " "If the argument HEX is given, the binary data will be converted to " "hexadecimal representation and this will be stored in the variable.\n" - "MD5 " + "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 " "will compute a cryptographic hash of the content of a file.\n" "STRINGS will parse a list of ASCII strings from a file and " "store it in a variable. Binary data in the file are ignored. Carriage " |