Home | History | Annotate | Download | only in crypto
      1 // Copyright 2012 The Chromium OS 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 "third_party/chromium/crypto/sha2.h"
      6 
      7 #include <algorithm>
      8 #include <openssl/sha.h>
      9 
     10 #include <base/memory/scoped_ptr.h>
     11 
     12 namespace crypto {
     13 
     14 void SHA256HashString(const std::string& str, uint8_t* output, size_t len) {
     15   std::string hash = SHA256HashString(str);
     16   len = std::min(hash.size(), len);
     17   std::copy(hash.begin(), hash.begin() + len, output);
     18 }
     19 
     20 std::string SHA256HashString(const std::string& str) {
     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;
     28 }
     29 
     30 }  // namespace crypto
     31