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 a functions that compute SHA1 hashes.
14 ** Two SQL functions are implemented:
19 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
22 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
23 ** and returns a hash of their results.
25 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
26 ** is used. If SIZE is included it must be one of the integers 224, 256,
27 ** 384, or 512, to determine SHA3 hash variant that is computed.
29 #include "sqlite3ext.h"
30 SQLITE_EXTENSION_INIT1
34 typedef sqlite3_uint64 u64
;
36 /******************************************************************************
40 ** Macros to determine whether the machine is big or little endian,
41 ** and whether or not that determination is run-time or compile-time.
43 ** For best performance, an attempt is made to guess at the byte-order
44 ** using C-preprocessor macros. If that is unsuccessful, or if
45 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
48 #ifndef SHA3_BYTEORDER
49 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
50 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
51 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
53 # define SHA3_BYTEORDER 1234
54 # elif defined(sparc) || defined(__ppc__)
55 # define SHA3_BYTEORDER 4321
57 # define SHA3_BYTEORDER 0
63 ** State structure for a SHA3 hash in progress
65 typedef struct SHA3Context SHA3Context
;
68 u64 s
[25]; /* Keccak state. 5x5 lines of 64 bits each */
69 unsigned char x
[1600]; /* ... or 1600 bytes */
71 unsigned nRate
; /* Bytes of input accepted per Keccak iteration */
72 unsigned nLoaded
; /* Input bytes loaded into u.x[] so far this cycle */
73 unsigned ixMask
; /* Insert next input into u.x[nLoaded^ixMask]. */
77 ** A single step of the Keccak mixing function for a 1600-bit state
79 static void KeccakF1600Step(SHA3Context
*p
){
81 u64 b0
, b1
, b2
, b3
, b4
;
82 u64 c0
, c1
, c2
, c3
, c4
;
83 u64 d0
, d1
, d2
, d3
, d4
;
84 static const u64 RC
[] = {
85 0x0000000000000001ULL
, 0x0000000000008082ULL
,
86 0x800000000000808aULL
, 0x8000000080008000ULL
,
87 0x000000000000808bULL
, 0x0000000080000001ULL
,
88 0x8000000080008081ULL
, 0x8000000000008009ULL
,
89 0x000000000000008aULL
, 0x0000000000000088ULL
,
90 0x0000000080008009ULL
, 0x000000008000000aULL
,
91 0x000000008000808bULL
, 0x800000000000008bULL
,
92 0x8000000000008089ULL
, 0x8000000000008003ULL
,
93 0x8000000000008002ULL
, 0x8000000000000080ULL
,
94 0x000000000000800aULL
, 0x800000008000000aULL
,
95 0x8000000080008081ULL
, 0x8000000000008080ULL
,
96 0x0000000080000001ULL
, 0x8000000080008008ULL
98 # define a00 (p->u.s[0])
99 # define a01 (p->u.s[1])
100 # define a02 (p->u.s[2])
101 # define a03 (p->u.s[3])
102 # define a04 (p->u.s[4])
103 # define a10 (p->u.s[5])
104 # define a11 (p->u.s[6])
105 # define a12 (p->u.s[7])
106 # define a13 (p->u.s[8])
107 # define a14 (p->u.s[9])
108 # define a20 (p->u.s[10])
109 # define a21 (p->u.s[11])
110 # define a22 (p->u.s[12])
111 # define a23 (p->u.s[13])
112 # define a24 (p->u.s[14])
113 # define a30 (p->u.s[15])
114 # define a31 (p->u.s[16])
115 # define a32 (p->u.s[17])
116 # define a33 (p->u.s[18])
117 # define a34 (p->u.s[19])
118 # define a40 (p->u.s[20])
119 # define a41 (p->u.s[21])
120 # define a42 (p->u.s[22])
121 # define a43 (p->u.s[23])
122 # define a44 (p->u.s[24])
123 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
125 for(i
=0; i
<24; i
+=4){
126 c0
= a00
^a10
^a20
^a30
^a40
;
127 c1
= a01
^a11
^a21
^a31
^a41
;
128 c2
= a02
^a12
^a22
^a32
^a42
;
129 c3
= a03
^a13
^a23
^a33
^a43
;
130 c4
= a04
^a14
^a24
^a34
^a44
;
131 d0
= c4
^ROL64(c1
, 1);
132 d1
= c0
^ROL64(c2
, 1);
133 d2
= c1
^ROL64(c3
, 1);
134 d3
= c2
^ROL64(c4
, 1);
135 d4
= c3
^ROL64(c0
, 1);
138 b1
= ROL64((a11
^d1
), 44);
139 b2
= ROL64((a22
^d2
), 43);
140 b3
= ROL64((a33
^d3
), 21);
141 b4
= ROL64((a44
^d4
), 14);
142 a00
= b0
^((~b1
)& b2
);
144 a11
= b1
^((~b2
)& b3
);
145 a22
= b2
^((~b3
)& b4
);
146 a33
= b3
^((~b4
)& b0
);
147 a44
= b4
^((~b0
)& b1
);
149 b2
= ROL64((a20
^d0
), 3);
150 b3
= ROL64((a31
^d1
), 45);
151 b4
= ROL64((a42
^d2
), 61);
152 b0
= ROL64((a03
^d3
), 28);
153 b1
= ROL64((a14
^d4
), 20);
154 a20
= b0
^((~b1
)& b2
);
155 a31
= b1
^((~b2
)& b3
);
156 a42
= b2
^((~b3
)& b4
);
157 a03
= b3
^((~b4
)& b0
);
158 a14
= b4
^((~b0
)& b1
);
160 b4
= ROL64((a40
^d0
), 18);
161 b0
= ROL64((a01
^d1
), 1);
162 b1
= ROL64((a12
^d2
), 6);
163 b2
= ROL64((a23
^d3
), 25);
164 b3
= ROL64((a34
^d4
), 8);
165 a40
= b0
^((~b1
)& b2
);
166 a01
= b1
^((~b2
)& b3
);
167 a12
= b2
^((~b3
)& b4
);
168 a23
= b3
^((~b4
)& b0
);
169 a34
= b4
^((~b0
)& b1
);
171 b1
= ROL64((a10
^d0
), 36);
172 b2
= ROL64((a21
^d1
), 10);
173 b3
= ROL64((a32
^d2
), 15);
174 b4
= ROL64((a43
^d3
), 56);
175 b0
= ROL64((a04
^d4
), 27);
176 a10
= b0
^((~b1
)& b2
);
177 a21
= b1
^((~b2
)& b3
);
178 a32
= b2
^((~b3
)& b4
);
179 a43
= b3
^((~b4
)& b0
);
180 a04
= b4
^((~b0
)& b1
);
182 b3
= ROL64((a30
^d0
), 41);
183 b4
= ROL64((a41
^d1
), 2);
184 b0
= ROL64((a02
^d2
), 62);
185 b1
= ROL64((a13
^d3
), 55);
186 b2
= ROL64((a24
^d4
), 39);
187 a30
= b0
^((~b1
)& b2
);
188 a41
= b1
^((~b2
)& b3
);
189 a02
= b2
^((~b3
)& b4
);
190 a13
= b3
^((~b4
)& b0
);
191 a24
= b4
^((~b0
)& b1
);
193 c0
= a00
^a20
^a40
^a10
^a30
;
194 c1
= a11
^a31
^a01
^a21
^a41
;
195 c2
= a22
^a42
^a12
^a32
^a02
;
196 c3
= a33
^a03
^a23
^a43
^a13
;
197 c4
= a44
^a14
^a34
^a04
^a24
;
198 d0
= c4
^ROL64(c1
, 1);
199 d1
= c0
^ROL64(c2
, 1);
200 d2
= c1
^ROL64(c3
, 1);
201 d3
= c2
^ROL64(c4
, 1);
202 d4
= c3
^ROL64(c0
, 1);
205 b1
= ROL64((a31
^d1
), 44);
206 b2
= ROL64((a12
^d2
), 43);
207 b3
= ROL64((a43
^d3
), 21);
208 b4
= ROL64((a24
^d4
), 14);
209 a00
= b0
^((~b1
)& b2
);
211 a31
= b1
^((~b2
)& b3
);
212 a12
= b2
^((~b3
)& b4
);
213 a43
= b3
^((~b4
)& b0
);
214 a24
= b4
^((~b0
)& b1
);
216 b2
= ROL64((a40
^d0
), 3);
217 b3
= ROL64((a21
^d1
), 45);
218 b4
= ROL64((a02
^d2
), 61);
219 b0
= ROL64((a33
^d3
), 28);
220 b1
= ROL64((a14
^d4
), 20);
221 a40
= b0
^((~b1
)& b2
);
222 a21
= b1
^((~b2
)& b3
);
223 a02
= b2
^((~b3
)& b4
);
224 a33
= b3
^((~b4
)& b0
);
225 a14
= b4
^((~b0
)& b1
);
227 b4
= ROL64((a30
^d0
), 18);
228 b0
= ROL64((a11
^d1
), 1);
229 b1
= ROL64((a42
^d2
), 6);
230 b2
= ROL64((a23
^d3
), 25);
231 b3
= ROL64((a04
^d4
), 8);
232 a30
= b0
^((~b1
)& b2
);
233 a11
= b1
^((~b2
)& b3
);
234 a42
= b2
^((~b3
)& b4
);
235 a23
= b3
^((~b4
)& b0
);
236 a04
= b4
^((~b0
)& b1
);
238 b1
= ROL64((a20
^d0
), 36);
239 b2
= ROL64((a01
^d1
), 10);
240 b3
= ROL64((a32
^d2
), 15);
241 b4
= ROL64((a13
^d3
), 56);
242 b0
= ROL64((a44
^d4
), 27);
243 a20
= b0
^((~b1
)& b2
);
244 a01
= b1
^((~b2
)& b3
);
245 a32
= b2
^((~b3
)& b4
);
246 a13
= b3
^((~b4
)& b0
);
247 a44
= b4
^((~b0
)& b1
);
249 b3
= ROL64((a10
^d0
), 41);
250 b4
= ROL64((a41
^d1
), 2);
251 b0
= ROL64((a22
^d2
), 62);
252 b1
= ROL64((a03
^d3
), 55);
253 b2
= ROL64((a34
^d4
), 39);
254 a10
= b0
^((~b1
)& b2
);
255 a41
= b1
^((~b2
)& b3
);
256 a22
= b2
^((~b3
)& b4
);
257 a03
= b3
^((~b4
)& b0
);
258 a34
= b4
^((~b0
)& b1
);
260 c0
= a00
^a40
^a30
^a20
^a10
;
261 c1
= a31
^a21
^a11
^a01
^a41
;
262 c2
= a12
^a02
^a42
^a32
^a22
;
263 c3
= a43
^a33
^a23
^a13
^a03
;
264 c4
= a24
^a14
^a04
^a44
^a34
;
265 d0
= c4
^ROL64(c1
, 1);
266 d1
= c0
^ROL64(c2
, 1);
267 d2
= c1
^ROL64(c3
, 1);
268 d3
= c2
^ROL64(c4
, 1);
269 d4
= c3
^ROL64(c0
, 1);
272 b1
= ROL64((a21
^d1
), 44);
273 b2
= ROL64((a42
^d2
), 43);
274 b3
= ROL64((a13
^d3
), 21);
275 b4
= ROL64((a34
^d4
), 14);
276 a00
= b0
^((~b1
)& b2
);
278 a21
= b1
^((~b2
)& b3
);
279 a42
= b2
^((~b3
)& b4
);
280 a13
= b3
^((~b4
)& b0
);
281 a34
= b4
^((~b0
)& b1
);
283 b2
= ROL64((a30
^d0
), 3);
284 b3
= ROL64((a01
^d1
), 45);
285 b4
= ROL64((a22
^d2
), 61);
286 b0
= ROL64((a43
^d3
), 28);
287 b1
= ROL64((a14
^d4
), 20);
288 a30
= b0
^((~b1
)& b2
);
289 a01
= b1
^((~b2
)& b3
);
290 a22
= b2
^((~b3
)& b4
);
291 a43
= b3
^((~b4
)& b0
);
292 a14
= b4
^((~b0
)& b1
);
294 b4
= ROL64((a10
^d0
), 18);
295 b0
= ROL64((a31
^d1
), 1);
296 b1
= ROL64((a02
^d2
), 6);
297 b2
= ROL64((a23
^d3
), 25);
298 b3
= ROL64((a44
^d4
), 8);
299 a10
= b0
^((~b1
)& b2
);
300 a31
= b1
^((~b2
)& b3
);
301 a02
= b2
^((~b3
)& b4
);
302 a23
= b3
^((~b4
)& b0
);
303 a44
= b4
^((~b0
)& b1
);
305 b1
= ROL64((a40
^d0
), 36);
306 b2
= ROL64((a11
^d1
), 10);
307 b3
= ROL64((a32
^d2
), 15);
308 b4
= ROL64((a03
^d3
), 56);
309 b0
= ROL64((a24
^d4
), 27);
310 a40
= b0
^((~b1
)& b2
);
311 a11
= b1
^((~b2
)& b3
);
312 a32
= b2
^((~b3
)& b4
);
313 a03
= b3
^((~b4
)& b0
);
314 a24
= b4
^((~b0
)& b1
);
316 b3
= ROL64((a20
^d0
), 41);
317 b4
= ROL64((a41
^d1
), 2);
318 b0
= ROL64((a12
^d2
), 62);
319 b1
= ROL64((a33
^d3
), 55);
320 b2
= ROL64((a04
^d4
), 39);
321 a20
= b0
^((~b1
)& b2
);
322 a41
= b1
^((~b2
)& b3
);
323 a12
= b2
^((~b3
)& b4
);
324 a33
= b3
^((~b4
)& b0
);
325 a04
= b4
^((~b0
)& b1
);
327 c0
= a00
^a30
^a10
^a40
^a20
;
328 c1
= a21
^a01
^a31
^a11
^a41
;
329 c2
= a42
^a22
^a02
^a32
^a12
;
330 c3
= a13
^a43
^a23
^a03
^a33
;
331 c4
= a34
^a14
^a44
^a24
^a04
;
332 d0
= c4
^ROL64(c1
, 1);
333 d1
= c0
^ROL64(c2
, 1);
334 d2
= c1
^ROL64(c3
, 1);
335 d3
= c2
^ROL64(c4
, 1);
336 d4
= c3
^ROL64(c0
, 1);
339 b1
= ROL64((a01
^d1
), 44);
340 b2
= ROL64((a02
^d2
), 43);
341 b3
= ROL64((a03
^d3
), 21);
342 b4
= ROL64((a04
^d4
), 14);
343 a00
= b0
^((~b1
)& b2
);
345 a01
= b1
^((~b2
)& b3
);
346 a02
= b2
^((~b3
)& b4
);
347 a03
= b3
^((~b4
)& b0
);
348 a04
= b4
^((~b0
)& b1
);
350 b2
= ROL64((a10
^d0
), 3);
351 b3
= ROL64((a11
^d1
), 45);
352 b4
= ROL64((a12
^d2
), 61);
353 b0
= ROL64((a13
^d3
), 28);
354 b1
= ROL64((a14
^d4
), 20);
355 a10
= b0
^((~b1
)& b2
);
356 a11
= b1
^((~b2
)& b3
);
357 a12
= b2
^((~b3
)& b4
);
358 a13
= b3
^((~b4
)& b0
);
359 a14
= b4
^((~b0
)& b1
);
361 b4
= ROL64((a20
^d0
), 18);
362 b0
= ROL64((a21
^d1
), 1);
363 b1
= ROL64((a22
^d2
), 6);
364 b2
= ROL64((a23
^d3
), 25);
365 b3
= ROL64((a24
^d4
), 8);
366 a20
= b0
^((~b1
)& b2
);
367 a21
= b1
^((~b2
)& b3
);
368 a22
= b2
^((~b3
)& b4
);
369 a23
= b3
^((~b4
)& b0
);
370 a24
= b4
^((~b0
)& b1
);
372 b1
= ROL64((a30
^d0
), 36);
373 b2
= ROL64((a31
^d1
), 10);
374 b3
= ROL64((a32
^d2
), 15);
375 b4
= ROL64((a33
^d3
), 56);
376 b0
= ROL64((a34
^d4
), 27);
377 a30
= b0
^((~b1
)& b2
);
378 a31
= b1
^((~b2
)& b3
);
379 a32
= b2
^((~b3
)& b4
);
380 a33
= b3
^((~b4
)& b0
);
381 a34
= b4
^((~b0
)& b1
);
383 b3
= ROL64((a40
^d0
), 41);
384 b4
= ROL64((a41
^d1
), 2);
385 b0
= ROL64((a42
^d2
), 62);
386 b1
= ROL64((a43
^d3
), 55);
387 b2
= ROL64((a44
^d4
), 39);
388 a40
= b0
^((~b1
)& b2
);
389 a41
= b1
^((~b2
)& b3
);
390 a42
= b2
^((~b3
)& b4
);
391 a43
= b3
^((~b4
)& b0
);
392 a44
= b4
^((~b0
)& b1
);
397 ** Initialize a new hash. iSize determines the size of the hash
398 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
399 ** can be zero to use the default hash size of 256 bits.
401 static void SHA3Init(SHA3Context
*p
, int iSize
){
402 memset(p
, 0, sizeof(*p
));
403 if( iSize
>=128 && iSize
<=512 ){
404 p
->nRate
= (1600 - ((iSize
+ 31)&~31)*2)/8;
406 p
->nRate
= (1600 - 2*256)/8;
408 #if SHA3_BYTEORDER==1234
409 /* Known to be little-endian at compile-time. No-op */
410 #elif SHA3_BYTEORDER==4321
411 p
->ixMask
= 7; /* Big-endian */
414 static unsigned int one
= 1;
415 if( 1==*(unsigned char*)&one
){
416 /* Little endian. No byte swapping. */
419 /* Big endian. Byte swap. */
427 ** Make consecutive calls to the SHA3Update function to add new content
430 static void SHA3Update(
432 const unsigned char *aData
,
436 #if SHA3_BYTEORDER==1234
437 if( (p
->nLoaded
% 8)==0 && ((aData
- (const unsigned char*)0)&7)==0 ){
438 for(; i
+7<nData
; i
+=8){
439 p
->u
.s
[p
->nLoaded
/8] ^= *(u64
*)&aData
[i
];
441 if( p
->nLoaded
>=p
->nRate
){
449 #if SHA3_BYTEORDER==1234
450 p
->u
.x
[p
->nLoaded
] ^= aData
[i
];
451 #elif SHA3_BYTEORDER==4321
452 p
->u
.x
[p
->nLoaded
^0x07] ^= aData
[i
];
454 p
->u
.x
[p
->nLoaded
^p
->ixMask
] ^= aData
[i
];
457 if( p
->nLoaded
==p
->nRate
){
465 ** After all content has been added, invoke SHA3Final() to compute
466 ** the final hash. The function returns a pointer to the binary
469 static unsigned char *SHA3Final(SHA3Context
*p
){
471 if( p
->nLoaded
==p
->nRate
-1 ){
472 const unsigned char c1
= 0x86;
473 SHA3Update(p
, &c1
, 1);
475 const unsigned char c2
= 0x06;
476 const unsigned char c3
= 0x80;
477 SHA3Update(p
, &c2
, 1);
478 p
->nLoaded
= p
->nRate
- 1;
479 SHA3Update(p
, &c3
, 1);
481 for(i
=0; i
<p
->nRate
; i
++){
482 p
->u
.x
[i
+p
->nRate
] = p
->u
.x
[i
^p
->ixMask
];
484 return &p
->u
.x
[p
->nRate
];
486 /* End of the hashing logic
487 *****************************************************************************/
490 ** Implementation of the sha3(X,SIZE) function.
492 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
493 ** size is 256. If X is a BLOB, it is hashed as is.
494 ** For all other non-NULL types of input, X is converted into a UTF-8 string
495 ** and the string is hashed without the trailing 0x00 terminator. The hash
496 ** of a NULL value is NULL.
498 static void sha3Func(
499 sqlite3_context
*context
,
504 int eType
= sqlite3_value_type(argv
[0]);
505 int nByte
= sqlite3_value_bytes(argv
[0]);
510 iSize
= sqlite3_value_int(argv
[1]);
511 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
512 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
517 if( eType
==SQLITE_NULL
) return;
518 SHA3Init(&cx
, iSize
);
519 if( eType
==SQLITE_BLOB
){
520 SHA3Update(&cx
, sqlite3_value_blob(argv
[0]), nByte
);
522 SHA3Update(&cx
, sqlite3_value_text(argv
[0]), nByte
);
524 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
527 /* Compute a string using sqlite3_vsnprintf() with a maximum length
528 ** of 50 bytes and add it to the hash.
530 static void hash_step_vformat(
531 SHA3Context
*p
, /* Add content to this context */
538 va_start(ap
, zFormat
);
539 sqlite3_vsnprintf(sizeof(zBuf
),zBuf
,zFormat
,ap
);
541 n
= (int)strlen(zBuf
);
542 SHA3Update(p
, (unsigned char*)zBuf
, n
);
546 ** Implementation of the sha3_query(SQL,SIZE) function.
548 ** This function compiles and runs the SQL statement(s) given in the
549 ** argument. The results are hashed using a SIZE-bit SHA3. The default
552 ** The format of the byte stream that is hashed is summarized as follows:
562 ** <sql> is the original SQL text for each statement run and <n> is
563 ** the size of that text. The SQL text is UTF-8. A single R character
564 ** occurs before the start of each row. N means a NULL value.
565 ** I mean an 8-byte little-endian integer <int>. F is a floating point
566 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
567 ** B means blobs of <size> bytes. T means text rendered as <size>
568 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
571 ** For each SQL statement in the X input, there is one S segment. Each
572 ** S segment is followed by zero or more R segments, one for each row in the
573 ** result set. After each R, there are one or more N, I, F, B, or T segments,
574 ** one for each column in the result set. Segments are concatentated directly
575 ** with no delimiters of any kind.
577 static void sha3QueryFunc(
578 sqlite3_context
*context
,
582 sqlite3
*db
= sqlite3_context_db_handle(context
);
583 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
584 sqlite3_stmt
*pStmt
= 0;
585 int nCol
; /* Number of columns in the result set */
586 int i
; /* Loop counter */
596 iSize
= sqlite3_value_int(argv
[1]);
597 if( iSize
!=224 && iSize
!=256 && iSize
!=384 && iSize
!=512 ){
598 sqlite3_result_error(context
, "SHA3 size should be one of: 224 256 "
603 if( zSql
==0 ) return;
604 SHA3Init(&cx
, iSize
);
606 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zSql
);
608 char *zMsg
= sqlite3_mprintf("error SQL statement [%s]: %s",
609 zSql
, sqlite3_errmsg(db
));
610 sqlite3_finalize(pStmt
);
611 sqlite3_result_error(context
, zMsg
, -1);
615 if( !sqlite3_stmt_readonly(pStmt
) ){
616 char *zMsg
= sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt
));
617 sqlite3_finalize(pStmt
);
618 sqlite3_result_error(context
, zMsg
, -1);
622 nCol
= sqlite3_column_count(pStmt
);
623 z
= sqlite3_sql(pStmt
);
625 hash_step_vformat(&cx
,"S%d:",n
);
626 SHA3Update(&cx
,(unsigned char*)z
,n
);
628 /* Compute a hash over the result of the query */
629 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
630 SHA3Update(&cx
,(const unsigned char*)"R",1);
631 for(i
=0; i
<nCol
; i
++){
632 switch( sqlite3_column_type(pStmt
,i
) ){
634 SHA3Update(&cx
, (const unsigned char*)"N",1);
637 case SQLITE_INTEGER
: {
641 sqlite3_int64 v
= sqlite3_column_int64(pStmt
,i
);
648 SHA3Update(&cx
, x
, 9);
655 double r
= sqlite3_column_double(pStmt
,i
);
666 int n2
= sqlite3_column_bytes(pStmt
, i
);
667 const unsigned char *z2
= sqlite3_column_text(pStmt
, i
);
668 hash_step_vformat(&cx
,"T%d:",n2
);
669 SHA3Update(&cx
, z2
, n2
);
673 int n2
= sqlite3_column_bytes(pStmt
, i
);
674 const unsigned char *z2
= sqlite3_column_blob(pStmt
, i
);
675 hash_step_vformat(&cx
,"B%d:",n2
);
676 SHA3Update(&cx
, z2
, n2
);
682 sqlite3_finalize(pStmt
);
684 sqlite3_result_blob(context
, SHA3Final(&cx
), iSize
/8, SQLITE_TRANSIENT
);
689 __declspec(dllexport
)
691 int sqlite3_shathree_init(
694 const sqlite3_api_routines
*pApi
697 SQLITE_EXTENSION_INIT2(pApi
);
698 (void)pzErrMsg
; /* Unused parameter */
699 rc
= sqlite3_create_function(db
, "sha3", 1, SQLITE_UTF8
, 0,
702 rc
= sqlite3_create_function(db
, "sha3", 2, SQLITE_UTF8
, 0,
706 rc
= sqlite3_create_function(db
, "sha3_query", 1, SQLITE_UTF8
, 0,
707 sha3QueryFunc
, 0, 0);
710 rc
= sqlite3_create_function(db
, "sha3_query", 2, SQLITE_UTF8
, 0,
711 sha3QueryFunc
, 0, 0);