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 SHA1 hashes.
14 ** Two SQL functions are implemented:
19 ** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
22 ** The sha1_query(Y) function evalutes all queries in the SQL statements of Y
23 ** and returns a hash of their results.
25 #include "sqlite3ext.h"
26 SQLITE_EXTENSION_INIT1
31 /******************************************************************************
34 /* Context for the SHA1 hash */
35 typedef struct SHA1Context SHA1Context
;
37 unsigned int state
[5];
38 unsigned int count
[2];
39 unsigned char buffer
[64];
42 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
43 #define rol(x,k) SHA_ROT(x,k,32-(k))
44 #define ror(x,k) SHA_ROT(x,32-(k),k)
46 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
47 |(rol(block[i],8)&0x00FF00FF))
48 #define blk0be(i) block[i]
49 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
50 ^block[(i+2)&15]^block[i&15],1))
53 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
55 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
56 * determined at run-time.
58 #define Rl0(v,w,x,y,z,i) \
59 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
60 #define Rb0(v,w,x,y,z,i) \
61 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
62 #define R1(v,w,x,y,z,i) \
63 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
64 #define R2(v,w,x,y,z,i) \
65 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
66 #define R3(v,w,x,y,z,i) \
67 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
68 #define R4(v,w,x,y,z,i) \
69 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
72 * Hash a single 512-bit block. This is the core of the algorithm.
74 static void SHA1Transform(unsigned int state
[5], const unsigned char buffer
[64]){
75 unsigned int qq
[5]; /* a, b, c, d, e; */
77 unsigned int block
[16];
78 memcpy(block
, buffer
, 64);
79 memcpy(qq
,state
,5*sizeof(unsigned int));
87 /* Copy p->state[] to working vars */
96 /* 4 rounds of 20 operations each. Loop unrolled. */
97 if( 1 == *(unsigned char*)&one
){
98 Rl0(a
,b
,c
,d
,e
, 0); Rl0(e
,a
,b
,c
,d
, 1); Rl0(d
,e
,a
,b
,c
, 2); Rl0(c
,d
,e
,a
,b
, 3);
99 Rl0(b
,c
,d
,e
,a
, 4); Rl0(a
,b
,c
,d
,e
, 5); Rl0(e
,a
,b
,c
,d
, 6); Rl0(d
,e
,a
,b
,c
, 7);
100 Rl0(c
,d
,e
,a
,b
, 8); Rl0(b
,c
,d
,e
,a
, 9); Rl0(a
,b
,c
,d
,e
,10); Rl0(e
,a
,b
,c
,d
,11);
101 Rl0(d
,e
,a
,b
,c
,12); Rl0(c
,d
,e
,a
,b
,13); Rl0(b
,c
,d
,e
,a
,14); Rl0(a
,b
,c
,d
,e
,15);
103 Rb0(a
,b
,c
,d
,e
, 0); Rb0(e
,a
,b
,c
,d
, 1); Rb0(d
,e
,a
,b
,c
, 2); Rb0(c
,d
,e
,a
,b
, 3);
104 Rb0(b
,c
,d
,e
,a
, 4); Rb0(a
,b
,c
,d
,e
, 5); Rb0(e
,a
,b
,c
,d
, 6); Rb0(d
,e
,a
,b
,c
, 7);
105 Rb0(c
,d
,e
,a
,b
, 8); Rb0(b
,c
,d
,e
,a
, 9); Rb0(a
,b
,c
,d
,e
,10); Rb0(e
,a
,b
,c
,d
,11);
106 Rb0(d
,e
,a
,b
,c
,12); Rb0(c
,d
,e
,a
,b
,13); Rb0(b
,c
,d
,e
,a
,14); Rb0(a
,b
,c
,d
,e
,15);
108 R1(e
,a
,b
,c
,d
,16); R1(d
,e
,a
,b
,c
,17); R1(c
,d
,e
,a
,b
,18); R1(b
,c
,d
,e
,a
,19);
109 R2(a
,b
,c
,d
,e
,20); R2(e
,a
,b
,c
,d
,21); R2(d
,e
,a
,b
,c
,22); R2(c
,d
,e
,a
,b
,23);
110 R2(b
,c
,d
,e
,a
,24); R2(a
,b
,c
,d
,e
,25); R2(e
,a
,b
,c
,d
,26); R2(d
,e
,a
,b
,c
,27);
111 R2(c
,d
,e
,a
,b
,28); R2(b
,c
,d
,e
,a
,29); R2(a
,b
,c
,d
,e
,30); R2(e
,a
,b
,c
,d
,31);
112 R2(d
,e
,a
,b
,c
,32); R2(c
,d
,e
,a
,b
,33); R2(b
,c
,d
,e
,a
,34); R2(a
,b
,c
,d
,e
,35);
113 R2(e
,a
,b
,c
,d
,36); R2(d
,e
,a
,b
,c
,37); R2(c
,d
,e
,a
,b
,38); R2(b
,c
,d
,e
,a
,39);
114 R3(a
,b
,c
,d
,e
,40); R3(e
,a
,b
,c
,d
,41); R3(d
,e
,a
,b
,c
,42); R3(c
,d
,e
,a
,b
,43);
115 R3(b
,c
,d
,e
,a
,44); R3(a
,b
,c
,d
,e
,45); R3(e
,a
,b
,c
,d
,46); R3(d
,e
,a
,b
,c
,47);
116 R3(c
,d
,e
,a
,b
,48); R3(b
,c
,d
,e
,a
,49); R3(a
,b
,c
,d
,e
,50); R3(e
,a
,b
,c
,d
,51);
117 R3(d
,e
,a
,b
,c
,52); R3(c
,d
,e
,a
,b
,53); R3(b
,c
,d
,e
,a
,54); R3(a
,b
,c
,d
,e
,55);
118 R3(e
,a
,b
,c
,d
,56); R3(d
,e
,a
,b
,c
,57); R3(c
,d
,e
,a
,b
,58); R3(b
,c
,d
,e
,a
,59);
119 R4(a
,b
,c
,d
,e
,60); R4(e
,a
,b
,c
,d
,61); R4(d
,e
,a
,b
,c
,62); R4(c
,d
,e
,a
,b
,63);
120 R4(b
,c
,d
,e
,a
,64); R4(a
,b
,c
,d
,e
,65); R4(e
,a
,b
,c
,d
,66); R4(d
,e
,a
,b
,c
,67);
121 R4(c
,d
,e
,a
,b
,68); R4(b
,c
,d
,e
,a
,69); R4(a
,b
,c
,d
,e
,70); R4(e
,a
,b
,c
,d
,71);
122 R4(d
,e
,a
,b
,c
,72); R4(c
,d
,e
,a
,b
,73); R4(b
,c
,d
,e
,a
,74); R4(a
,b
,c
,d
,e
,75);
123 R4(e
,a
,b
,c
,d
,76); R4(d
,e
,a
,b
,c
,77); R4(c
,d
,e
,a
,b
,78); R4(b
,c
,d
,e
,a
,79);
125 /* Add the working vars back into context.state[] */
140 /* Initialize a SHA1 context */
141 static void hash_init(SHA1Context
*p
){
142 /* SHA1 initialization constants */
143 p
->state
[0] = 0x67452301;
144 p
->state
[1] = 0xEFCDAB89;
145 p
->state
[2] = 0x98BADCFE;
146 p
->state
[3] = 0x10325476;
147 p
->state
[4] = 0xC3D2E1F0;
148 p
->count
[0] = p
->count
[1] = 0;
151 /* Add new content to the SHA1 hash */
152 static void hash_step(
153 SHA1Context
*p
, /* Add content to this context */
154 const unsigned char *data
, /* Data to be added */
155 unsigned int len
/* Number of bytes in data */
160 if( (p
->count
[0] += len
<< 3) < j
){
161 p
->count
[1] += (len
>>29)+1;
164 if( (j
+ len
) > 63 ){
165 (void)memcpy(&p
->buffer
[j
], data
, (i
= 64-j
));
166 SHA1Transform(p
->state
, p
->buffer
);
167 for(; i
+ 63 < len
; i
+= 64){
168 SHA1Transform(p
->state
, &data
[i
]);
174 (void)memcpy(&p
->buffer
[j
], &data
[i
], len
- i
);
177 /* Compute a string using sqlite3_vsnprintf() and hash it */
178 static void hash_step_vformat(
179 SHA1Context
*p
, /* Add content to this context */
186 va_start(ap
, zFormat
);
187 sqlite3_vsnprintf(sizeof(zBuf
),zBuf
,zFormat
,ap
);
189 n
= (int)strlen(zBuf
);
190 hash_step(p
, (unsigned char*)zBuf
, n
);
194 /* Add padding and compute the message digest. Render the
195 ** message digest as lower-case hexadecimal and put it into
196 ** zOut[]. zOut[] must be at least 41 bytes long. */
197 static void hash_finish(
198 SHA1Context
*p
, /* The SHA1 context to finish and render */
199 char *zOut
/* Store hexadecimal hash here */
202 unsigned char finalcount
[8];
203 unsigned char digest
[20];
204 static const char zEncode
[] = "0123456789abcdef";
206 for (i
= 0; i
< 8; i
++){
207 finalcount
[i
] = (unsigned char)((p
->count
[(i
>= 4 ? 0 : 1)]
208 >> ((3-(i
& 3)) * 8) ) & 255); /* Endian independent */
210 hash_step(p
, (const unsigned char *)"\200", 1);
211 while ((p
->count
[0] & 504) != 448){
212 hash_step(p
, (const unsigned char *)"\0", 1);
214 hash_step(p
, finalcount
, 8); /* Should cause a SHA1Transform() */
215 for (i
= 0; i
< 20; i
++){
216 digest
[i
] = (unsigned char)((p
->state
[i
>>2] >> ((3-(i
& 3)) * 8) ) & 255);
219 zOut
[i
*2] = zEncode
[(digest
[i
]>>4)&0xf];
220 zOut
[i
*2+1] = zEncode
[digest
[i
] & 0xf];
224 /* End of the hashing logic
225 *****************************************************************************/
228 ** Implementation of the sha1(X) function.
230 ** Return a lower-case hexadecimal rendering of the SHA1 hash of the
231 ** argument X. If X is a BLOB, it is hashed as is. For all other
232 ** types of input, X is converted into a UTF-8 string and the string
233 ** is hash without the trailing 0x00 terminator. The hash of a NULL
236 static void sha1Func(
237 sqlite3_context
*context
,
242 int eType
= sqlite3_value_type(argv
[0]);
243 int nByte
= sqlite3_value_bytes(argv
[0]);
247 if( eType
==SQLITE_NULL
) return;
249 if( eType
==SQLITE_BLOB
){
250 hash_step(&cx
, sqlite3_value_blob(argv
[0]), nByte
);
252 hash_step(&cx
, sqlite3_value_text(argv
[0]), nByte
);
254 hash_finish(&cx
, zOut
);
255 sqlite3_result_text(context
, zOut
, 40, SQLITE_TRANSIENT
);
259 ** Implementation of the sha1_query(SQL) function.
261 ** This function compiles and runs the SQL statement(s) given in the
262 ** argument. The results are hashed using SHA1 and that hash is returned.
264 ** The original SQL text is included as part of the hash.
266 ** The hash is not just a concatenation of the outputs. Each query
267 ** is delimited and each row and value within the query is delimited,
268 ** with all values being marked with their datatypes.
270 static void sha1QueryFunc(
271 sqlite3_context
*context
,
275 sqlite3
*db
= sqlite3_context_db_handle(context
);
276 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
277 sqlite3_stmt
*pStmt
= 0;
278 int nCol
; /* Number of columns in the result set */
279 int i
; /* Loop counter */
287 if( zSql
==0 ) return;
290 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zSql
);
292 char *zMsg
= sqlite3_mprintf("error SQL statement [%s]: %s",
293 zSql
, sqlite3_errmsg(db
));
294 sqlite3_finalize(pStmt
);
295 sqlite3_result_error(context
, zMsg
, -1);
299 if( !sqlite3_stmt_readonly(pStmt
) ){
300 char *zMsg
= sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt
));
301 sqlite3_finalize(pStmt
);
302 sqlite3_result_error(context
, zMsg
, -1);
306 nCol
= sqlite3_column_count(pStmt
);
307 z
= sqlite3_sql(pStmt
);
309 hash_step_vformat(&cx
,"S%d:",n
);
310 hash_step(&cx
,(unsigned char*)z
,n
);
312 /* Compute a hash over the result of the query */
313 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
314 hash_step(&cx
,(const unsigned char*)"R",1);
315 for(i
=0; i
<nCol
; i
++){
316 switch( sqlite3_column_type(pStmt
,i
) ){
318 hash_step(&cx
, (const unsigned char*)"N",1);
321 case SQLITE_INTEGER
: {
325 sqlite3_int64 v
= sqlite3_column_int64(pStmt
,i
);
332 hash_step(&cx
, x
, 9);
339 double r
= sqlite3_column_double(pStmt
,i
);
350 int n2
= sqlite3_column_bytes(pStmt
, i
);
351 const unsigned char *z2
= sqlite3_column_text(pStmt
, i
);
352 hash_step_vformat(&cx
,"T%d:",n2
);
353 hash_step(&cx
, z2
, n2
);
357 int n2
= sqlite3_column_bytes(pStmt
, i
);
358 const unsigned char *z2
= sqlite3_column_blob(pStmt
, i
);
359 hash_step_vformat(&cx
,"B%d:",n2
);
360 hash_step(&cx
, z2
, n2
);
366 sqlite3_finalize(pStmt
);
368 hash_finish(&cx
, zOut
);
369 sqlite3_result_text(context
, zOut
, 40, SQLITE_TRANSIENT
);
374 __declspec(dllexport
)
376 int sqlite3_sha_init(
379 const sqlite3_api_routines
*pApi
382 SQLITE_EXTENSION_INIT2(pApi
);
383 (void)pzErrMsg
; /* Unused parameter */
384 rc
= sqlite3_create_function(db
, "sha1", 1,
385 SQLITE_UTF8
| SQLITE_INNOCUOUS
| SQLITE_DETERMINISTIC
,
388 rc
= sqlite3_create_function(db
, "sha1_query", 1,
389 SQLITE_UTF8
|SQLITE_DIRECTONLY
, 0,
390 sha1QueryFunc
, 0, 0);