Sqlite Hashing Functions Library

Looking for Sqlite Hashing Functions Library? Look No Further!

I have found a way to quite easily use the existing digest functions provided by OpenSSL. I have successfully been able to compile and run this on Solaris 10×86 and I think that as long as you have a current ssl implementation, this should work quite well. I have not thoroughly tested everything however, but have repeated the EVP_ function code pattern found here. This pattern is very simple and easy to use. I have also used a code pattern for a sample Sqlite3 Extension from here. As it is, the documentation is good enough to perform the simple task a few times and at a minimum for my implementation include these algorithms: md2, md5, sha, sha1, ripemd160.

Code Pattern for the Sqlite Extension

static void [function_name](
    sqlite3_context *context,
    int argc,
    sqlite3_value **argv
        unsigned char md_value[EVP_MAX_MD_SIZE];
        int md_len,i;
        if ( argc != 1 ){
                return ;
        EVP_MD_CTX ctx  ;
                char * input = 0 ;
                char * output = 0 ;
                input = ( char * ) sqlite3_value_text(argv[0]);
                EVP_DigestUpdate(&ctx, input, strlen(input));
                output = ( char * ) malloc ( md_len * 2 + 1 );
                if ( !output ){
                        return ;
                for (i = 0; i < md_len; i++){
                        sprintf (&output[i*2],"%02x", (unsigned char)md_value[i]);

Sqlite Init Function

int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
  sqlite3_create_function(db, "md2", 1, SQLITE_ANY, 0, md2, 0, 0);
  sqlite3_create_function(db, "md5", 1, SQLITE_ANY, 0, md5, 0, 0);
  sqlite3_create_function(db, "sha", 1, SQLITE_ANY, 0, sha, 0, 0);
  sqlite3_create_function(db, "sha1", 1, SQLITE_ANY, 0, sha1, 0, 0);
// ** Not Used as it didn't seem to be available in my OpenSSL
//  sqlite3_create_function(db, "mdc2", 1, SQLITE_ANY, 0, mdc2, 0, 0);
  sqlite3_create_function(db, "ripemd160", 1, SQLITE_ANY, 0, ripemd160, 0, 0);
  return 0;

Command Line Used for Compilation

The command line I used to compile was quite simple and just needed to include the -lssl .

gcc  -shared -fPIC -Isqlite3 -lssl  -o sqlite_digest.ext sqlite_digest.c

My set up includes sqlite and openssl in my include path so there may be issues if this is not your set up.

Loading the library in Sqlite

To load the library in Sqlite, you simply need to run the command line version as follows:

sqlite3 [optional database name]
sqlite>SELECT load_extension('./sqlite_digest.ext');
sqlite>SELECT md2('test');
sqlite>SELECT md5('test');
sqlite>SELECT sha('test');
sqlite>SELECT sha1('test');
sqlite>SELECT ripemd160('test');

Source Code

New Updated Link
Source Code to Sqlite Hashing Functions Library
SwhistleSoft Downloads


About ttessier

Professional Developer and Operator of SwhistleSoft
This entry was posted in C Programming and tagged , , , , , . Bookmark the permalink.

2 Responses to Sqlite Hashing Functions Library

  1. Rudi says:

    The code has a problem as it assumes that the data passed in are strings. It fails, if the hash is to be calculated on a blob or some other binary data. In order to fix this, use sqlite3_value_bytes(argv[0]) / sqlite3_value_blob(argv[0]) instead of strlen(input) / sqlite3_value_text(argv[0]).

    • admin says:

      I wouldn’t exactly concider that a problem as that is exactly how I intended to use this. Since the code is there for you to modify, I welcome you to make some tweaks. I do not have time currently to implement any changes but will most likely include your suggestion when I do.


Leave a Reply

Your email address will not be published. Required fields are marked *