summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2011-11-16 01:32:43 (GMT)
committerBrad King <brad.king@kitware.com>2011-11-16 15:15:44 (GMT)
commit38771d3bdf51b81d46578e0c81ebddbdea0ce3b2 (patch)
treecb83e9ed3b723144b427a5acf083fdc384953761
parent73efd4a5044d2346e14d019197e2ddced3f9b7a8 (diff)
downloadCMake-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.
-rw-r--r--Source/cmCryptoHash.cxx21
-rw-r--r--Source/cmCryptoHash.h21
-rw-r--r--Source/cmFileCommand.cxx17
-rw-r--r--Source/cmFileCommand.h4
-rw-r--r--Tests/CMakeTests/File-SHA1-Works.cmake2
-rw-r--r--Tests/CMakeTests/File-SHA224-Works.cmake2
-rw-r--r--Tests/CMakeTests/File-SHA256-Works.cmake2
-rw-r--r--Tests/CMakeTests/File-SHA384-Works.cmake2
-rw-r--r--Tests/CMakeTests/File-SHA512-Works.cmake2
-rw-r--r--Tests/CMakeTests/FileTest.cmake.in15
10 files changed, 85 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 "
diff --git a/Tests/CMakeTests/File-SHA1-Works.cmake b/Tests/CMakeTests/File-SHA1-Works.cmake
new file mode 100644
index 0000000..03ea3ac
--- /dev/null
+++ b/Tests/CMakeTests/File-SHA1-Works.cmake
@@ -0,0 +1,2 @@
+file(SHA1 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha1)
+message("${sha1}")
diff --git a/Tests/CMakeTests/File-SHA224-Works.cmake b/Tests/CMakeTests/File-SHA224-Works.cmake
new file mode 100644
index 0000000..0295346
--- /dev/null
+++ b/Tests/CMakeTests/File-SHA224-Works.cmake
@@ -0,0 +1,2 @@
+file(SHA224 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha224)
+message("${sha224}")
diff --git a/Tests/CMakeTests/File-SHA256-Works.cmake b/Tests/CMakeTests/File-SHA256-Works.cmake
new file mode 100644
index 0000000..32bc9fe
--- /dev/null
+++ b/Tests/CMakeTests/File-SHA256-Works.cmake
@@ -0,0 +1,2 @@
+file(SHA256 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha256)
+message("${sha256}")
diff --git a/Tests/CMakeTests/File-SHA384-Works.cmake b/Tests/CMakeTests/File-SHA384-Works.cmake
new file mode 100644
index 0000000..5c56de2
--- /dev/null
+++ b/Tests/CMakeTests/File-SHA384-Works.cmake
@@ -0,0 +1,2 @@
+file(SHA384 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha384)
+message("${sha384}")
diff --git a/Tests/CMakeTests/File-SHA512-Works.cmake b/Tests/CMakeTests/File-SHA512-Works.cmake
new file mode 100644
index 0000000..cf10706
--- /dev/null
+++ b/Tests/CMakeTests/File-SHA512-Works.cmake
@@ -0,0 +1,2 @@
+file(SHA512 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha512)
+message("${sha512}")
diff --git a/Tests/CMakeTests/FileTest.cmake.in b/Tests/CMakeTests/FileTest.cmake.in
index 3aa88a7..824aba7 100644
--- a/Tests/CMakeTests/FileTest.cmake.in
+++ b/Tests/CMakeTests/FileTest.cmake.in
@@ -22,6 +22,16 @@ set(MD5-BadArg4-RESULT 1)
set(MD5-BadArg4-STDERR "file MD5 requires a file name and output variable")
set(MD5-Works-RESULT 0)
set(MD5-Works-STDERR "a28a44fb5b58a6acf0cbec46557bc775")
+set(SHA1-Works-RESULT 0)
+set(SHA1-Works-STDERR "e0ce452a7e34e9c705b46e4e7c9cd959082c0777")
+set(SHA224-Works-RESULT 0)
+set(SHA224-Works-STDERR "d1f1c9db3df73586a5a200014505ef03b70da5e0539eb35c11b4ef6f")
+set(SHA256-Works-RESULT 0)
+set(SHA256-Works-STDERR "bb38de1c34713f750a4f84404ef5fb1efb93cf815cb09182db457ba91de19195")
+set(SHA384-Works-RESULT 0)
+set(SHA384-Works-STDERR "2f01d58436bd04a40ec2c938a15f2bb661bf1bf1560a5f2d40a3ebbb716a58e400ab2992c23ba19fa96dc519e0042c26")
+set(SHA512-Works-RESULT 0)
+set(SHA512-Works-STDERR "349d02e347ddb133afb4557c9e541163f54b6eaa9eb345672bfd7bbce90c9872bf1fad9c6662248eb7b092aef719e4b8c3b8bb1cf47f287abbde12360c073686")
include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
check_cmake_test(File
@@ -37,6 +47,11 @@ check_cmake_test(File
MD5-BadArg2
MD5-BadArg4
MD5-Works
+ SHA1-Works
+ SHA224-Works
+ SHA256-Works
+ SHA384-Works
+ SHA512-Works
)
# Also execute each test listed in FileTestScript.cmake: