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)17SecureHash::SecureHash(const EVP_MD* type) { 18 EVP_DigestInit(ctx_.get(), type); 19 } 20 SecureHash(const SecureHash & other)21SecureHash::SecureHash(const SecureHash& other) { 22 *this = other; 23 } 24 operator =(const SecureHash & other)25SecureHash& 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)35void SecureHash::Update(const uint8_t* input, size_t len) { 36 EVP_DigestUpdate(ctx_.get(), input, len); 37 } 38 Finish(uint8_t * output)39void SecureHash::Finish(uint8_t* output) { 40 EVP_DigestFinal(ctx_.get(), output, nullptr); 41 } 42 Update(const std::string & input)43void SecureHash::Update(const std::string& input) { 44 Update(reinterpret_cast<const uint8_t*>(input.data()), input.length()); 45 } 46 Finish(char * output)47void SecureHash::Finish(char* output) { 48 Finish(reinterpret_cast<uint8_t*>(output)); 49 } 50 GetHashLength() const51size_t SecureHash::GetHashLength() const { 52 return EVP_MD_CTX_size(ctx_.get()); 53 } 54 55 } // namespace openscreen 56