Skip to content

Commit

Permalink
add sha512 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dreik committed Jun 17, 2023
1 parent d5a09ce commit 0bdc917
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2023-06-17 Paul Dreik <[email protected]>
* add sha512 support
2021-08-13 Paul Dreik <[email protected]>
* release 1.5.0
2021-08-12 Paul Dreik <[email protected]>
Expand Down
18 changes: 18 additions & 0 deletions Checksum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Checksum::Checksum(checksumtypes type)
case checksumtypes::SHA256: {
sha256_init(&m_state.sha256);
} break;
case checksumtypes::SHA512: {
sha512_init(&m_state.sha512);
} break;
case checksumtypes::MD5: {
md5_init(&m_state.md5);
} break;
Expand All @@ -44,6 +47,9 @@ Checksum::update(std::size_t length, const unsigned char* buffer)
case checksumtypes::SHA256:
sha256_update(&m_state.sha256, length, buffer);
break;
case checksumtypes::SHA512:
sha512_update(&m_state.sha512, length, buffer);
break;
case checksumtypes::MD5:
md5_update(&m_state.md5, length, buffer);
break;
Expand Down Expand Up @@ -107,6 +113,8 @@ Checksum::getDigestLength() const
return SHA1_DIGEST_SIZE;
case checksumtypes::SHA256:
return SHA256_DIGEST_SIZE;
case checksumtypes::SHA512:
return SHA512_DIGEST_SIZE;
case checksumtypes::MD5:
return MD5_DIGEST_SIZE;
default:
Expand Down Expand Up @@ -141,6 +149,16 @@ Checksum::printToBuffer(void* buffer, std::size_t N)
return -1;
}
break;
case checksumtypes::SHA512:
if (N >= SHA512_DIGEST_SIZE) {
sha512_digest(&m_state.sha512,
SHA512_DIGEST_SIZE,
static_cast<unsigned char*>(buffer));
} else {
// bad size.
return -1;
}
break;
case checksumtypes::MD5:
if (N >= MD5_DIGEST_SIZE) {
md5_digest(
Expand Down
2 changes: 2 additions & 0 deletions Checksum.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public:
MD5,
SHA1,
SHA256,
SHA512
};

explicit Checksum(checksumtypes type);
Expand Down Expand Up @@ -52,6 +53,7 @@ private:
{
sha1_ctx sha1;
sha256_ctx sha256;
sha512_ctx sha512;
md5_ctx md5;
} m_state;
};
Expand Down
3 changes: 3 additions & 0 deletions Fileinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ Fileinfo::fillwithbytes(enum readtobuffermode filltype,
case readtobuffermode::CREATE_SHA256_CHECKSUM:
checksumtype = Checksum::checksumtypes::SHA256;
break;
case readtobuffermode::CREATE_SHA512_CHECKSUM:
checksumtype = Checksum::checksumtypes::SHA512;
break;
default:
std::cerr << "does not know how to do that filltype:"
<< static_cast<long>(filltype) << std::endl;
Expand Down
1 change: 1 addition & 0 deletions Fileinfo.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public:
CREATE_MD5_CHECKSUM = 2,
CREATE_SHA1_CHECKSUM,
CREATE_SHA256_CHECKSUM,
CREATE_SHA512_CHECKSUM,
};

// type of duplicate
Expand Down
4 changes: 2 additions & 2 deletions rdfind.1
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Follow symlinks. Default is false.
Removes items found which have identical inode and device ID. Default
is true.
.TP
.BR \-checksum " " \fImd5\fR|\fIsha1\fR|\fIsha256\fR
What type of checksum to be used: md5, sha1 or sha256. The default is
.BR \-checksum " " \fImd5\fR|\fIsha1\fR|\fIsha256\fR|\fIsha512\fR
What type of checksum to be used: md5, sha1, sha256 or sha512. The default is
sha1 since version 1.4.0.
.TP
.BR \-deterministic " " \fItrue\fR|\fIfalse\fR
Expand Down
13 changes: 10 additions & 3 deletions rdfind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ usage()
<< " -followsymlinks true |(false) follow symlinks\n"
<< " -removeidentinode (true)| false ignore files with nonunique "
"device and inode\n"
<< " -checksum md5 |(sha1)| sha256\n"
<< " -checksum md5 |(sha1)| sha256 | sha512\n"
<< " checksum type\n"
<< " -deterministic (true)| false makes results independent of order\n"
<< " from listing the filesystem\n"
Expand Down Expand Up @@ -106,6 +106,7 @@ struct Options
bool usemd5 = false; // use md5 checksum to check for similarity
bool usesha1 = false; // use sha1 checksum to check for similarity
bool usesha256 = false; // use sha256 checksum to check for similarity
bool usesha512 = false; // use sha512 checksum to check for similarity
bool deterministic = true; // be independent of filesystem order
long nsecsleep = 0; // number of nanoseconds to sleep between each file read.
std::string resultsfile = "results.txt"; // results file name.
Expand Down Expand Up @@ -174,8 +175,10 @@ parseOptions(Parser& parser)
o.usesha1 = true;
} else if (parser.parsed_string_is("sha256")) {
o.usesha256 = true;
} else if (parser.parsed_string_is("sha512")) {
o.usesha512 = true;
} else {
std::cerr << "expected md5/sha1/sha256, not \""
std::cerr << "expected md5/sha1/sha256/sha512, not \""
<< parser.get_parsed_string() << "\"\n";
std::exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -237,7 +240,7 @@ parseOptions(Parser& parser)
// done with parsing of options. remaining arguments are files and dirs.

// decide what checksum to use - if no checksum is set, force sha1!
if (!o.usemd5 && !o.usesha1 && !o.usesha256) {
if (!o.usemd5 && !o.usesha1 && !o.usesha256 && !o.usesha512) {
o.usesha1 = true;
}
return o;
Expand Down Expand Up @@ -368,6 +371,10 @@ main(int narg, const char* argv[])
modes.emplace_back(Fileinfo::readtobuffermode::CREATE_SHA256_CHECKSUM,
"sha256 checksum");
}
if (o.usesha512) {
modes.emplace_back(Fileinfo::readtobuffermode::CREATE_SHA512_CHECKSUM,
"sha512 checksum");
}

for (auto it = modes.begin() + 1; it != modes.end(); ++it) {
std::cout << dryruntext << "Now eliminating candidates based on "
Expand Down
2 changes: 1 addition & 1 deletion testcases/checksum_options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -e



for checksumtype in md5 sha1 sha256; do
for checksumtype in md5 sha1 sha256 sha512; do
reset_teststate
dbgecho "trying checksum $checksumtype"
echo checksumtest >a
Expand Down

0 comments on commit 0bdc917

Please sign in to comment.