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