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 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];
43 #if __GNUC__ && (defined(__i386__) || defined(__x86_64__))
45 * GCC by itself only generates left rotates. Use right rotates if
46 * possible to be kinder to dinky implementations with iterative rotate
49 #define SHA_ROT(op, x, k) \
50 ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; })
51 #define rol(x,k) SHA_ROT("roll", x, k)
52 #define ror(x,k) SHA_ROT("rorl", x, k)
55 /* Generic C equivalent */
56 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
57 #define rol(x,k) SHA_ROT(x,k,32-(k))
58 #define ror(x,k) SHA_ROT(x,32-(k),k)
62 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
63 |(rol(block[i],8)&0x00FF00FF))
64 #define blk0be(i) block[i]
65 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
66 ^block[(i+2)&15]^block[i&15],1))
69 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
71 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
72 * determined at run-time.
74 #define Rl0(v,w,x,y,z,i) \
75 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
76 #define Rb0(v,w,x,y,z,i) \
77 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
78 #define R1(v,w,x,y,z,i) \
79 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
80 #define R2(v,w,x,y,z,i) \
81 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
82 #define R3(v,w,x,y,z,i) \
83 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
84 #define R4(v,w,x,y,z,i) \
85 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
88 * Hash a single 512-bit block. This is the core of the algorithm.
90 void SHA1Transform(unsigned int state
[5], const unsigned char buffer
[64]){
91 unsigned int qq
[5]; /* a, b, c, d, e; */
93 unsigned int block
[16];
94 memcpy(block
, buffer
, 64);
95 memcpy(qq
,state
,5*sizeof(unsigned int));
103 /* Copy p->state[] to working vars */
112 /* 4 rounds of 20 operations each. Loop unrolled. */
113 if( 1 == *(unsigned char*)&one
){
114 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);
115 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);
116 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);
117 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);
119 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);
120 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);
121 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);
122 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);
124 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);
125 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);
126 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);
127 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);
128 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);
129 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);
130 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);
131 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);
132 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);
133 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);
134 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);
135 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);
136 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);
137 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);
138 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);
139 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);
141 /* Add the working vars back into context.state[] */
156 /* Initialize a SHA1 context */
157 static void hash_init(SHA1Context
*p
){
158 /* SHA1 initialization constants */
159 p
->state
[0] = 0x67452301;
160 p
->state
[1] = 0xEFCDAB89;
161 p
->state
[2] = 0x98BADCFE;
162 p
->state
[3] = 0x10325476;
163 p
->state
[4] = 0xC3D2E1F0;
164 p
->count
[0] = p
->count
[1] = 0;
167 /* Add new content to the SHA1 hash */
168 static void hash_step(
169 SHA1Context
*p
, /* Add content to this context */
170 const unsigned char *data
, /* Data to be added */
171 unsigned int len
/* Number of bytes in data */
176 if( (p
->count
[0] += len
<< 3) < j
){
177 p
->count
[1] += (len
>>29)+1;
180 if( (j
+ len
) > 63 ){
181 (void)memcpy(&p
->buffer
[j
], data
, (i
= 64-j
));
182 SHA1Transform(p
->state
, p
->buffer
);
183 for(; i
+ 63 < len
; i
+= 64){
184 SHA1Transform(p
->state
, &data
[i
]);
190 (void)memcpy(&p
->buffer
[j
], &data
[i
], len
- i
);
193 /* Compute a string using sqlite3_vsnprintf() and hash it */
194 static void hash_step_vformat(
195 SHA1Context
*p
, /* Add content to this context */
202 va_start(ap
, zFormat
);
203 sqlite3_vsnprintf(sizeof(zBuf
),zBuf
,zFormat
,ap
);
205 n
= (int)strlen(zBuf
);
206 hash_step(p
, (unsigned char*)zBuf
, n
);
210 /* Add padding and compute the message digest. Render the
211 ** message digest as lower-case hexadecimal and put it into
212 ** zOut[]. zOut[] must be at least 41 bytes long. */
213 static void hash_finish(
214 SHA1Context
*p
, /* The SHA1 context to finish and render */
215 char *zOut
/* Store hexadecimal hash here */
218 unsigned char finalcount
[8];
219 unsigned char digest
[20];
220 static const char zEncode
[] = "0123456789abcdef";
222 for (i
= 0; i
< 8; i
++){
223 finalcount
[i
] = (unsigned char)((p
->count
[(i
>= 4 ? 0 : 1)]
224 >> ((3-(i
& 3)) * 8) ) & 255); /* Endian independent */
226 hash_step(p
, (const unsigned char *)"\200", 1);
227 while ((p
->count
[0] & 504) != 448){
228 hash_step(p
, (const unsigned char *)"\0", 1);
230 hash_step(p
, finalcount
, 8); /* Should cause a SHA1Transform() */
231 for (i
= 0; i
< 20; i
++){
232 digest
[i
] = (unsigned char)((p
->state
[i
>>2] >> ((3-(i
& 3)) * 8) ) & 255);
235 zOut
[i
*2] = zEncode
[(digest
[i
]>>4)&0xf];
236 zOut
[i
*2+1] = zEncode
[digest
[i
] & 0xf];
240 /* End of the hashing logic
241 *****************************************************************************/
244 ** Implementation of the sha1(X) function.
246 ** Return a lower-case hexadecimal rendering of the SHA1 hash of the
247 ** argument X. If X is a BLOB, it is hashed as is. For all other
248 ** types of input, X is converted into a UTF-8 string and the string
249 ** is hash without the trailing 0x00 terminator. The hash of a NULL
252 static void sha1Func(
253 sqlite3_context
*context
,
258 int eType
= sqlite3_value_type(argv
[0]);
259 int nByte
= sqlite3_value_bytes(argv
[0]);
263 if( eType
==SQLITE_NULL
) return;
265 if( eType
==SQLITE_BLOB
){
266 hash_step(&cx
, sqlite3_value_blob(argv
[0]), nByte
);
268 hash_step(&cx
, sqlite3_value_text(argv
[0]), nByte
);
270 hash_finish(&cx
, zOut
);
271 sqlite3_result_text(context
, zOut
, 40, SQLITE_TRANSIENT
);
275 ** Implementation of the sha1_query(SQL) function.
277 ** This function compiles and runs the SQL statement(s) given in the
278 ** argument. The results are hashed using SHA1 and that hash is returned.
280 ** The original SQL text is included as part of the hash.
282 ** The hash is not just a concatenation of the outputs. Each query
283 ** is delimited and each row and value within the query is delimited,
284 ** with all values being marked with their datatypes.
286 static void sha1QueryFunc(
287 sqlite3_context
*context
,
291 sqlite3
*db
= sqlite3_context_db_handle(context
);
292 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
293 sqlite3_stmt
*pStmt
= 0;
294 int nCol
; /* Number of columns in the result set */
295 int i
; /* Loop counter */
303 if( zSql
==0 ) return;
306 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zSql
);
308 char *zMsg
= sqlite3_mprintf("error SQL statement [%s]: %s",
309 zSql
, sqlite3_errmsg(db
));
310 sqlite3_finalize(pStmt
);
311 sqlite3_result_error(context
, zMsg
, -1);
315 if( !sqlite3_stmt_readonly(pStmt
) ){
316 char *zMsg
= sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt
));
317 sqlite3_finalize(pStmt
);
318 sqlite3_result_error(context
, zMsg
, -1);
322 nCol
= sqlite3_column_count(pStmt
);
323 z
= sqlite3_sql(pStmt
);
325 hash_step_vformat(&cx
,"S%d:",n
);
326 hash_step(&cx
,(unsigned char*)z
,n
);
328 /* Compute a hash over the result of the query */
329 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
330 hash_step(&cx
,(const unsigned char*)"R",1);
331 for(i
=0; i
<nCol
; i
++){
332 switch( sqlite3_column_type(pStmt
,i
) ){
334 hash_step(&cx
, (const unsigned char*)"N",1);
337 case SQLITE_INTEGER
: {
341 sqlite3_int64 v
= sqlite3_column_int64(pStmt
,i
);
348 hash_step(&cx
, x
, 9);
355 double r
= sqlite3_column_double(pStmt
,i
);
366 int n2
= sqlite3_column_bytes(pStmt
, i
);
367 const unsigned char *z2
= sqlite3_column_text(pStmt
, i
);
368 hash_step_vformat(&cx
,"T%d:",n2
);
369 hash_step(&cx
, z2
, n2
);
373 int n2
= sqlite3_column_bytes(pStmt
, i
);
374 const unsigned char *z2
= sqlite3_column_blob(pStmt
, i
);
375 hash_step_vformat(&cx
,"B%d:",n2
);
376 hash_step(&cx
, z2
, n2
);
382 sqlite3_finalize(pStmt
);
384 hash_finish(&cx
, zOut
);
385 sqlite3_result_text(context
, zOut
, 40, SQLITE_TRANSIENT
);
390 __declspec(dllexport
)
392 int sqlite3_sha_init(
395 const sqlite3_api_routines
*pApi
398 SQLITE_EXTENSION_INIT2(pApi
);
399 (void)pzErrMsg
; /* Unused parameter */
400 rc
= sqlite3_create_function(db
, "sha1", 1, SQLITE_UTF8
, 0,
403 rc
= sqlite3_create_function(db
, "sha1_query", 1, SQLITE_UTF8
, 0,
404 sha1QueryFunc
, 0, 0);