sha2.cc 965 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (c) 2012 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. #include "crypto/sha2.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include "crypto/secure_hash.h"
  8. #include "third_party/boringssl/src/include/openssl/sha.h"
  9. namespace crypto {
  10. std::array<uint8_t, kSHA256Length> SHA256Hash(base::span<const uint8_t> input) {
  11. std::array<uint8_t, kSHA256Length> digest;
  12. ::SHA256(input.data(), input.size(), digest.data());
  13. return digest;
  14. }
  15. void SHA256HashString(base::StringPiece str, void* output, size_t len) {
  16. std::unique_ptr<SecureHash> ctx(SecureHash::Create(SecureHash::SHA256));
  17. ctx->Update(str.data(), str.length());
  18. ctx->Finish(output, len);
  19. }
  20. std::string SHA256HashString(base::StringPiece str) {
  21. std::string output(kSHA256Length, 0);
  22. SHA256HashString(str, std::data(output), output.size());
  23. return output;
  24. }
  25. } // namespace crypto