2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <dpl/log/log.h>
19 #include <openssl/evp.h>
26 m_digest(EVP_MAX_MD_SIZE)
30 m_initialized = false;
36 EVP_MD_CTX_destroy(m_ctx);
44 EVP_MD_CTX_destroy(m_ctx);
48 m_initialized = false;
50 m_ctx = EVP_MD_CTX_create();
51 if (m_ctx == nullptr) {
54 ret = EVP_DigestInit_ex(m_ctx, m_md, NULL);
56 ThrowMsg(Exception::InternalError,
57 "Failed to create digest context.");
60 m_digest.resize(EVP_MAX_MD_SIZE);
64 void Digest::append(const RawBuffer &data, std::size_t len)
68 if (data.size() == 0) {
69 ThrowMsg(Exception::InternalError, "Empty data.");
74 ThrowMsg(Exception::InternalError, "Already finalized.");
76 if (not m_initialized)
78 ret = EVP_DigestUpdate(m_ctx, data.data(), len);
80 ThrowMsg(Exception::InternalError,
81 "Failed to calculate digest in openssl.");
85 RawBuffer Digest::finalize()
91 ThrowMsg(Exception::InternalError, "Already finalized.");
94 ret = EVP_DigestFinal_ex(m_ctx, m_digest.data(), &dlen);
96 ThrowMsg(Exception::InternalError,
97 "Failed in digest final in openssl.");
99 if (dlen != length()) {
100 ThrowMsg(Exception::InternalError, "Invalid digest length.");
102 if (dlen != EVP_MAX_MD_SIZE)
103 m_digest.resize(dlen);
107 RawBuffer Digest::get()
115 unsigned int Digest::length()
117 return m_md->md_size;