1 // Copyright 2019 The Chromium Authors
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 "base/hash/sha1_boringssl.h"
6
7 #include <stdint.h>
8
9 #include <cstddef>
10 #include <string>
11 #include <string_view>
12
13 #include "base/containers/span.h"
14 #include "base/hash/sha1.h"
15 #include "third_party/boringssl/src/include/openssl/sha.h"
16
17 namespace base {
18 static_assert(kSHA1Length == SHA_DIGEST_LENGTH,
19 "SHA-1 digest length mismatch.");
20
SHA1Hash(span<const uint8_t> data)21 SHA1Digest SHA1Hash(span<const uint8_t> data) {
22 SHA1Digest digest;
23 SHA1(data.data(), data.size(), digest.data());
24 return digest;
25 }
26
SHA1HashString(std::string_view str)27 std::string SHA1HashString(std::string_view str) {
28 std::string digest(kSHA1Length, '\0');
29 SHA1(reinterpret_cast<const uint8_t*>(str.data()), str.size(),
30 reinterpret_cast<uint8_t*>(digest.data()));
31 return digest;
32 }
33
34 // These functions allow streaming SHA-1 operations.
SHA1Init(SHA1Context & context)35 void SHA1Init(SHA1Context& context) {
36 SHA1_Init(&context);
37 }
38
SHA1Update(std::string_view data,SHA1Context & context)39 void SHA1Update(std::string_view data, SHA1Context& context) {
40 SHA1_Update(&context, data.data(), data.size());
41 }
42
SHA1Final(SHA1Context & context,SHA1Digest & digest)43 void SHA1Final(SHA1Context& context, SHA1Digest& digest) {
44 SHA1Context ctx(context);
45 SHA1_Final(digest.data(), &ctx);
46 }
47
48 } // namespace base
49