Coverage Report

Created: 2026-10-31 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/include/tscore/SHA256.h
Line
Count
Source
1
/** @file
2
3
  SHA256 support class.
4
5
  @section license License
6
7
  Licensed to the Apache Software Foundation (ASF) under one
8
  or more contributor license agreements.  See the NOTICE file
9
  distributed with this work for additional information
10
  regarding copyright ownership.  The ASF licenses this file
11
  to you under the Apache License, Version 2.0 (the
12
  "License"); you may not use this file except in compliance
13
  with the License.  You may obtain a copy of the License at
14
15
      http://www.apache.org/licenses/LICENSE-2.0
16
17
  Unless required by applicable law or agreed to in writing, software
18
  distributed under the License is distributed on an "AS IS" BASIS,
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
  See the License for the specific language governing permissions and
21
  limitations under the License.
22
 */
23
24
#pragma once
25
26
#include "tscore/ink_defs.h"
27
#include "tscore/CryptoHash.h"
28
#if HAVE_SHA256_INIT
29
#include <openssl/sha.h>
30
#else
31
#include <openssl/evp.h>
32
#endif
33
34
class SHA256Context : public ts::CryptoContext::Hasher
35
{
36
public:
37
  SHA256Context()
38
0
  {
39
0
#if HAVE_SHA256_INIT
40
0
#pragma GCC diagnostic push
41
0
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42
0
    SHA256_Init(&_sha256ctx);
43
0
#pragma GCC diagnostic pop
44
0
#else
45
0
    _ctx = EVP_MD_CTX_new();
46
0
    EVP_DigestInit_ex(_ctx, EVP_sha256(), nullptr);
47
0
#endif
48
0
  }
49
  ~SHA256Context() override
50
0
  {
51
0
#if HAVE_SHA256_INIT
52
0
    // _sha256ctx does not need to be freed
53
0
#else
54
0
    EVP_MD_CTX_free(_ctx);
55
0
#endif
56
0
  }
57
  /// Update the hash with @a data of @a length bytes.
58
  bool
59
  update(void const *data, int length) override
60
0
  {
61
0
#if HAVE_SHA256_INIT
62
0
#pragma GCC diagnostic push
63
0
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
64
0
    return SHA256_Update(&_sha256ctx, data, length);
65
0
#pragma GCC diagnostic pop
66
0
#else
67
0
    return EVP_DigestUpdate(_ctx, data, length);
68
0
#endif
69
0
  }
70
  /// Finalize and extract the @a hash.
71
  bool
72
  finalize(CryptoHash &hash) override
73
0
  {
74
0
#if HAVE_SHA256_INIT
75
0
#pragma GCC diagnostic push
76
0
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77
0
    return SHA256_Final(hash.u8, &_sha256ctx);
78
0
#pragma GCC diagnostic pop
79
0
#else
80
0
    return EVP_DigestFinal_ex(_ctx, hash.u8, nullptr);
81
0
#endif
82
0
  }
83
#if HAVE_SHA256_INIT
84
private:
85
  SHA256_CTX _sha256ctx;
86
#endif
87
};