views:

69

answers:

3

Working with several DBs in parallel and need to initialize some records with hashed passwords. In MS SQL server there are handy functions that allow to hash on the fly:


HashBytes('SHA1', CONVERT(nvarchar(32), N'admin'))

Is there is a similar function with SQLite?

If not, which is the easiest workaround (such as select from SQL server and somehow insert it into SQLite tables)?

The preferred hashing algorithm is SHA1 and the passwords are stored in a BLOB column.

Update: I use C# language in the current project.

+2  A: 

There is no such function built into SQLite3.

But you could define a user function e.g. with sqlite3_create_function if you're using the C interface, and implement SHA-1 with that. (But if you're having a programmable interface perhaps you could just SHA-1 the password outside of the SQL engine.)

You could also try to find / create an extension and load with the load_extension function, but I don't have experience on that.

Edit:

KennyTM
+1  A: 

SQLite does not come with SHA1, but it is relatively easily to add. You didn't say what language, you're using, but you can look at the C documentation for create_function and sqlite3_result. You can also take a look at this example of how to add SHA1 to SQLite using Ruby.

With System.Data.SQLite, they're called user-defined functions. You can look at this example on the main site.

Matthew Flaschen
I use C#/.NET on Win platform (the question body is updated)
BreakPhreak
A: 

As far as I know, SQLite doesn't have any hashing functions built-in.

There is a way to add custom functions to SQLite, but it's probably easier if you just calculate the SHA1 hash in your program and store it in SQlite.

Creating custom functions for SQLite depends somewhat on the API and the language you're using. I only have experience with creating SQLite functions from Python.

pableu