Skip to content

Commit 264c5bb

Browse files
committed
Update to SHA256
1 parent d2c06c1 commit 264c5bb

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

Unix/tools/changes/changes.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#include <dirent.h>
1818
#include <algorithm>
1919
#include <sys/stat.h>
20-
#include <openssl/md5.h>
20+
#include <openssl/sha.h>
2121

2222
using namespace std;
2323

2424
const char* arg0;
2525

26-
// Key=path, Value=MD5:
26+
// Key=path, Value=SHA256:
2727
typedef pair<string, string> Pair;
2828
typedef map<string, string> Map;
2929

@@ -64,27 +64,26 @@ static bool LoadFile(
6464
return true;
6565
}
6666

67-
static bool FindMD5(
67+
static bool FindSHA(
6868
const char* path,
69-
string& md5)
69+
string& sha256)
7070
{
7171
vector<char> data;
7272

7373
if (!LoadFile(path, data))
7474
return false;
7575

7676
{
77-
MD5_CTX ctx;
78-
MD5_Init(&ctx);
79-
MD5_Update(&ctx, &data[0], data.size());
80-
unsigned char digest[MD5_DIGEST_LENGTH];
81-
MD5_Final(digest, &ctx);
82-
83-
for (size_t i = 0; i < MD5_DIGEST_LENGTH; i++)
77+
SHA256_CTX ctx;
78+
SHA256_Init(&ctx);
79+
SHA256_Update(&ctx, &data[0], data.size());
80+
unsigned char digest[SHA256_DIGEST_LENGTH];
81+
SHA256_Final(digest, &ctx);
82+
for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++)
8483
{
8584
char buf[3];
8685
sprintf(buf, "%02X", digest[i]);
87-
md5 += buf;
86+
sha256 += buf;
8887
}
8988
}
9089

@@ -142,20 +141,20 @@ static void Scan(
142141
{
143142
dirs.push_back(path);
144143

145-
string md5 = "00000000000000000000000000000000";
146-
files.insert(Pair(path, md5));
144+
string sha256 = "0000000000000000000000000000000000000000000000000000000000000000";
145+
files.insert(Pair(path, sha256));
147146
}
148147
else
149148
{
150-
string md5;
149+
string sha256;
151150

152-
if (!FindMD5(path.c_str(), md5))
151+
if (!FindSHA(path.c_str(), sha256))
153152
{
154-
fprintf(stderr, "%s: failed to find MD5: %s\n",
153+
fprintf(stderr, "%s: failed to find SHA256: %s\n",
155154
arg0, path.c_str());
156155
}
157156

158-
files.insert(Pair(path, md5));
157+
files.insert(Pair(path, sha256));
159158
}
160159
}
161160

@@ -198,9 +197,9 @@ static bool WriteChangesFile(
198197
while (p != end)
199198
{
200199
string path = (*p).first;
201-
string md5 = (*p).second;
200+
string sha256 = (*p).second;
202201

203-
fprintf(os, "%s:%s\n", md5.c_str(), path.c_str());
202+
fprintf(os, "%s:%s\n", sha256.c_str(), path.c_str());
204203
p++;
205204
}
206205

@@ -234,15 +233,15 @@ static bool LoadChangesFile(
234233
return false;
235234
}
236235

237-
string md5 = string(buf, colon - buf);
236+
string sha256 = string(buf, colon - buf);
238237
string path = string(colon + 1);
239238

240239
#if 0
241-
printf("md5{%s}\n", md5.c_str());
240+
printf("sha256{%s}\n", sha256.c_str());
242241
printf("path{%s}\n", path.c_str());
243242
#endif
244243

245-
files.insert(Pair(path, md5));
244+
files.insert(Pair(path, sha256));
246245
}
247246

248247
fclose(is);
@@ -288,10 +287,10 @@ void DiffChanges(
288287
}
289288
else
290289
{
291-
string md5a = (*p).second;
292-
string md5b = (*pos).second;
290+
string sha256a = (*p).second;
291+
string sha256b = (*pos).second;
293292

294-
if (md5a != md5b)
293+
if (sha256a != sha256b)
295294
{
296295
Change tmp;
297296
tmp.type = 'M';

0 commit comments

Comments
 (0)