Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 1 | // Copyright 2012 The Chromium OS Authors. All rights reserved. |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 5 | #include "third_party/chromium/crypto/sha2.h" |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 6 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <openssl/sha.h> |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 9 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 10 | #include <base/memory/scoped_ptr.h> |
| 11 | |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 12 | namespace crypto { |
| 13 | |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 14 | void SHA256HashString(const std::string& str, uint8_t* output, size_t len) { |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 15 | std::string hash = SHA256HashString(str); |
| 16 | len = std::min(hash.size(), len); |
| 17 | std::copy(hash.begin(), hash.begin() + len, output); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 20 | std::string SHA256HashString(const std::string& str) { |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 21 | SHA256_CTX sha_context; |
| 22 | SHA256_Init(&sha_context); |
| 23 | SHA256_Update(&sha_context, str.data(), str.size()); |
| 24 | |
| 25 | std::string hash(kSHA256Length, 0); |
| 26 | SHA256_Final(reinterpret_cast<uint8_t*>(&hash[0]), &sha_context); |
| 27 | return hash; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | } // namespace crypto |