Add sha1sum, sha224sum, sha256sum, sha384sum and sha512sum to command mode

This commit is contained in:
André Klitzing 2017-07-08 22:57:45 +02:00
parent c4647d8432
commit c4de0a25ac
35 changed files with 139 additions and 22 deletions

View File

@ -262,6 +262,36 @@ Available commands are:
351abe79cd3800b38cdfb25d45015a15 file1.txt
052f86c15bbde68af55c7f7b340ab639 file2.txt
``sha1sum <file>...``
Create SHA1 checksum of files in ``sha1sum`` compatible format::
4bb7932a29e6f73c97bb9272f2bdc393122f86e0 file1.txt
1df4c8f318665f9a5f2ed38f55adadb7ef9f559c file2.txt
``sha224sum <file>...``
Create SHA224 checksum of files in ``sha224sum`` compatible format::
b9b9346bc8437bbda630b0b7ddfc5ea9ca157546dbbf4c613192f930 file1.txt
6dfbe55f4d2edc5fe5c9197bca51ceaaf824e48eba0cc453088aee24 file2.txt
``sha256sum <file>...``
Create SHA256 checksum of files in ``sha256sum`` compatible format::
76713b23615d31680afeb0e9efe94d47d3d4229191198bb46d7485f9cb191acc file1.txt
15b682ead6c12dedb1baf91231e1e89cfc7974b3787c1e2e01b986bffadae0ea file2.txt
``sha384sum <file>...``
Create SHA384 checksum of files in ``sha384sum`` compatible format::
acc049fedc091a22f5f2ce39a43b9057fd93c910e9afd76a6411a28a8f2b8a12c73d7129e292f94fc0329c309df49434 file1.txt
668ddeb108710d271ee21c0f3acbd6a7517e2b78f9181c6a2ff3b8943af92b0195dcb7cce48aa3e17893173c0a39e23d file2.txt
``sha512sum <file>...``
Create SHA512 checksum of files in ``sha512sum`` compatible format::
2a78d7a6c5328cfb1467c63beac8ff21794213901eaadafd48e7800289afbc08e5fb3e86aa31116c945ee3d7bf2a6194489ec6101051083d1108defc8e1dba89 file1.txt
7a0b54896fe5e70cca6dd643ad6f672614b189bf26f8153061c4d219474b05dad08c4e729af9f4b009f1a1a280cb625454bf587c690f4617c27e3aebdf3b7a2d file2.txt
``remove [-f] <file>...``
Remove the file(s). If any of the listed files already do not
exist, the command returns a non-zero exit code, but no message

View File

@ -0,0 +1,5 @@
cmake-command-mode-shasum
-------------------------
* Added sha1sum, sha224sum, sha256sum, sha384sum and sha512sum
as an equivalent to existing md5sum to cmake command mode.

View File

@ -3,7 +3,6 @@
#include "cmcmd.h"
#include "cmAlgorithms.h"
#include "cmCryptoHash.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
@ -89,6 +88,11 @@ void CMakeCommandUsage(const char* program)
<< " environment - display the current environment\n"
<< " make_directory <dir>... - create parent and <dir> directories\n"
<< " md5sum <file>... - create MD5 checksum of files\n"
<< " sha1sum <file>... - create SHA1 checksum of files\n"
<< " sha224sum <file>... - create SHA224 checksum of files\n"
<< " sha256sum <file>... - create SHA256 checksum of files\n"
<< " sha384sum <file>... - create SHA384 checksum of files\n"
<< " sha512sum <file>... - create SHA512 checksum of files\n"
<< " remove [-f] <file>... - remove the file(s), use -f to force "
"it\n"
<< " remove_directory dir - remove a directory and its contents\n"
@ -642,27 +646,28 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
// Command to calculate the md5sum of a file
if (args[1] == "md5sum" && args.size() >= 3) {
int retval = 0;
for (std::string::size_type cc = 2; cc < args.size(); cc++) {
const char* filename = args[cc].c_str();
// Cannot compute md5sum of a directory
if (cmSystemTools::FileIsDirectory(filename)) {
std::cerr << "Error: " << filename << " is a directory" << std::endl;
retval++;
} else {
std::string value =
cmSystemTools::ComputeFileHash(filename, cmCryptoHash::AlgoMD5);
if (value.empty()) {
// To mimic "md5sum" behavior in a shell:
std::cerr << filename << ": No such file or directory"
<< std::endl;
retval++;
} else {
std::cout << value << " " << filename << std::endl;
}
}
}
return retval;
return HashSumFile(args, cmCryptoHash::AlgoMD5);
}
// Command to calculate the sha1sum of a file
if (args[1] == "sha1sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA1);
}
if (args[1] == "sha224sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA224);
}
if (args[1] == "sha256sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA256);
}
if (args[1] == "sha384sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA384);
}
if (args[1] == "sha512sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA512);
}
// Command to change directory and run a program.
@ -1078,6 +1083,33 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
return 1;
}
int cmcmd::HashSumFile(std::vector<std::string>& args, cmCryptoHash::Algo algo)
{
if (args.size() < 3) {
return -1;
}
int retval = 0;
for (std::string::size_type cc = 2; cc < args.size(); cc++) {
const char* filename = args[cc].c_str();
// Cannot compute sum of a directory
if (cmSystemTools::FileIsDirectory(filename)) {
std::cerr << "Error: " << filename << " is a directory" << std::endl;
retval++;
} else {
std::string value = cmSystemTools::ComputeFileHash(filename, algo);
if (value.empty()) {
// To mimic "md5sum/shasum" behavior in a shell:
std::cerr << filename << ": No such file or directory" << std::endl;
retval++;
} else {
std::cout << value << " " << filename << std::endl;
}
}
}
return retval;
}
int cmcmd::SymlinkLibrary(std::vector<std::string>& args)
{
int result = 0;

View File

@ -4,6 +4,7 @@
#define cmcmd_h
#include "cmConfigure.h" // IWYU pragma: keep
#include "cmCryptoHash.h"
#include <string>
#include <vector>
@ -18,6 +19,8 @@ public:
static int ExecuteCMakeCommand(std::vector<std::string>&);
protected:
static int HashSumFile(std::vector<std::string>& args,
cmCryptoHash::Algo algo);
static int SymlinkLibrary(std::vector<std::string>& args);
static int SymlinkExecutable(std::vector<std::string>& args);
static bool SymlinkInternal(std::string const& file,

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
829c3804401b0727f70f73d4415e162400cbe57b ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
37d32c6dbabed711cb1d4620b64090fef0ef63ab16a4a51d668259e6 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
43c1835ceba2e29596f05e3859d4fe2b6d124a181ed670f68e914bd3ed251b02b4be609608a13f23ec3d98da6c4eb8cd ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
1692526aab84461a8aebcefddcba2b33fb5897ab180c53e8b345ae125484d0aaa35baf60487050be21ed8909a48eace93851bf139087ce1f7a87d97b6120a651 ../dummy

View File

@ -173,10 +173,27 @@ run_cmake_command(E_env-set ${CMAKE_COMMAND} -E env TEST_ENV=1 ${CMAKE_COMMAND
run_cmake_command(E_env-unset ${CMAKE_COMMAND} -E env TEST_ENV=1 ${CMAKE_COMMAND} -E env --unset=TEST_ENV ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/E_env-unset.cmake)
run_cmake_command(E_md5sum-dir ${CMAKE_COMMAND} -E md5sum .)
run_cmake_command(E_sha1sum-dir ${CMAKE_COMMAND} -E sha1sum .)
run_cmake_command(E_sha224sum-dir ${CMAKE_COMMAND} -E sha224sum .)
run_cmake_command(E_sha256sum-dir ${CMAKE_COMMAND} -E sha256sum .)
run_cmake_command(E_sha384sum-dir ${CMAKE_COMMAND} -E sha384sum .)
run_cmake_command(E_sha512sum-dir ${CMAKE_COMMAND} -E sha512sum .)
run_cmake_command(E_md5sum-no-file ${CMAKE_COMMAND} -E md5sum nonexisting)
run_cmake_command(E_sha1sum-no-file ${CMAKE_COMMAND} -E sha1sum nonexisting)
run_cmake_command(E_sha224sum-no-file ${CMAKE_COMMAND} -E sha224sum nonexisting)
run_cmake_command(E_sha256sum-no-file ${CMAKE_COMMAND} -E sha256sum nonexisting)
run_cmake_command(E_sha384sum-no-file ${CMAKE_COMMAND} -E sha384sum nonexisting)
run_cmake_command(E_sha512sum-no-file ${CMAKE_COMMAND} -E sha512sum nonexisting)
file(WRITE "${RunCMake_BINARY_DIR}/dummy" "dummy")
run_cmake_command(E_md5sum ${CMAKE_COMMAND} -E md5sum ../dummy)
run_cmake_command(E_md5sum-mixed ${CMAKE_COMMAND} -E md5sum . ../dummy nonexisting)
run_cmake_command(E_sha1sum ${CMAKE_COMMAND} -E sha1sum ../dummy)
run_cmake_command(E_sha224sum ${CMAKE_COMMAND} -E sha224sum ../dummy)
run_cmake_command(E_sha256sum ${CMAKE_COMMAND} -E sha256sum ../dummy)
run_cmake_command(E_sha384sum ${CMAKE_COMMAND} -E sha384sum ../dummy)
run_cmake_command(E_sha512sum ${CMAKE_COMMAND} -E sha512sum ../dummy)
file(REMOVE "${RunCMake_BINARY_DIR}/dummy")
set(RunCMake_DEFAULT_stderr ".")