Coverage Report

Created: 2026-01-06 16:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/sha.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/sha.h"
19
20
#include <openssl/sha.h>
21
22
#include <string_view>
23
24
namespace doris {
25
26
constexpr static char dig_vec_lower[] = "0123456789abcdef";
27
28
43
void SHA1Digest::reset(const void* data, size_t length) {
29
43
    SHA1_Init(&_sha_ctx);
30
43
    SHA1_Update(&_sha_ctx, data, length);
31
43
}
32
33
43
std::string_view SHA1Digest::digest() {
34
43
    unsigned char buf[SHA_DIGEST_LENGTH];
35
43
    SHA1_Final(buf, &_sha_ctx);
36
37
43
    char* to = _reuse_hex;
38
903
    for (int i = 0; i < SHA_DIGEST_LENGTH; ++i) {
39
860
        *to++ = dig_vec_lower[buf[i] >> 4];
40
860
        *to++ = dig_vec_lower[buf[i] & 0x0F];
41
860
    }
42
43
43
    return std::string_view {_reuse_hex, _reuse_hex + 2 * SHA_DIGEST_LENGTH};
44
43
}
45
46
21
void SHA224Digest::reset(const void* data, size_t length) {
47
21
    SHA224_Init(&_sha224_ctx);
48
21
    SHA224_Update(&_sha224_ctx, data, length);
49
21
}
50
51
21
std::string_view SHA224Digest::digest() {
52
21
    unsigned char buf[SHA224_DIGEST_LENGTH];
53
21
    SHA224_Final(buf, &_sha224_ctx);
54
55
21
    char* to = _reuse_hex;
56
609
    for (int i = 0; i < SHA224_DIGEST_LENGTH; ++i) {
57
588
        *to++ = dig_vec_lower[buf[i] >> 4];
58
588
        *to++ = dig_vec_lower[buf[i] & 0x0F];
59
588
    }
60
61
21
    return std::string_view {_reuse_hex, _reuse_hex + 2 * SHA224_DIGEST_LENGTH};
62
21
}
63
64
2.95M
void SHA256Digest::reset(const void* data, size_t length) {
65
2.95M
    SHA256_Init(&_sha256_ctx);
66
2.95M
    SHA256_Update(&_sha256_ctx, data, length);
67
2.95M
}
68
69
5.89M
std::string_view SHA256Digest::digest() {
70
5.89M
    unsigned char buf[SHA256_DIGEST_LENGTH];
71
5.89M
    SHA256_Final(buf, &_sha256_ctx);
72
73
5.89M
    char* to = _reuse_hex;
74
194M
    for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
75
188M
        *to++ = dig_vec_lower[buf[i] >> 4];
76
188M
        *to++ = dig_vec_lower[buf[i] & 0x0F];
77
188M
    }
78
79
5.89M
    return std::string_view {_reuse_hex, _reuse_hex + 2 * SHA256_DIGEST_LENGTH};
80
5.89M
}
81
82
21
void SHA384Digest::reset(const void* data, size_t length) {
83
21
    SHA384_Init(&_sha384_ctx);
84
21
    SHA384_Update(&_sha384_ctx, data, length);
85
21
}
86
87
21
std::string_view SHA384Digest::digest() {
88
21
    unsigned char buf[SHA384_DIGEST_LENGTH];
89
21
    SHA384_Final(buf, &_sha384_ctx);
90
91
21
    char* to = _reuse_hex;
92
1.02k
    for (int i = 0; i < SHA384_DIGEST_LENGTH; ++i) {
93
1.00k
        *to++ = dig_vec_lower[buf[i] >> 4];
94
1.00k
        *to++ = dig_vec_lower[buf[i] & 0x0F];
95
1.00k
    }
96
97
21
    return std::string_view {_reuse_hex, _reuse_hex + 2 * SHA384_DIGEST_LENGTH};
98
21
}
99
100
23
void SHA512Digest::reset(const void* data, size_t length) {
101
23
    SHA512_Init(&_sha512_ctx);
102
23
    SHA512_Update(&_sha512_ctx, data, length);
103
23
}
104
105
23
std::string_view SHA512Digest::digest() {
106
23
    unsigned char buf[SHA512_DIGEST_LENGTH];
107
23
    SHA512_Final(buf, &_sha512_ctx);
108
109
23
    char* to = _reuse_hex;
110
1.49k
    for (int i = 0; i < SHA512_DIGEST_LENGTH; ++i) {
111
1.47k
        *to++ = dig_vec_lower[buf[i] >> 4];
112
1.47k
        *to++ = dig_vec_lower[buf[i] & 0x0F];
113
1.47k
    }
114
115
    return std::string_view {_reuse_hex, _reuse_hex + 2 * SHA512_DIGEST_LENGTH};
116
23
}
117
118
} // namespace doris