3
3
#include < md5.h>
4
4
#include < iostream>
5
5
#include < cstring>
6
+ #include < fstream>
6
7
7
8
// ---------------------------------------------------------------------
9
+ // WsjcppHashes
8
10
9
- std::string WsjcppHashes::sha1_calc_hex (const std::string &sSource ) {
11
+ std::string WsjcppHashes::sha1_calc_hex (const std::string &sSource ) { // deprecated
10
12
char hexstring[41 ]; // 40 chars + a zero
11
13
std::memset (hexstring, 0 , sizeof hexstring);
12
14
@@ -16,11 +18,81 @@ std::string WsjcppHashes::sha1_calc_hex(const std::string &sSource) {
16
18
return std::string (hexstring);
17
19
}
18
20
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
22
22
MD5 md5 = MD5 (sSource );
23
23
return md5.hexdigest ();
24
24
}
25
25
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
+
0 commit comments