Skip to content

Commit 52b6055

Browse files
authored
Merge pull request #5 from wsjcpp/version-0.1.3
Version 0.1.3
2 parents d1dd98d + 961dba7 commit 52b6055

13 files changed

+368
-16
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to wsjcpp-hashes project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [v0.1.3] - 2020-09-12 (2020 Sep 12)
9+
10+
### Added
11+
12+
- added `WsjcppHashes::getMd5ByString("some string")`
13+
- added `WsjcppHashes::getMd5ByFile("path to file")`
14+
- added `WsjcppHashes::getSha1ByString("some string")`
15+
- added `WsjcppHashes::getSha1ByFile("path to file")`
16+
817
## [v0.1.2] - 2020-09-12 (2020 Sep 12)
918

1019
### Security

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# wsjcpp-hashes
22

3-
[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-hashes.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-hashes) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-hashes.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-hashes/stargazers) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-hashes.svg)](https://github.com/wsjcpp/wsjcpp-hashes/) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-hashes.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-hashes/network/members) [![Total alerts](https://img.shields.io/lgtm/alerts/g/wsjcpp/wsjcpp-hashes.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-hashes/alerts/) [![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/wsjcpp/wsjcpp-hashes.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-hashes/context:cpp)
3+
[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-hashes.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-hashes) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-hashes.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-hashes/stargazers) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-hashes.svg)](https://github.com/wsjcpp/wsjcpp-hashes/) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-hashes.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-hashes/network/members) [![Total alerts](https://img.shields.io/lgtm/alerts/g/wsjcpp/wsjcpp-hashes.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-hashes/alerts/) [![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/wsjcpp/wsjcpp-hashes.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-hashes/context:cpp) [![deepcode](https://www.deepcode.ai/api/gh/badge?key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwbGF0Zm9ybTEiOiJnaCIsIm93bmVyMSI6IndzamNwcCIsInJlcG8xIjoid3NqY3BwLWhhc2hlcyIsImluY2x1ZGVMaW50IjpmYWxzZSwiYXV0aG9ySWQiOjE1NjQxLCJpYXQiOjE2MTgyODE3NTN9.aIkvF2elMfDPAioHW6xLwSjJS_FGHpjOKMnBcgdsB-U)](https://www.deepcode.ai/app/gh/wsjcpp/wsjcpp-hashes/_/dashboard?utm_content=gh%2Fwsjcpp%2Fwsjcpp-hashes)
44

55

66
## Intagrate to your project

src.wsjcpp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by [email protected]
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_APP_VERSION="v0.1.2")
4+
add_definitions(-DWSJCPP_APP_VERSION="v0.1.3")
55
add_definitions(-DWSJCPP_APP_NAME="wsjcpp-hashes")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/wsjcpp_hashes.cpp

+77-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <md5.h>
44
#include <iostream>
55
#include <cstring>
6+
#include <fstream>
67

78
// ---------------------------------------------------------------------
9+
// WsjcppHashes
810

9-
std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) {
11+
std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) { // deprecated
1012
char hexstring[41]; // 40 chars + a zero
1113
std::memset(hexstring, 0, sizeof hexstring);
1214

@@ -16,11 +18,81 @@ std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) {
1618
return std::string(hexstring);
1719
}
1820

19-
// ---------------------------------------------------------------------
20-
21-
std::string WsjcppHashes::md5_calc_hex(const std::string &sSource) {
21+
std::string WsjcppHashes::md5_calc_hex(const std::string &sSource) { // deprecated
2222
MD5 md5 = MD5(sSource);
2323
return md5.hexdigest();
2424
}
2525

26-
// ---------------------------------------------------------------------
26+
std::string WsjcppHashes::getMd5ByString(const std::string &sStr) {
27+
MD5 md5 = MD5(sStr);
28+
return md5.hexdigest();
29+
}
30+
31+
std::string WsjcppHashes::getMd5ByFile(const std::string &sFilename) {
32+
std::ifstream f(sFilename, std::ifstream::binary);
33+
if (!f) {
34+
return "Could not open file";
35+
}
36+
37+
// get length of file:
38+
f.seekg (0, f.end);
39+
int nBufferSize = f.tellg();
40+
f.seekg (0, f.beg);
41+
42+
char *pBuffer = new char [nBufferSize];
43+
44+
// read data as a block:
45+
f.read(pBuffer, nBufferSize);
46+
if (!f) {
47+
delete[] pBuffer;
48+
// f.close();
49+
return "Could not read file. Only " + std::to_string(f.gcount()) + " could be read";
50+
}
51+
f.close();
52+
53+
MD5 md5;
54+
md5.update(pBuffer, nBufferSize);
55+
md5.finalize();
56+
return md5.hexdigest();
57+
}
58+
59+
std::string WsjcppHashes::getSha1ByString(const std::string &sStr) {
60+
char hexstring[41]; // 40 chars + a zero
61+
std::memset(hexstring, 0, sizeof hexstring);
62+
63+
unsigned char hash[20];
64+
sha1::calc(sStr.c_str(), sStr.length(), hash);
65+
sha1::toHexString(hash, hexstring);
66+
return std::string(hexstring);
67+
}
68+
69+
std::string WsjcppHashes::getSha1ByFile(const std::string &sFilename) {
70+
std::ifstream f(sFilename, std::ifstream::binary);
71+
if (!f) {
72+
return "Could not open file";
73+
}
74+
75+
// get length of file:
76+
f.seekg (0, f.end);
77+
int nBufferSize = f.tellg();
78+
f.seekg (0, f.beg);
79+
80+
char *pBuffer = new char [nBufferSize];
81+
82+
// read data as a block:
83+
f.read(pBuffer, nBufferSize);
84+
if (!f) {
85+
delete[] pBuffer;
86+
// f.close();
87+
return "Could not read file. Only " + std::to_string(f.gcount()) + " could be read";
88+
}
89+
f.close();
90+
91+
char hexstring[41]; // 40 chars + a zero
92+
std::memset(hexstring, 0, sizeof hexstring);
93+
unsigned char hash[20];
94+
sha1::calc(pBuffer, nBufferSize, hash);
95+
sha1::toHexString(hash, hexstring);
96+
return std::string(hexstring);
97+
}
98+

src/wsjcpp_hashes.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
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 getMd5ByFile(const std::string &sFilename);
13+
14+
static std::string getSha1ByString(const std::string &sStr);
15+
static std::string getSha1ByFile(const std::string &sFilename);
16+
1017
};
1118

1219
#endif // WSJCPP_HASHES_H

unit-tests.wsjcpp/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
5-
add_definitions(-DWSJCPP_APP_VERSION="ut-v0.1.2")
5+
add_definitions(-DWSJCPP_APP_VERSION="ut-v0.1.3")
66
add_definitions(-DWSJCPP_APP_NAME="unit-tests-wsjcpp-hashes")
77

88
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -27,7 +27,7 @@ list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp")
2727
list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h")
2828
list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp")
2929

30-
# wsjcpp-hashes:v0.1.2
30+
# wsjcpp-hashes:v0.1.3
3131
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
3232
list (APPEND WSJCPP_SOURCES "../src/md5.cpp")
3333
list (APPEND WSJCPP_SOURCES "../src/md5.h")
@@ -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

+5
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

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

0 commit comments

Comments
 (0)