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 <string.h> |
| 8 |
7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
8 | 10 |
9 namespace base { | 11 namespace base { |
10 | 12 |
11 // Implementation of SHA-1. Only handles data in byte-sized blocks, | 13 // Implementation of SHA-1. Only handles data in byte-sized blocks, |
12 // which simplifies the code a fair bit. | 14 // which simplifies the code a fair bit. |
13 | 15 |
14 // Identifier names follow notation in FIPS PUB 180-3, where you'll | 16 // Identifier names follow notation in FIPS PUB 180-3, where you'll |
15 // also find a description of the algorithm: | 17 // also find a description of the algorithm: |
16 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf | 18 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 H[0] += A; | 190 H[0] += A; |
189 H[1] += B; | 191 H[1] += B; |
190 H[2] += C; | 192 H[2] += C; |
191 H[3] += D; | 193 H[3] += D; |
192 H[4] += E; | 194 H[4] += E; |
193 | 195 |
194 cursor = 0; | 196 cursor = 0; |
195 } | 197 } |
196 | 198 |
197 std::string SHA1HashString(const std::string& str) { | 199 std::string SHA1HashString(const std::string& str) { |
| 200 char hash[SecureHashAlgorithm::kDigestSizeBytes]; |
| 201 SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), |
| 202 str.length(), reinterpret_cast<unsigned char*>(hash)); |
| 203 return std::string(hash, SecureHashAlgorithm::kDigestSizeBytes); |
| 204 } |
| 205 |
| 206 void SHA1HashBytes(const unsigned char* data, size_t len, |
| 207 unsigned char* hash) { |
198 SecureHashAlgorithm sha; | 208 SecureHashAlgorithm sha; |
199 sha.Update(str.c_str(), str.length()); | 209 sha.Update(data, len); |
200 sha.Final(); | 210 sha.Final(); |
201 std::string out(reinterpret_cast<const char*>(sha.Digest()), | 211 |
202 SecureHashAlgorithm::kDigestSizeBytes); | 212 memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
203 return out; | |
204 } | 213 } |
205 | 214 |
206 } // namespace base | 215 } // namespace base |
OLD | NEW |