MySQL 8.4.0
Source Code Documentation
sql_digest.h
Go to the documentation of this file.
1/* Copyright (c) 2008, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_DIGEST_H
25#define SQL_DIGEST_H
26
27#include <string.h>
28#include <sys/types.h>
29
30#include "my_inttypes.h" // IWYU pragma: keep
31
32class String;
33
34#define MAX_DIGEST_STORAGE_SIZE (1024 * 1024)
35
36/**
37 Write SHA-256 hash value in a string to be used
38 as DIGEST for the statement.
39*/
40#define DIGEST_HASH_TO_STRING(_hash, _str) \
41 (void)sprintf(_str, \
42 "%02x%02x%02x%02x%02x%02x%02x%02x" \
43 "%02x%02x%02x%02x%02x%02x%02x%02x" \
44 "%02x%02x%02x%02x%02x%02x%02x%02x" \
45 "%02x%02x%02x%02x%02x%02x%02x%02x", \
46 _hash[0], _hash[1], _hash[2], _hash[3], _hash[4], _hash[5], \
47 _hash[6], _hash[7], _hash[8], _hash[9], _hash[10], _hash[11], \
48 _hash[12], _hash[13], _hash[14], _hash[15], _hash[16], \
49 _hash[17], _hash[18], _hash[19], _hash[20], _hash[21], \
50 _hash[22], _hash[23], _hash[24], _hash[25], _hash[26], \
51 _hash[27], _hash[28], _hash[29], _hash[30], _hash[31])
52
53/// SHA-256 = 32 bytes of binary = 64 printable characters.
54#define DIGEST_HASH_TO_STRING_LENGTH 64
55
56/*
57 Various hashes considered for digests.
58
59 MD5:
60 - 128 bits
61 - used up to MySQL 5.7
62 - abandoned in MySQL 8.0, non FIPS compliant.
63
64 SHA1:
65 - 160 bits
66 - non FIPS compliant in strict mode
67 - not used
68
69 SHA2-224
70 - 224 bits
71 - non FIPS compliant in strict mode
72 - not used
73
74 SHA2-256
75 - 256 bits
76 - FIPS compliant
77 - Used starting with MySQL 8.0
78
79 SHA2-384
80 - 384 bits
81
82 SHA2-512
83 - 512 bits
84*/
85
86/**
87 DIGEST hash size, in bytes.
88 256 bits, for SHA256.
89*/
90#define DIGEST_HASH_SIZE 32
91
93
94/**
95 Structure to store token count/array for a statement
96 on which digest is to be calculated.
97*/
99 bool m_full;
101 unsigned char m_hash[DIGEST_HASH_SIZE];
102 /** Character set number. */
104 /**
105 Token array.
106 Token array is an array of bytes to store tokens received during parsing.
107 Following is the way token array is formed.
108 ... &lt;non-id-token&gt; &lt;non-id-token&gt; &lt;id-token&gt;
109 &lt;id_len&gt; &lt;id_text&gt; ... For Example: SELECT * FROM T1;
110 &lt;SELECT_TOKEN&gt; &lt;*&gt; &lt;FROM_TOKEN&gt; &lt;ID_TOKEN&gt; &lt;2&gt;
111 &lt;T1&gt;
112
113 @note Only the first @c m_byte_count bytes are initialized,
114 out of @c m_token_array_length.
115 */
116 unsigned char *m_token_array;
117 /* Length of the token array to be considered for DIGEST_TEXT calculation. */
119
120 sql_digest_storage() { reset(nullptr, 0); }
121
122 inline void reset(unsigned char *token_array, size_t length) {
123 m_token_array = token_array;
125 reset();
126 }
127
128 inline void reset() {
129 m_full = false;
130 m_byte_count = 0;
132 memset(m_hash, 0, DIGEST_HASH_SIZE);
133 }
134
135 inline bool is_empty() { return (m_byte_count == 0); }
136
137 inline void copy(const sql_digest_storage *from) {
138 /*
139 Keep in mind this is a dirty copy of something that may change,
140 as the thread producing the digest is executing concurrently,
141 without any lock enforced.
142 */
143 const size_t byte_count_copy = m_token_array_length < from->m_byte_count
145 : from->m_byte_count;
146
147 if (byte_count_copy > 0) {
148 m_full = from->m_full;
149 m_byte_count = byte_count_copy;
152 memcpy(m_hash, from->m_hash, DIGEST_HASH_SIZE);
153 } else {
154 m_full = false;
155 m_byte_count = 0;
157 }
158 }
159};
161
162/**
163 Compute a digest hash.
164 @param digest_storage The digest
165 @param [out] hash The computed digest hash. This parameter is a buffer of size
166 @c DIGEST_HASH_SIZE.
167*/
168void compute_digest_hash(const sql_digest_storage *digest_storage,
169 unsigned char *hash);
170
171/**
172 Compute a digest text.
173 A 'digest text' is a textual representation of a query,
174 where:
175 - comments are removed,
176 - non significant spaces are removed,
177 - literal values are replaced with a special '?' marker,
178 - lists of values are collapsed using a shorter notation
179 @param digest_storage The digest
180 @param [out] digest_text The digest text
181*/
182void compute_digest_text(const sql_digest_storage *digest_storage,
183 String *digest_text);
184
185#endif
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
Some integer typedefs for easier portability.
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
void compute_digest_hash(const sql_digest_storage *digest_storage, unsigned char *hash)
Compute a digest hash.
Definition: sql_digest.cc:160
void compute_digest_text(const sql_digest_storage *digest_storage, String *digest_text)
Compute a digest text.
Definition: sql_digest.cc:171
ulong get_max_digest_length()
Definition: sql_digest.cc:51
#define DIGEST_HASH_SIZE
DIGEST hash size, in bytes.
Definition: sql_digest.h:90
Structure to store token count/array for a statement on which digest is to be calculated.
Definition: sql_digest.h:98
void copy(const sql_digest_storage *from)
Definition: sql_digest.h:137
uint m_charset_number
Character set number.
Definition: sql_digest.h:103
sql_digest_storage()
Definition: sql_digest.h:120
size_t m_byte_count
Definition: sql_digest.h:100
unsigned char m_hash[DIGEST_HASH_SIZE]
Definition: sql_digest.h:101
bool m_full
Definition: sql_digest.h:99
unsigned char * m_token_array
Token array.
Definition: sql_digest.h:116
void reset(unsigned char *token_array, size_t length)
Definition: sql_digest.h:122
bool is_empty()
Definition: sql_digest.h:135
void reset()
Definition: sql_digest.h:128
size_t m_token_array_length
Definition: sql_digest.h:118