Coverage Report

Created: 2024-11-21 10:56

/root/doris/be/src/util/sm3.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/sm3.h"
19
20
#include <openssl/evp.h>
21
#include <stdint.h>
22
23
#include "common/logging.h"
24
25
namespace doris {
26
27
57
SM3Digest::SM3Digest() {
28
57
    _md = EVP_sm3();
29
57
    _ctx = EVP_MD_CTX_new();
30
57
    EVP_DigestInit_ex(_ctx, _md, NULL);
31
57
}
32
33
57
SM3Digest::~SM3Digest() {
34
57
    EVP_MD_CTX_free(_ctx);
35
57
}
36
37
90
void SM3Digest::update(const void* data, size_t length) {
38
90
    EVP_DigestUpdate(_ctx, data, length);
39
90
}
40
41
57
void SM3Digest::digest() {
42
57
    unsigned char buf[SM3_DIGEST_LENGTH];
43
57
    uint32_t length;
44
57
    EVP_DigestFinal_ex(_ctx, buf, &length);
45
57
    DCHECK_EQ(SM3_DIGEST_LENGTH, length);
46
47
57
    char hex_buf[2 * SM3_DIGEST_LENGTH];
48
49
57
    static char dig_vec_lower[] = "0123456789abcdef";
50
57
    char* to = hex_buf;
51
1.88k
    for (int i = 0; i < SM3_DIGEST_LENGTH; ++i) {
52
1.82k
        *to++ = dig_vec_lower[buf[i] >> 4];
53
1.82k
        *to++ = dig_vec_lower[buf[i] & 0x0F];
54
1.82k
    }
55
57
    _hex.assign(hex_buf, 2 * SM3_DIGEST_LENGTH);
56
57
}
57
58
} // namespace doris