4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This SQLite extension implements functions that compute SHA3 hashes
14 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
15 ** Two SQL functions are implemented:
20 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
23 ** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
24 ** and returns a hash of their results.
26 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
27 ** is used. If SIZE is included it must be one of the integers 224, 256,
28 ** 384, or 512, to determine SHA3 hash variant that is computed.
30 #include "sqlite3ext.h"
31 SQLITE_EXTENSION_INIT1
36 #ifndef SQLITE_AMALGAMATION
37 typedef sqlite3_uint64 u64
;
38 #endif /* SQLITE_AMALGAMATION */
40 /******************************************************************************
44 ** Macros to determine whether the machine is big or little endian,
45 ** and whether or not that determination is run-time or compile-time.
47 ** For best performance, an attempt is made to guess at the byte-order
48 ** using C-preprocessor macros. If that is unsuccessful, or if
49 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
52 #ifndef SHA3_BYTEORDER
53 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
54 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
55 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
57 # define SHA3_BYTEORDER 1234
58 # elif defined(sparc) || defined(__ppc__)
59 # define SHA3_BYTEORDER 4321
61 # define SHA3_BYTEORDER 0
67 ** State structure for a SHA3 hash in progress
69 typedef struct SHA3Context SHA3Context
;
72 u64 s
[25]; /* Keccak state. 5x5 lines of 64 bits each */
73 unsigned char x
[1600]; /* ... or 1600 bytes */
75 unsigned nRate
; /* Bytes of input accepted per Keccak iteration */
76 unsigned nLoaded
; /* Input bytes loaded into u.x[] so far this cycle */
77 unsigned ixMask
; /* Insert next input into u.x[nLoaded^ixMask]. */
81 ** A single step of the Keccak mixing function for a 1600-bit state
83 static void KeccakF1600Step(SHA3Context
*p
){
85 u64 b0
, b1
, b2
, b3
, b4
;
86 u64 c0
, c1
, c2
, c3
, c4
;
87 u64 d0
, d1
, d2
, d3
, d4
;
88 static const u64 RC
[] = {
89 0x0000000000000001ULL
, 0x0000000000008082ULL
,
90 0x800000000000808aULL
, 0x8000000080008000ULL
,
91 0x000000000000808bULL
, 0x0000000080000001ULL
,
92 0x8000000080008081ULL
, 0x8000000000008009ULL
,
93 0x000000000000008aULL
, 0x0000000000000088ULL
,
94 0x0000000080008009ULL
, 0x000000008000000aULL
,
95 0x000000008000808bULL
, 0x800000000000008bULL
,
96 0x8000000000008089ULL
, 0x8000000000008003ULL
,
97 0x8000000000008002ULL
, 0x8000000000000080ULL
,
98 0x000000000000800aULL
, 0x800000008000000aULL
,
99 0x8000000080008081ULL
, 0x8000000000008080ULL
,
100 0x0000000080000001ULL
, 0x8000000080008008ULL
102 # define a00 (p->u.s[0])
103 # define a01 (p->u.s[1])
104 # define a02 (p->u.s[2])
105 # define a03 (p->u.s[3])
106 # define a04 (p->u.s[4])
107 # define a10 (p->u.s[5])
108 # define a11 (p->u.s[6])
109 # define a12 (p->u.s[7])
110 # define a13 (p->u.s[8])
111 # define a14 (p->u.s[9])
112 # define a20 (p->u.s[10])
113 # define a21 (p->u.s[11])
114 # define a22 (p->u.s[12])
115 # define a23 (p->u.s[13])
116 # define a24 (p->u.s[14])
117 # define a30 (p->u.s[15])
118 # define a31 (p->u.s[16])
119 # define a32 (p->u.s[17])
120 # define a33 (p->u.s[18])
121 # define a34 (p->u.s[19])
122 # define a40 (p->u.s[20])
123 # define a41 (p->u.s[21])
124 # define a42 (p->u.s[22])
125 # define a43 (p->u.s[23])
126 # define a44 (p->u.s[24])
127 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
129 for(i
=0; i
<24; i
+=4){
130 c0
= a00
^a10
^a20
^a30
^a40
;
131 c1
= a01
^a11
^a21
^a31
^a41
;
132 c2
= a02
^a12
^a22
^a32
^a42
;
133 c3
= a03
^a13
^a23
^a33
^a43
;
134 c4
= a04
^a14
^a24
^a34
^a44
;
135 d0
= c4
^ROL64(c1
, 1);
136 d1
= c0
^ROL64(c2
, 1);
137 d2
= c1
^ROL64(c3
, 1);
138 d3
= c2
^ROL64(c4
, 1);
139 d4
= c3
^ROL64(c0
, 1);
142 b1
= ROL64((a11
^d1
), 44);
143 b2
= ROL64((a22
^d2
), 43);
144 b3
= ROL64((a33
^d3
), 21);
145 b4
= ROL64((a44
^d4
), 14);
146 a00
= b0
^((~b1
)& b2
);
148 a11
= b1
^((~b2
)& b3
);
149 a22
= b2
^((~b3
)& b4
);
150 a33
= b3
^((~b4
)& b0
);
151 a44
= b4
^((~b0
)& b1
);
153 b2
= ROL64((a20
^d0
), 3);
154 b3
= ROL64((a31
^d1
), 45);
155 b4
= ROL64((a42
^d2
), 61);
156 b0
= ROL64((a03
^d3
), 28);
157 b1
= ROL64((a14
^d4
), 20);
158 a20
= b0
^((~b1
)& b2
);
159 a31
= b1
^((~b2
)& b3
);
160 a42
= b2
^((~b3
)& b4
);
161 a03
= b3
^((~b4
)& b0
);
162 a14
= b4
^((~b0
)& b1
);
164 b4
= ROL64((a40
^d0
), 18);
165 b0
= ROL64((a01
^d1
), 1);
166 b1
= ROL64((a12
^d2
), 6);
167 b2
= ROL64((a23
^d3
), 25);
168 b3
= ROL64((a34
^d4
), 8);
169 a40
= b0
^((~b1
)& b2
);
170 a01
= b1
^((~b2
)& b3
);
171 a12
= b2
^((~b3
)& b4
);
172 a23
= b3
^((~b4
)& b0
);
173 a34
= b4
^((~b0
)& b1
);
175 b1
= ROL64((a10
^d0
), 36);
176 b2
= ROL64((a21
^d1
), 10);
177 b3
= ROL64((a32
^d2
), 15);
178 b4
= ROL64((a43
^d3
), 56);
179 b0
= ROL64((a04
^d4
), 27);
180 a10
= b0
^((~b1
)& b2
);
181 a21
= b1
^((~b2
)& b3
);
182 a32
= b2
^((~b3
)& b4
);
183 a43
= b3
^((~b4
)& b0
);
184 a04
= b4
^((~b0
)& b1
);
186 b3
= ROL64((a30
^d0
), 41);
187 b4
= ROL64((a41
^d1
), 2);
188 b0
= ROL64((a02
^d2
), 62);
189 b1
= ROL64((a13
^d3
), 55);
190 b2
= ROL64((a24
^d4
), 39);
191 a30
= b0
^((~b1
)& b2
);
192 a41
= b1
^((~b2
)& b3
);
193 a02
= b2
^((~b3
)& b4
);
194 a13
= b3
^((~b4
)& b0
);
195 a24
= b4
^((~b0
)& b1
);
197 c0
= a00
^a20
^a40
^a10
^a30
;
198 c1
= a11
^a31
^a01
^a21
^a41
;
199 c2
= a22
^a42
^a12
^a32
^a02
;
200 c3
= a33
^a03
^a23
^a43
^a13
;
201 c4
= a44
^a14
^a34
^a04
^a24
;
202 d0
= c4
^ROL64(c1
, 1);
203 d1
= c0
^ROL64(c2
, 1);
204 d2
= c1
^ROL64(c3
, 1);
205 d3
= c2
^ROL64(c4
, 1);
206 d4
= c3
^ROL64(c0
, 1);
209 b1
= ROL64((a31
^d1
), 44);
210 b2
= ROL64((a12
^d2
), 43);
211 b3
= ROL64((a43
^d3
), 21);
212 b4
= ROL64((a24
^d4
), 14);
213 a00
= b0
^((~b1
)& b2
);
215 a31
= b1
^((~b2
)& b3
);
216 a12
= b2
^((~b3
)& b4
);
217 a43
= b3
^((~b4
)& b0
);
218 a24
= b4
^((~b0
)& b1
);
220 b2
= ROL64((a40
^d0
), 3);
221 b3
= ROL64((a21
^d1
), 45);
222 b4
= ROL64((a02
^d2
), 61);
223 b0
= ROL64((a33
^d3
), 28);
224 b1
= ROL64((a14
^d4
), 20);
225 a40
= b0
^((~b1
)& b2
);
226 a21
= b1
^((~b2
)& b3
);
227 a02
= b2
^((~b3
)& b4
);
228 a33
= b3
^((~b4
)& b0
);
229 a14
= b4
^((~b0
)& b1
);
231 b4
= ROL64((a30
^d0
), 18);
232 b0
= ROL64((a11
^d1
), 1);
233 b1
= ROL64((a42
^d2
), 6);
234 b2
= ROL64((a23
^d3
), 25);
235 b3
= ROL64((a04
^d4
), 8);
236 a30
= b0
^((~b1
)& b2
);
237 a11
= b1
^((~b2
)& b3
);
238 a42
= b2
^((~b3
)& b4
);
239 a23
= b3
^((~b4
)& b0
);
240 a04
= b4
^((~b0
)& b1
);
242 b1
= ROL64((a20
^d0
), 36);
243 b2
= ROL64((a01
^d1
), 10);
244 b3
= ROL64((a32
^d2
), 15);
245 b4
= ROL64((a13
^d3
), 56);
246 b0
= ROL64((a44
^d4
), 27);
247 a20
= b0
^((~b1
)& b2
);
248 a01
= b1
^((~b2
)& b3
);
249 a32
= b2
^((~b3
)& b4
);
250 a13
= b3
^((~b4
)& b0
);
251 a44
= b4
^((~b0
)& b1
);
253 b3
= ROL64((a10
^d0
), 41);
254 b4
= ROL64((a41
^d1
), 2);
255 b0
= ROL64((a22
^d2
), 62);
256 b1
= ROL64((a03
^d3
), 55);
257 b2
= ROL64((a34
^d4
), 39);
258 a10
= b0
^((~b1
)& b2
);
259 a41
= b1
^((~b2
)& b3
);
260 a22
= b2
^((~b3
)& b4
);
261 a03
= b3
^((~b4
)& b0
);
262 a34
= b4
^((~b0
)& b1
);
264 c0
= a00
^a40
^a30
^a20
^a10
;
265 c1
= a31
^a21
^a11
^a01
^a41
;
266 c2
= a12
^a02
^a42
^a32
^a22
;
267 c3
= a43
^a33
^a23
^a13
^a03
;
268 c4
= a24
^a14
^a04
^a44
^a34
;
269 d0
= c4
^ROL64(c1
, 1);
270 d1
= c0
^ROL64(c2
, 1);
271 d2
= c1
^ROL64(c3
, 1);
272 d3
= c2
^ROL64(c4
, 1);
273 d4
= c3
^ROL64(c0
, 1);
276 b1
= ROL64((a21
^d1
), 44);
277 b2
= ROL64((a42
^d2
), 43);
278 b3
= ROL64((a13
^d3
), 21);
279 b4
= ROL64((a34
^d4
), 14);
280 a00
= b0
^((~b1
)& b2
);
282 a21
= b1
^((~b2
)& b3
);
283 a42
= b2
^((~b3
)& b4
);
284 a13
= b3
^((~b4
)& b0
);
285 a34
= b4
^((~b0
)& b1
);
287 b2
= ROL64((a30
^d0
), 3);
288 b3
= ROL64((a01
^d1
), 45);
289 b4
= ROL64((a22
^d2
), 61);
290 b0
= ROL64((a43
^d3
), 28);
291 b1
= ROL64((a14
^d4
), 20);
292 a30
= b0
^((~b1
)& b2
);
293 a01
= b1
^((~b2
)& b3
);
294 a22
= b2
^((~b3
)& b4
);
295 a43
= b3
^((~b4
)& b0
);
296 a14
= b4
^((~b0
)& b1
);
298 b4
= ROL64((a10
^d0
), 18);
299 b0
= ROL64((a31
^d1
), 1);
300 b1
= ROL64((a02
^d2
), 6);
301 b2
= ROL64((a23
^d3
), 25);
302 b3
= ROL64((a44
^d4
), 8);
303 a10
= b0
^((~b1
)& b2
);
304 a31
= b1
^((~b2
)& b3
);
305 a02
= b2
^((~b3
)& b4
);
306 a23
= b3
^((~b4
)& b0
);
307 a44
= b4
^((~b0
)& b1
);
309 b1
= ROL64((a40
^d0
), 36);
310 b2
= ROL64((a11
^d1
), 10);
311 b3
= ROL64((a32
^d2
), 15);
312 b4
= ROL64((a03
^d3
), 56);
313 b0
= ROL64((a24
^d4
), 27);
314 a40
= b0
^((~b1
)& b2
);
315 a11
= b1
^((~b2
)& b3
);
316 a32
= b2
^((~b3
)& b4
);
317 a03
= b3
^((~b4
)& b0
);
318 a24
= b4
^((~b0
)& b1
);
320 b3
= ROL64((a20
^d0
), 41);
321 b4
= ROL64((a41
^d1
), 2);
322 b0
= ROL64((a12
^d2
), 62);
323 b1
= ROL64((a33
^d3
), 55);
324 b2
= ROL64((a04
^d4
), 39);
325 a20
= b0
^((~b1
)& b2
);
326 a41
= b1
^((~b2
)& b3
);
327 a12
= b2
^((~b3
)& b4
);
328 a33
= b3
^((~b4
)& b0
);
329 a04
= b4
^((~b0
)& b1
);
331 c0
= a00
^a30
^a10
^a40
^a20
;
332 c1
= a21
^a01
^a31
^a11
^a41
;
333 c2
= a42
^a22
^a02
^a32
^a12
;
334 c3
= a13
^a43
^a23
^a03
^a33
;
335 c4
= a34
^a14
^a44
^a24
^a04
;
336 d0
= c4
^ROL64(c1
, 1);
337 d1
= c0
^ROL64(c2
, 1);
338 d2
= c1
^ROL64(c3
, 1);
339 d3
= c2
^ROL64(c4
, 1);
340 d4
= c3
^ROL64(c0
, 1);
343 b1
= ROL64((a01
^d1
), 44);
344 b2
= ROL64((a02
^d2
), 43);
345 b3
= ROL64((a03
^d3
), 21);
346 b4
= ROL64((a04
^d4
), 14);
347 a00
= b0
^((~b1
)& b2
);
349 a01
= b1
^((~b2
)& b3
);
350 a02
= b2
^((~b3
)& b4
);
351 a03
= b3
^((~b4
)& b0
);
352 a04
= b4
^((~b0
)& b1
);
354 b2
= ROL64((a10
^d0
), 3);
355 b3
= ROL64((a11
^d1
), 45);
356 b4
= ROL64((a12
^d2
), 61);
357 b0
= ROL64((a13
^d3
), 28);
358 b1
= ROL64((a14
^d4
), 20);
359 a10
= b0
^((~b1
)& b2
);
360 a11
= b1
^((~b2
)& b3
);
361 a12
= b2
^((~b3
)& b4
);
362 a13
= b3
^((~b4
)& b0
);
363 a14
= b4
^((~b0
)& b1
);
365 b4
= ROL64((a20
^d0
), 18);
366 b0
= ROL64((a21
^d1
), 1);
367 b1
= ROL64((a22
^d2
), 6);
368 b2
= ROL64((a23
^d3
), 25);
369 b3
= ROL64((a24
^d4
), 8);
370 a20
= b0
^((~b1
)& b2
);
371 a21
= b1
^((~b2
)& b3
);
372 a22
= b2
^((~b3
)& b4
);
373 a23
= b3
^((~b4
)& b0
);
374 a24
= b4
^((~b0
)& b1
);
376 b1
= ROL64((a30
^d0
), 36);
377 b2
= ROL64((a31
^d1
), 10);
378 b3
= ROL64((a32
^d2
), 15);
379 b4
= ROL64((a33
^d3
), 56);
380 b0
= ROL64((a34
^d4
), 27);
381 a30
= b0
^((~b1
)& b2
);
382 a31
= b1
^((~b2
)& b3
);
383 a32
= b2
^((~b3
)& b4
);
384 a33
= b3
^((~b4
)& b0
);
385 a34
= b4
^((~b0
)& b1
);
387 b3
= ROL64((a40
^d0
), 41);
388 b4
= ROL64((a41
^d1
), 2);
389 b0
= ROL64((a42
^d2
), 62);
390 b1
= ROL64((a43
^d3
), 55);
391 b2
= ROL64((a44
^d4
), 39);
392 a40
= b0
^((~b1
)& b2
);
393 a41
= b1
^((~b2
)& b3
);
394 a42
= b2
^((~b3
)& b4
);
395 a43
= b3
^((~b4
)& b0
);
396 a44
= b4
^((~b0
)& b1
);
401 ** Initialize a new hash. iSize determines the size of the hash
402 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
403 ** can be zero to use the default hash size of 256 bits.
405 static void SHA3Init(SHA3Context
*p
, int iSize
){
406 memset(p
, 0, sizeof(*p
));
407 if( iSize
>=128 && iSize
<=512 ){
408 p
->nRate
= (1600 - ((iSize
+ 31)&~31)*2)/8;
410 p
->nRate
= (1600 - 2*256)/8;
412 #if SHA3_BYTEORDER==1234
413 /* Known to be little-endian at compile-time. No-op */
414 #elif SHA3_BYTEORDER==4321
415 p
->ixMask
= 7; /* Big-endian */
418 static unsigned int one
= 1;
419 if( 1==*(unsigned char*)&one
){
420 /* Little endian. No byte swapping. */
423 /* Big endian. Byte swap. */
431 ** Make consecutive calls to the SHA3Update function to add new content
434 static void SHA3Update(
436 const unsigned char *aData
,
440 if( aData
==0 ) return;
441 #if SHA3_BYTEORDER==1234
442 if( (p
->nLoaded
% 8)==0 && ((aData
- (const unsigned char*)0)&7)==0 ){
443 for(; i
+7<nData
; i
+=8){
444 p
->u
.s
[p
->nLoaded
/8] ^= *(u64
*)&aData
[i
];
446 if( p
->nLoaded
>=p
->nRate
){
454 #if SHA3_BYTEORDER==1234
455 p
->u
.x
[p
->nLoaded
] ^= aData
[i
];
456 #elif SHA3_BYTEORDER==4321
457 p
->u
.x
[p
->nLoaded
^0x07] ^= aData
[i
];
459 p
->u
.x
[p
->nLoaded
^p
->ixMask
] ^= aData
[i
];
462 if( p
->nLoaded
==p
->nRate
){
470 ** After all content has been added, invoke SHA3Final() to compute
471 ** the final hash. The function returns a pointer to the binary
474 static unsigned char *SHA3Final(SHA3Context
*p
){
476 if( p
->nLoaded
==p
->nRate
-1 ){
477 const unsigned char c1
= 0x86;
478 SHA3Update(p
, &c1
, 1);
480 const unsigned char c2
= 0x06;
481 const unsigned char c3
= 0x80;
482 SHA3Update(p
, &c2
, 1);
483 p
->nLoaded
= p
->nRate
- 1;
484 SHA3Update(p
, &c3
, 1);
486 for(i
=0; i
<p
->nRate
; i
++){
487 p
->u
.x
[i
+p
->nRate
] = p
->u
.x
[i
^p
->ixMask
];
489 return &p
->u
.x
[p
->nRate
];
491 /* End of the hashing logic
492 *****************************************************************************/
495 ** Implementation of the sha3(X,SIZE) function.
497 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
498 ** size is 256. If X is a BLOB, it is hashed as is.
499 ** For all other non-NULL types of input, X is converted into a UTF-8 string
500 ** and the string is hashed without the trailing 0x00 terminator. The hash
501 ** of a NULL value is NULL.
503 static void sha3Func(
504 sqlite3_context
*context
,
509 int eType
= sqlite3_value_type(argv
[0]);
510 int nByte
= sqlite3_value_bytes(argv
[0]);
515 iSize
= sqlite3_value_int(argv
[1]);
516 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
517 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
522 if( eType
==SQLITE_NULL
) return;
523 SHA3Init(&cx
, iSize
);
524 if( eType
==SQLITE_BLOB
){
525 SHA3Update(&cx
, sqlite3_value_blob(argv
[0]), nByte
);
527 SHA3Update(&cx
, sqlite3_value_text(argv
[0]), nByte
);
529 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
532 /* Compute a string using sqlite3_vsnprintf() with a maximum length
533 ** of 50 bytes and add it to the hash.
535 static void sha3_step_vformat(
536 SHA3Context
*p
, /* Add content to this context */
543 va_start(ap
, zFormat
);
544 sqlite3_vsnprintf(sizeof(zBuf
),zBuf
,zFormat
,ap
);
546 n
= (int)strlen(zBuf
);
547 SHA3Update(p
, (unsigned char*)zBuf
, n
);
551 ** Implementation of the sha3_query(SQL,SIZE) function.
553 ** This function compiles and runs the SQL statement(s) given in the
554 ** argument. The results are hashed using a SIZE-bit SHA3. The default
557 ** The format of the byte stream that is hashed is summarized as follows:
567 ** <sql> is the original SQL text for each statement run and <n> is
568 ** the size of that text. The SQL text is UTF-8. A single R character
569 ** occurs before the start of each row. N means a NULL value.
570 ** I mean an 8-byte little-endian integer <int>. F is a floating point
571 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
572 ** B means blobs of <size> bytes. T means text rendered as <size>
573 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
576 ** For each SQL statement in the X input, there is one S segment. Each
577 ** S segment is followed by zero or more R segments, one for each row in the
578 ** result set. After each R, there are one or more N, I, F, B, or T segments,
579 ** one for each column in the result set. Segments are concatentated directly
580 ** with no delimiters of any kind.
582 static void sha3QueryFunc(
583 sqlite3_context
*context
,
587 sqlite3
*db
= sqlite3_context_db_handle(context
);
588 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
589 sqlite3_stmt
*pStmt
= 0;
590 int nCol
; /* Number of columns in the result set */
591 int i
; /* Loop counter */
601 iSize
= sqlite3_value_int(argv
[1]);
602 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
603 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
608 if( zSql
==0 ) return;
609 SHA3Init(&cx
, iSize
);
611 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zSql
);
613 char *zMsg
= sqlite3_mprintf("error SQL statement [%s]: %s",
614 zSql
, sqlite3_errmsg(db
));
615 sqlite3_finalize(pStmt
);
616 sqlite3_result_error(context
, zMsg
, -1);
620 if( !sqlite3_stmt_readonly(pStmt
) ){
621 char *zMsg
= sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt
));
622 sqlite3_finalize(pStmt
);
623 sqlite3_result_error(context
, zMsg
, -1);
627 nCol
= sqlite3_column_count(pStmt
);
628 z
= sqlite3_sql(pStmt
);
631 sha3_step_vformat(&cx
,"S%d:",n
);
632 SHA3Update(&cx
,(unsigned char*)z
,n
);
635 /* Compute a hash over the result of the query */
636 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
637 SHA3Update(&cx
,(const unsigned char*)"R",1);
638 for(i
=0; i
<nCol
; i
++){
639 switch( sqlite3_column_type(pStmt
,i
) ){
641 SHA3Update(&cx
, (const unsigned char*)"N",1);
644 case SQLITE_INTEGER
: {
648 sqlite3_int64 v
= sqlite3_column_int64(pStmt
,i
);
655 SHA3Update(&cx
, x
, 9);
662 double r
= sqlite3_column_double(pStmt
,i
);
673 int n2
= sqlite3_column_bytes(pStmt
, i
);
674 const unsigned char *z2
= sqlite3_column_text(pStmt
, i
);
675 sha3_step_vformat(&cx
,"T%d:",n2
);
676 SHA3Update(&cx
, z2
, n2
);
680 int n2
= sqlite3_column_bytes(pStmt
, i
);
681 const unsigned char *z2
= sqlite3_column_blob(pStmt
, i
);
682 sha3_step_vformat(&cx
,"B%d:",n2
);
683 SHA3Update(&cx
, z2
, n2
);
689 sqlite3_finalize(pStmt
);
691 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
696 __declspec(dllexport
)
698 int sqlite3_shathree_init(
701 const sqlite3_api_routines
*pApi
704 SQLITE_EXTENSION_INIT2(pApi
);
705 (void)pzErrMsg
; /* Unused parameter */
706 rc
= sqlite3_create_function(db
, "sha3", 1,
707 SQLITE_UTF8
| SQLITE_INNOCUOUS
| SQLITE_DETERMINISTIC
,
710 rc
= sqlite3_create_function(db
, "sha3", 2,
711 SQLITE_UTF8
| SQLITE_INNOCUOUS
| SQLITE_DETERMINISTIC
,
715 rc
= sqlite3_create_function(db
, "sha3_query", 1,
716 SQLITE_UTF8
| SQLITE_DIRECTONLY
,
717 0, sha3QueryFunc
, 0, 0);
720 rc
= sqlite3_create_function(db
, "sha3_query", 2,
721 SQLITE_UTF8
| SQLITE_DIRECTONLY
,
722 0, sha3QueryFunc
, 0, 0);