xref: /aosp_15_r20/external/openscreen/util/crypto/secure_hash.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 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 
5 #include "util/crypto/secure_hash.h"
6 
7 #include <stddef.h>
8 
9 #include <cstring>
10 
11 #include "openssl/mem.h"
12 #include "util/crypto/openssl_util.h"
13 #include "util/osp_logging.h"
14 
15 namespace openscreen {
16 
SecureHash(const EVP_MD * type)17 SecureHash::SecureHash(const EVP_MD* type) {
18   EVP_DigestInit(ctx_.get(), type);
19 }
20 
SecureHash(const SecureHash & other)21 SecureHash::SecureHash(const SecureHash& other) {
22   *this = other;
23 }
24 
operator =(const SecureHash & other)25 SecureHash& SecureHash::operator=(const SecureHash& other) {
26   EVP_MD_CTX_copy_ex(this->ctx_.get(), other.ctx_.get());
27   return *this;
28 }
29 
30 SecureHash::SecureHash(SecureHash&& other) = default;
31 SecureHash& SecureHash::operator=(SecureHash&& other) = default;
32 
33 SecureHash::~SecureHash() = default;
34 
Update(const uint8_t * input,size_t len)35 void SecureHash::Update(const uint8_t* input, size_t len) {
36   EVP_DigestUpdate(ctx_.get(), input, len);
37 }
38 
Finish(uint8_t * output)39 void SecureHash::Finish(uint8_t* output) {
40   EVP_DigestFinal(ctx_.get(), output, nullptr);
41 }
42 
Update(const std::string & input)43 void SecureHash::Update(const std::string& input) {
44   Update(reinterpret_cast<const uint8_t*>(input.data()), input.length());
45 }
46 
Finish(char * output)47 void SecureHash::Finish(char* output) {
48   Finish(reinterpret_cast<uint8_t*>(output));
49 }
50 
GetHashLength() const51 size_t SecureHash::GetHashLength() const {
52   return EVP_MD_CTX_size(ctx_.get());
53 }
54 
55 }  // namespace openscreen
56