Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1812)

Unified Diff: base/sha1_portable.cc

Issue 1449693002: Updating Mojo/Base's Sha1 functionality to hash entire files. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/sha1.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/sha1_portable.cc
diff --git a/base/sha1_portable.cc b/base/sha1_portable.cc
index 0b9df830797c6f5de28b587f0ccafc9168b7eedf..e653843452467485fe6d2442283ee44ceee59b0f 100644
--- a/base/sha1_portable.cc
+++ b/base/sha1_portable.cc
@@ -4,9 +4,11 @@
#include "base/sha1.h"
+#include <stdio.h>
#include <string.h>
#include "base/basictypes.h"
+#include "base/files/scoped_file.h"
namespace base {
@@ -213,4 +215,27 @@ void SHA1HashBytes(const unsigned char* data, size_t len,
memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes);
}
+bool SHA1HashFile(const std::string& file_path, unsigned char* hash) {
+ SecureHashAlgorithm sha;
+ base::ScopedFILE file(fopen(file_path.c_str(), "rb"));
+ if (!file) {
+ LOG(ERROR) << "Could not open file " << file_path;
+ return false;
+ }
+ const size_t kBufferSize = 1 << 16;
+ scoped_ptr<char[]> buf(new char[kBufferSize]);
+ size_t len;
+ while ((len = fread(buf.get(), 1, kBufferSize, file.get())) > 0)
+ sha.Update(buf.get(), len);
+ if (ferror(file.get())) {
+ LOG(ERROR) << "Error reading file " << file_path;
+ return false;
+ }
+
+ sha.Final();
+
+ memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes);
+ return true;
+}
+
} // namespace base
« no previous file with comments | « base/sha1.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698