forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1_boringssl.cc
51 lines (40 loc) · 1.37 KB
/
sha1_boringssl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/hash/sha1.h"
#include <stdint.h>
#include "base/strings/string_util.h"
#include "third_party/boringssl/src/include/openssl/crypto.h"
#include "third_party/boringssl/src/include/openssl/sha.h"
namespace base {
static_assert(kSHA1Length == SHA_DIGEST_LENGTH,
"SHA-1 digest length mismatch.");
SHA1Digest SHA1HashSpan(span<const uint8_t> data) {
CRYPTO_library_init();
SHA1Digest digest;
SHA1(data.data(), data.size(), digest.data());
return digest;
}
std::string SHA1HashString(StringPiece str) {
CRYPTO_library_init();
std::string digest;
SHA1(reinterpret_cast<const uint8_t*>(str.data()), str.size(),
reinterpret_cast<uint8_t*>(WriteInto(&digest, kSHA1Length + 1)));
return digest;
}
void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) {
CRYPTO_library_init();
SHA1(data, len, hash);
}
// These functions allow streaming SHA-1 operations.
void SHA1Init(SHA1Context& context) {
SHA1_Init(&context);
}
void SHA1Update(const StringPiece data, SHA1Context& context) {
SHA1_Update(&context, data.data(), data.size());
}
void SHA1Final(SHA1Context& context, SHA1Digest& digest) {
SHA1Context ctx(context);
SHA1_Final(digest.data(), &ctx);
}
} // namespace base