Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
IHashAlgorithm.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* IHashAlgorithm.h (C) 2000-2023 */
9/* */
10/* Interface of a hashing algorithm. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_IHASHALGORITHM_H
13#define ARCANE_UTILS_IHASHALGORITHM_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include "arccore/base/Span.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief Hash algorithm return value.
33 */
34class ARCANE_UTILS_EXPORT HashAlgorithmValue
35{
36 public:
37
38 static constexpr Int32 MAX_SIZE = 64;
39
40 public:
41
43 {
44 return { m_value.data(), m_size };
45 }
46 SmallSpan<const std::byte> bytes() const
47 {
48 return { m_value.data(), m_size };
49 }
50 SmallSpan<const Byte> asLegacyBytes() const
51 {
52 return { reinterpret_cast<const Byte*>(m_value.data()), m_size };
53 }
54 void setSize(Int32 size);
55
56 private:
57
58 std::array<std::byte, MAX_SIZE> m_value = {};
59 Int32 m_size = MAX_SIZE;
60};
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
65/*!
66 * \brief Context for calculating a hash incrementally.
67 *
68 * The same context can be used multiple times by calling reset() to
69 * reset the instance.
70 *
71 * \code
72 * IHashAlgorithm* algo = ...;
73 * HashAlgorithmValue hash_value;
74 * hash_value.reserve(algo->hashSize());
75 * UniqueArray<std::byte> array1 = ...;
76 * UniqueArray<std::byte> array2 = ...;
77 *
78 * Ref<IHashContext> context = algo->createContext();
79 * context->updateHash(array1);
80 * context->updateHash(array2);
81 * context->computeHashValue(hash_value);
82 * \endcode
83 */
84class ARCANE_UTILS_EXPORT IHashAlgorithmContext
85{
86 public:
87
88 virtual ~IHashAlgorithmContext() = default;
89
90 public:
91
92 //! Resets the instance to calculate a new hash value.
93 virtual void reset() = 0;
94
95 //! Adds the array \a input to the calculated hash
96 virtual void updateHash(Span<const std::byte> input) = 0;
97
98 //! Calculates the hash value and returns it in hash_value.
99 virtual void computeHashValue(HashAlgorithmValue& hash_value) = 0;
100};
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
105/*!
106 * \brief Interface of a hashing algorithm.
107 */
108class ARCANE_UTILS_EXPORT IHashAlgorithm
109{
110 public:
111
112 virtual ~IHashAlgorithm() = default;
113
114 public:
115
116 //NOTE: for now (version 3.10) still pure virtual to remain
117 // compatible with existing code
118 //! Name of the algorithm
119 virtual String name() const;
120
121 //NOTE: for now (version 3.10) still pure virtual to remain
122 // compatible with existing code. Throws FatalErrorException if not overridden
123 //! Size (in bytes) of the hash key.
124 virtual Int32 hashSize() const;
125
126 /*!
127 * \brief Calculates the hash value for the array \a input.
128 *
129 * The hash value is <strong >added</strong > to \a output.
130 * The added length is equal to hashSize().
131 */
132 virtual void computeHash64(Span<const Byte> input, ByteArray& output);
133
134 /*!
135 * \brief Calculates the hash value for the array \a input.
136 *
137 * The hash value is <strong >added</strong > to \a output.
138 * The added length is equal to hashSize().
139 */
140 virtual void computeHash64(Span<const std::byte> input, ByteArray& output);
141
142 //NOTE: for now (version 3.10) still pure virtual to remain
143 // compatible with existing code
144 /*!
145 * \brief Calculates the hash value for the array \a input.
146 *
147 * The hash value is positioned in \a value
148 */
149 virtual void computeHash(Span<const std::byte> input, HashAlgorithmValue& value);
150
151 //NOTE: for now (version 3.11) still pure virtual to remain
152 // compatible with existing code
153 /*!
154 * \brief Creates a context to calculate the hash value
155 * incrementally.
156 *
157 * If the implementation does not support incremental mode (hasCreateContext()==false),
158 * an exception is thrown.
159 */
161
162 //NOTE: for now (version 3.11) still pure virtual to remain
163 // compatible with existing code
164 //! Indicates if the implementation supports incremental hashing
165 virtual bool hasCreateContext() const { return false; }
166
167 public:
168
169 /*!
170 * \brief Calculates the hash value for the array \a input.
171 *
172 * The hash value is <strong >added</strong > to \a output.
173 * The length depends on the algorithm used.
174 */
175 ARCANE_DEPRECATED_REASON("Y2023: Use computeHash64(Span<const std::byte> input,ByteArray& output) instead")
176 virtual void computeHash(ByteConstArrayView input, ByteArray& output) = 0;
177};
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182} // namespace Arcane
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187#endif
Types and functions associated with the classes SpanImpl, SmallSpan and Span.
Declarations of types used in Arcane.
Hash algorithm return value.
Context for calculating a hash incrementally.
virtual void updateHash(Span< const std::byte > input)=0
Adds the array input to the calculated hash.
virtual void reset()=0
Resets the instance to calculate a new hash value.
virtual void computeHashValue(HashAlgorithmValue &hash_value)=0
Calculates the hash value and returns it in hash_value.
Interface of a hashing algorithm.
virtual bool hasCreateContext() const
Indicates if the implementation supports incremental hashing.
virtual String name() const
Name of the algorithm.
virtual void computeHash(Span< const std::byte > input, HashAlgorithmValue &value)
Calculates the hash value for the array input.
virtual void computeHash64(Span< const Byte > input, ByteArray &output)
Calculates the hash value for the array input.
virtual Int32 hashSize() const
Size (in bytes) of the hash key.
virtual Ref< IHashAlgorithmContext > createContext()
Creates a context to calculate the hash value incrementally.
Reference to an instance.
View of an array of elements of type T.
Definition Span.h:805
View of an array of elements of type T.
Definition Span.h:635
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
Definition UtilsTypes.h:121
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:476
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
std::int32_t Int32
Signed integer type of 32 bits.