Skip to content

Commit d6298ee

Browse files
nhatnghihojustsmth
andauthored
Add sha1 CLI (#2885)
### Description of changes: Expose `openssl sha1` as a CLI command. ### Call-outs: Relies on `dgstTool` so only minimal code change is needed. ### Testing: Unit tests By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. Co-authored-by: Justin W Smith <[email protected]>
1 parent 745d009 commit d6298ee

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

tool-openssl/dgst.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,6 @@ bool dgstTool(const args_list_t &args) {
511511
bool md5Tool(const args_list_t &args) {
512512
return dgstToolInternal(args, EVP_md5());
513513
}
514+
bool sha1Tool(const args_list_t &args) {
515+
return dgstToolInternal(args, EVP_sha1());
516+
}

tool-openssl/dgst_test.cc

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,47 @@ TEST_F(DgstComparisonTest, SignAndVerify) {
246246
EXPECT_EQ(awslc_hash, openssl_hash);
247247
}
248248

249+
class MD5ComparisonTest : public DgstComparisonTest {};
250+
251+
TEST_F(MD5ComparisonTest, Digest) {
252+
// default digest
253+
std::string awslc_command = std::string(awslc_executable_path) +
254+
" md5 -out " + out_path_awslc + " " + in_path;
255+
256+
std::string openssl_command = std::string(openssl_executable_path) +
257+
" md5 -out " + out_path_openssl + " " + in_path;
258+
259+
RunCommandsAndCompareOutput(awslc_command, openssl_command, out_path_awslc,
260+
out_path_openssl, awslc_output_str,
261+
openssl_output_str);
262+
263+
std::string awslc_hash = GetHash(awslc_output_str);
264+
std::string openssl_hash = GetHash(openssl_output_str);
265+
266+
EXPECT_EQ(awslc_hash, openssl_hash);
267+
}
268+
269+
class SHA1ComparisonTest : public DgstComparisonTest {};
270+
271+
TEST_F(SHA1ComparisonTest, Digest) {
272+
// default digest
273+
std::string awslc_command = std::string(awslc_executable_path) +
274+
" sha1 -out " + out_path_awslc + " " + in_path;
275+
276+
std::string openssl_command = std::string(openssl_executable_path) +
277+
" sha1 -out " + out_path_openssl + " " +
278+
in_path;
279+
280+
RunCommandsAndCompareOutput(awslc_command, openssl_command, out_path_awslc,
281+
out_path_openssl, awslc_output_str,
282+
openssl_output_str);
283+
284+
std::string awslc_hash = GetHash(awslc_output_str);
285+
std::string openssl_hash = GetHash(openssl_output_str);
286+
287+
EXPECT_EQ(awslc_hash, openssl_hash);
288+
}
289+
249290
class DgstTest : public ::testing::Test {
250291
protected:
251292
void SetUp() override {
@@ -330,7 +371,7 @@ TEST_F(DgstTest, DigestDefault) {
330371
EXPECT_TRUE(dgstTool(args));
331372
}
332373

333-
TEST_F(DgstTest, DigestSHA1) {
374+
TEST_F(DgstTest, CustomDigest) {
334375
args_list_t args = {"-sha1", in_path};
335376
EXPECT_TRUE(dgstTool(args));
336377
}
@@ -404,3 +445,17 @@ TEST_F(DgstTest, PassinBasicIntegrationTest) {
404445
bool result = dgstTool(args);
405446
ASSERT_TRUE(result);
406447
}
448+
449+
class MD5Test : public DgstTest {};
450+
451+
TEST_F(MD5Test, Sign) {
452+
args_list_t args = {in_path};
453+
EXPECT_TRUE(md5Tool(args));
454+
}
455+
456+
class SHA1Test : public DgstTest {};
457+
458+
TEST_F(SHA1Test, Sign) {
459+
args_list_t args = {in_path};
460+
EXPECT_TRUE(sha1Tool(args));
461+
}

tool-openssl/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ bool pkeyutlTool(const args_list_t &args);
9999
bool RehashTool(const args_list_t &args);
100100
bool reqTool(const args_list_t &args);
101101
bool rsaTool(const args_list_t &args);
102+
bool sha1Tool(const args_list_t &args);
102103
bool SClientTool(const args_list_t &args);
103104
bool VerifyTool(const args_list_t &args);
104105
bool VersionTool(const args_list_t &args);

tool-openssl/tool.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "./internal.h"
1717

18-
static const std::array<Tool, 17> kTools = {{
18+
static const std::array<Tool, 18> kTools = {{
1919
{"crl", CRLTool},
2020
{"dgst", dgstTool},
2121
{"dhparam", dhparamTool},
@@ -29,6 +29,7 @@ static const std::array<Tool, 17> kTools = {{
2929
{"rehash", RehashTool},
3030
{"req", reqTool},
3131
{"rsa", rsaTool},
32+
{"sha1", sha1Tool},
3233
{"s_client", SClientTool},
3334
{"verify", VerifyTool},
3435
{"version", VersionTool},

0 commit comments

Comments
 (0)