Skip to content

Commit 016b498

Browse files
committed
Prepare getMd5ByFile / getSha1ByFile
1 parent c542b4d commit 016b498

File tree

10 files changed

+302
-10
lines changed

10 files changed

+302
-10
lines changed

src/wsjcpp_hashes.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include <cstring>
66

77
// ---------------------------------------------------------------------
8+
// WsjcppHashes
89

9-
std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) {
10+
std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) { // deprecated
1011
char hexstring[41]; // 40 chars + a zero
1112
std::memset(hexstring, 0, sizeof hexstring);
1213

@@ -16,11 +17,31 @@ std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) {
1617
return std::string(hexstring);
1718
}
1819

19-
// ---------------------------------------------------------------------
20-
21-
std::string WsjcppHashes::md5_calc_hex(const std::string &sSource) {
20+
std::string WsjcppHashes::md5_calc_hex(const std::string &sSource) { // deprecated
2221
MD5 md5 = MD5(sSource);
2322
return md5.hexdigest();
2423
}
2524

26-
// ---------------------------------------------------------------------
25+
std::string WsjcppHashes::getMd5ByString(const std::string &sStr) {
26+
MD5 md5 = MD5(sStr);
27+
return md5.hexdigest();
28+
}
29+
30+
std::string WsjcppHashes::getSha1ByString(const std::string &sStr) {
31+
char hexstring[41]; // 40 chars + a zero
32+
std::memset(hexstring, 0, sizeof hexstring);
33+
34+
unsigned char hash[20];
35+
sha1::calc(sStr.c_str(), sStr.length(), hash);
36+
sha1::toHexString(hash, hexstring);
37+
return std::string(hexstring);
38+
}
39+
40+
std::string WsjcppHashes::getMd5ByFile(const std::string &sFilename) {
41+
return "todo";
42+
}
43+
44+
std::string WsjcppHashes::getSha1ByFile(const std::string &sFilename) {
45+
return "todo";
46+
}
47+

src/wsjcpp_hashes.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55

66
class WsjcppHashes {
77
public:
8-
static std::string sha1_calc_hex(const std::string &sSource);
9-
static std::string md5_calc_hex(const std::string &sSource);
8+
static std::string sha1_calc_hex(const std::string &sSource); // deprecated
9+
static std::string md5_calc_hex(const std::string &sSource); // deprecated
10+
11+
static std::string getMd5ByString(const std::string &sStr);
12+
static std::string getSha1ByString(const std::string &sStr);
13+
static std::string getMd5ByFile(const std::string &sFilename);
14+
static std::string getSha1ByFile(const std::string &sFilename);
15+
1016
};
1117

1218
#endif // WSJCPP_HASHES_H

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ list (APPEND WSJCPP_SOURCES "../src/wsjcpp_hashes.h")
4040
list (APPEND WSJCPP_INCLUDE_DIRS "src")
4141
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_md5.cpp")
4242
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_sha1.cpp")
43+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_md5_file.cpp")
44+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_sha1_file.cpp")
45+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_sha1_string.cpp")
46+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_md5_string.cpp")
4347

4448
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
4549

unit-tests.wsjcpp/data/test_file0

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
some some
2+
some some
3+
some some
4+
some some
5+
some some

unit-tests.wsjcpp/data/test_file1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
some some
2+
some some
3+
some some
4+
some some
5+
some some
6+
some some
7+
some some
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_hashes.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestGetMd5File
8+
9+
class UnitTestGetMd5File : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestGetMd5File();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestGetMd5File)
18+
19+
UnitTestGetMd5File::UnitTestGetMd5File()
20+
: WsjcppUnitTestBase("UnitTestGetMd5File") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestGetMd5File::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestGetMd5File::executeTest() {
33+
struct Md5Test {
34+
Md5Test(std::string sFile, std::string sExpectedMd5) : sFile(sFile), sExpectedMd5(sExpectedMd5) {}
35+
std::string sFile;
36+
std::string sExpectedMd5;
37+
};
38+
39+
std::vector<Md5Test *> tests;
40+
tests.push_back(new Md5Test("data/test_file0", "2ceadcd1a95ddea67516c36fc75a5218"));
41+
tests.push_back(new Md5Test("data/test_file1", "d2066ac59c5a0a153836c2a9c1fb1901"));
42+
43+
unsigned int nSuccess = 0;
44+
for (unsigned int i = 0; i < tests.size(); i++) {
45+
std::string sFile = tests[i]->sFile;
46+
std::string sExpectedMd5 = tests[i]->sExpectedMd5;
47+
std::string sGotMd5 = WsjcppHashes::getMd5ByFile(sFile);
48+
sExpectedMd5 = WsjcppCore::toLower(sExpectedMd5);
49+
sGotMd5 = WsjcppCore::toLower(sGotMd5);
50+
compare("text '" + sFile + "'", sGotMd5, sExpectedMd5);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestGetMd5File::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_hashes.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestGetMd5String
8+
9+
class UnitTestGetMd5String : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestGetMd5String();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestGetMd5String)
18+
19+
UnitTestGetMd5String::UnitTestGetMd5String()
20+
: WsjcppUnitTestBase("UnitTestGetMd5String") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestGetMd5String::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestGetMd5String::executeTest() {
33+
struct Md5Test {
34+
Md5Test(std::string sOrig, std::string sExpectedMd5) : sOrig(sOrig), sExpectedMd5(sExpectedMd5) {}
35+
std::string sOrig;
36+
std::string sExpectedMd5;
37+
};
38+
39+
std::vector<Md5Test *> tests;
40+
tests.push_back(new Md5Test("test", "098F6BCD4621D373CADE4E832627B4F6"));
41+
tests.push_back(new Md5Test("admin", "21232F297A57A5A743894A0E4A801FC3"));
42+
43+
unsigned int nSuccess = 0;
44+
for (unsigned int i = 0; i < tests.size(); i++) {
45+
std::string sOrig = tests[i]->sOrig;
46+
std::string sExpectedMd5 = tests[i]->sExpectedMd5;
47+
std::string sGotMd5 = WsjcppHashes::getMd5ByString(sOrig);
48+
sExpectedMd5 = WsjcppCore::toLower(sExpectedMd5);
49+
sGotMd5 = WsjcppCore::toLower(sGotMd5);
50+
compare("text '" + sOrig + "'", sGotMd5, sExpectedMd5);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestGetMd5String::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_hashes.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestGetSha1File
8+
9+
class UnitTestGetSha1File : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestGetSha1File();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestGetSha1File)
18+
19+
UnitTestGetSha1File::UnitTestGetSha1File()
20+
: WsjcppUnitTestBase("UnitTestGetSha1File") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestGetSha1File::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestGetSha1File::executeTest() {
33+
struct Sha1Test {
34+
Sha1Test(std::string sFile, std::string sExpectedSha1) : sFile(sFile), sExpectedSha1(sExpectedSha1) {}
35+
std::string sFile;
36+
std::string sExpectedSha1;
37+
};
38+
39+
std::vector<Sha1Test *> tests;
40+
tests.push_back(new Sha1Test("data/test_file0", "7093e61cecfcf8ae864d1c56a52f6d04f6f4e94c"));
41+
tests.push_back(new Sha1Test("data/test_file1", "899cb945a21da884413a5e952650ae35c4ad06d8"));
42+
43+
unsigned int nSuccess = 0;
44+
for (unsigned int i = 0; i < tests.size(); i++) {
45+
std::string sOriginal = tests[i]->sFile;
46+
std::string sExpectedSha1 = tests[i]->sExpectedSha1;
47+
std::string sGotSha1 = WsjcppHashes::getSha1ByFile(sOriginal);
48+
sExpectedSha1 = WsjcppCore::toLower(sExpectedSha1);
49+
sGotSha1 = WsjcppCore::toLower(sGotSha1);
50+
compare("text '" + sOriginal + "'", sGotSha1, sExpectedSha1);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestGetSha1File::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_hashes.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestGetSha1String
8+
9+
class UnitTestGetSha1String : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestGetSha1String();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestGetSha1String)
18+
19+
UnitTestGetSha1String::UnitTestGetSha1String()
20+
: WsjcppUnitTestBase("UnitTestGetSha1String") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestGetSha1String::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestGetSha1String::executeTest() {
33+
struct Sha1Test {
34+
Sha1Test(std::string sOrig, std::string sExpectedSha1) : sOrig(sOrig), sExpectedSha1(sExpectedSha1) {}
35+
std::string sOrig;
36+
std::string sExpectedSha1;
37+
};
38+
39+
std::vector<Sha1Test *> tests;
40+
tests.push_back(new Sha1Test("test", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"));
41+
tests.push_back(new Sha1Test("admin", "d033e22ae348aeb5660fc2140aec35850c4da997"));
42+
43+
unsigned int nSuccess = 0;
44+
for (unsigned int i = 0; i < tests.size(); i++) {
45+
std::string sOriginal = tests[i]->sOrig;
46+
std::string sExpectedSha1 = tests[i]->sExpectedSha1;
47+
std::string sGotSha1 = WsjcppHashes::getSha1ByString(sOriginal);
48+
sExpectedSha1 = WsjcppCore::toLower(sExpectedSha1);
49+
sGotSha1 = WsjcppCore::toLower(sGotSha1);
50+
compare("text '" + sOriginal + "'", sGotSha1, sExpectedSha1);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestGetSha1String::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+

wsjcpp.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ authors:
2727
# wrapper
2828
- name: Evgenii Sopov
2929
30-
3130
dependencies:
3231
- name: "wsjcpp-core"
3332
version: "v0.2.1"
3433
url: "https://github.com/wsjcpp/wsjcpp-core:master"
3534
origin: "https://github.com/"
3635
installation-dir: "./src.wsjcpp/wsjcpp_core"
37-
3836
distribution:
3937
- source-file: src/md5.cpp
4038
target-file: md5.cpp
@@ -54,10 +52,17 @@ distribution:
5452
- source-file: src/wsjcpp_hashes.h
5553
target-file: wsjcpp_hashes.h
5654
type: "source-code"
57-
5855
unit-tests:
5956
cases:
6057
- name: "Md5"
6158
description: "check md5"
6259
- name: "Sha1"
6360
description: "check sha1"
61+
- name: "GetMd5File"
62+
description: ""
63+
- name: "GetSha1File"
64+
description: ""
65+
- name: "GetSha1String"
66+
description: ""
67+
- name: "GetMd5String"
68+
description: ""

0 commit comments

Comments
 (0)