1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "util/digest_parameter.h"
17 
18 #include "openssl/evp.h"
19 
20 namespace OHOS::Security::Verify {
DigestParameter()21 DigestParameter::DigestParameter() : digestOutputSizeBytes(0), md(nullptr), ptrCtx(nullptr)
22 {
23 }
24 
~DigestParameter()25 DigestParameter::~DigestParameter()
26 {
27     if (ptrCtx != nullptr) {
28         EVP_MD_CTX_destroy(ptrCtx);
29         ptrCtx = nullptr;
30     }
31     /* md points to the OpenSSL global static struct constant, no need to free. */
32     md = nullptr;
33 }
34 
DigestParameter(const DigestParameter & other)35 DigestParameter::DigestParameter(const DigestParameter& other)
36 {
37     digestOutputSizeBytes = other.digestOutputSizeBytes;
38     md = other.md;
39     ptrCtx = EVP_MD_CTX_create();
40     EVP_MD_CTX_copy(ptrCtx, other.ptrCtx);
41 }
42 
operator =(const DigestParameter & other)43 DigestParameter& DigestParameter::operator = (const DigestParameter& other)
44 {
45     if (ptrCtx != nullptr) {
46         EVP_MD_CTX_destroy(ptrCtx);
47         ptrCtx = nullptr;
48     }
49 
50     digestOutputSizeBytes = other.digestOutputSizeBytes;
51     md = other.md;
52     ptrCtx = EVP_MD_CTX_create();
53     EVP_MD_CTX_copy(ptrCtx, other.ptrCtx);
54     return *this;
55 }
56 }  // namespace OHOS::Security::Verify
57