Add file(SHA*) commands to compute cryptographic hashes

Add a file() command API for SHA1, SHA224, SHA256, SHA384, and SHA512.
This commit is contained in:
Brad King 2011-11-15 20:32:43 -05:00
parent 73efd4a504
commit 38771d3bdf
10 changed files with 85 additions and 3 deletions

View File

@ -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)

View File

@ -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

View File

@ -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());

View File

@ -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 "

View File

@ -0,0 +1,2 @@
file(SHA1 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha1)
message("${sha1}")

View File

@ -0,0 +1,2 @@
file(SHA224 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha224)
message("${sha224}")

View File

@ -0,0 +1,2 @@
file(SHA256 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha256)
message("${sha256}")

View File

@ -0,0 +1,2 @@
file(SHA384 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha384)
message("${sha384}")

View File

@ -0,0 +1,2 @@
file(SHA512 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake sha512)
message("${sha512}")

View File

@ -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: