OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/util/crypto_helpers.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/rand_util.h" |
| 14 #include "base/string_util.h" |
| 15 |
| 16 using std::string; |
| 17 using std::vector; |
| 18 |
| 19 MD5Calculator::MD5Calculator() { |
| 20 MD5Init(&context_); |
| 21 } |
| 22 |
| 23 void MD5Calculator::AddData(const unsigned char* data, int length) { |
| 24 CHECK(bin_digest_.empty()); |
| 25 MD5Update(&context_, data, length); |
| 26 } |
| 27 |
| 28 void MD5Calculator::CalcDigest() { |
| 29 if (bin_digest_.empty()) { |
| 30 MD5Digest digest; |
| 31 MD5Final(&digest, &context_); |
| 32 bin_digest_.assign(digest.a, digest.a + arraysize(digest.a)); |
| 33 } |
| 34 } |
| 35 |
| 36 vector<uint8> MD5Calculator::GetDigest() { |
| 37 CalcDigest(); |
| 38 return bin_digest_; |
| 39 } |
| 40 |
| 41 PathString MD5Calculator::GetHexDigest() { |
| 42 CalcDigest(); |
| 43 string hex = HexEncode(reinterpret_cast<char*>(&bin_digest_.front()), |
| 44 bin_digest_.size()); |
| 45 StringToLowerASCII(&hex); |
| 46 return PathString(hex.begin(), hex.end()); |
| 47 } |
| 48 |
| 49 void GetRandomBytes(char* output, int output_length) { |
| 50 for (int i = 0; i < output_length; i++) { |
| 51 // TODO(chron): replace this with something less stupid. |
| 52 output[i] = static_cast<char>(base::RandUint64()); |
| 53 } |
| 54 } |
| 55 |
| 56 string Generate128BitRandomHexString() { |
| 57 int64 chunk1 = static_cast<int64>(base::RandUint64()); |
| 58 int64 chunk2 = static_cast<int64>(base::RandUint64()); |
| 59 |
| 60 return StringPrintf("%016" PRId64 "x%016" PRId64 "x", |
| 61 chunk1, chunk2); |
| 62 } |
OLD | NEW |