Coverage Report

Created: 2025-10-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/include/Poco/DigestEngine.h
Line
Count
Source
1
//
2
// DigestEngine.h
3
//
4
// Library: Foundation
5
// Package: Crypt
6
// Module:  DigestEngine
7
//
8
// Definition of class DigestEngine.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_DigestEngine_INCLUDED
18
#define Foundation_DigestEngine_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
#include <vector>
23
24
25
namespace Poco {
26
27
28
class Foundation_API DigestEngine
29
  /// This class is an abstract base class
30
  /// for all classes implementing a message
31
  /// digest algorithm, like MD5Engine
32
  /// and SHA1Engine.
33
  /// Call update() repeatedly with data to
34
  /// compute the digest from. When done,
35
  /// call digest() to obtain the message
36
  /// digest.
37
{
38
public:
39
  using Digest = std::vector<unsigned char>;
40
41
  DigestEngine();
42
  virtual ~DigestEngine();
43
44
  void update(const void* data, std::size_t length);
45
  void update(char data);
46
  void update(const std::string& data);
47
    /// Updates the digest with the given data.
48
49
  virtual std::size_t digestLength() const = 0;
50
    /// Returns the length of the digest in bytes.
51
52
  virtual void reset() = 0;
53
    /// Resets the engine so that a new
54
    /// digest can be computed.
55
56
  virtual const Digest& digest() = 0;
57
    /// Finishes the computation of the digest and
58
    /// returns the message digest. Resets the engine
59
    /// and can thus only be called once for every digest.
60
    /// The returned reference is valid until the next
61
    /// time digest() is called, or the engine object is destroyed.
62
63
  static std::string digestToHex(const Digest& bytes, std::size_t length = 0);
64
    /// Converts a message digest into a string of hexadecimal numbers.
65
    /// If length is greater than zero, the output is truncated to length
66
    /// bytes. If size is greater than the length of untruncated output,
67
    /// InvalidArgumentException is thrown.
68
69
  static Digest digestFromHex(const std::string& digest);
70
    /// Converts a string created by digestToHex back to its Digest presentation
71
72
  static bool constantTimeEquals(const Digest& d1, const Digest& d2);
73
    /// Compares two Digest values using a constant-time comparison
74
    /// algorithm. This can be used to prevent timing attacks
75
    /// (as discussed in <https://codahale.com/a-lesson-in-timing-attacks/>).
76
77
protected:
78
  virtual void updateImpl(const void* data, std::size_t length) = 0;
79
    /// Updates the digest with the given data. Must be implemented
80
    /// by subclasses.
81
82
private:
83
  DigestEngine(const DigestEngine&);
84
  DigestEngine& operator = (const DigestEngine&);
85
};
86
87
88
//
89
// inlines
90
//
91
92
93
inline void DigestEngine::update(const void* data, std::size_t length)
94
176k
{
95
176k
  updateImpl(data, length);
96
176k
}
97
98
99
inline void DigestEngine::update(char data)
100
25.6k
{
101
25.6k
  updateImpl(&data, 1);
102
25.6k
}
103
104
105
inline void DigestEngine::update(const std::string& data)
106
51.8k
{
107
51.8k
  updateImpl(data.data(), data.size());
108
51.8k
}
109
110
111
} // namespace Poco
112
113
114
#endif // Foundation_DigestEngine_INCLUDED