blob: 7dcef0b09125fad1a2b41365250ec22e5ab2e731 [file] [log] [blame]
Vitaly Buka9ba72a82015-08-06 17:36:17 -07001// Copyright 2012 The Chromium OS Authors. All rights reserved.
Vitaly Buka6ca6a232015-08-06 17:32:43 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka9e5b6832015-10-14 15:57:14 -07005#include "third_party/chromium/crypto/sha2.h"
Vitaly Buka6ca6a232015-08-06 17:32:43 -07006
Vitaly Buka9ba72a82015-08-06 17:36:17 -07007#include <algorithm>
8#include <openssl/sha.h>
Vitaly Buka6ca6a232015-08-06 17:32:43 -07009
Vitaly Buka9ba72a82015-08-06 17:36:17 -070010#include <base/memory/scoped_ptr.h>
11
Vitaly Buka6ca6a232015-08-06 17:32:43 -070012namespace crypto {
13
Vitaly Buka0d501072015-08-18 18:09:46 -070014void SHA256HashString(const std::string& str, uint8_t* output, size_t len) {
Vitaly Buka9ba72a82015-08-06 17:36:17 -070015 std::string hash = SHA256HashString(str);
16 len = std::min(hash.size(), len);
17 std::copy(hash.begin(), hash.begin() + len, output);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070018}
19
Vitaly Buka0d501072015-08-18 18:09:46 -070020std::string SHA256HashString(const std::string& str) {
Vitaly Buka9ba72a82015-08-06 17:36:17 -070021 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 Buka6ca6a232015-08-06 17:32:43 -070028}
29
30} // namespace crypto