OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sha1.h" | 5 #include "base/sha1.h" |
6 | 6 |
| 7 #include <stdio.h> |
7 #include <string.h> | 8 #include <string.h> |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/files/scoped_file.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
13 // Implementation of SHA-1. Only handles data in byte-sized blocks, | 15 // Implementation of SHA-1. Only handles data in byte-sized blocks, |
14 // which simplifies the code a fair bit. | 16 // which simplifies the code a fair bit. |
15 | 17 |
16 // Identifier names follow notation in FIPS PUB 180-3, where you'll | 18 // Identifier names follow notation in FIPS PUB 180-3, where you'll |
17 // also find a description of the algorithm: | 19 // also find a description of the algorithm: |
18 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf | 20 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf |
19 | 21 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 208 |
207 void SHA1HashBytes(const unsigned char* data, size_t len, | 209 void SHA1HashBytes(const unsigned char* data, size_t len, |
208 unsigned char* hash) { | 210 unsigned char* hash) { |
209 SecureHashAlgorithm sha; | 211 SecureHashAlgorithm sha; |
210 sha.Update(data, len); | 212 sha.Update(data, len); |
211 sha.Final(); | 213 sha.Final(); |
212 | 214 |
213 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); | 215 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
214 } | 216 } |
215 | 217 |
| 218 bool SHA1HashFile(const std::string& file_path, unsigned char* hash) { |
| 219 SecureHashAlgorithm sha; |
| 220 base::ScopedFILE file(fopen(file_path.c_str(), "rb")); |
| 221 if (!file) { |
| 222 LOG(ERROR) << "Could not open file " << file_path; |
| 223 return false; |
| 224 } |
| 225 const size_t kBufferSize = 1 << 16; |
| 226 scoped_ptr<char[]> buf(new char[kBufferSize]); |
| 227 size_t len; |
| 228 while ((len = fread(buf.get(), 1, kBufferSize, file.get())) > 0) |
| 229 sha.Update(buf.get(), len); |
| 230 if (ferror(file.get())) { |
| 231 LOG(ERROR) << "Error reading file " << file_path; |
| 232 return false; |
| 233 } |
| 234 |
| 235 sha.Final(); |
| 236 |
| 237 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
| 238 return true; |
| 239 } |
| 240 |
216 } // namespace base | 241 } // namespace base |
OLD | NEW |