|
Introduction
This is an extended stored procedure for Microsoft SQL Server 2000 that implements an optimized MD5 hash algorithm. It is intended to work much like the MySQL MD5() function. The algorithm was taken from here. I only added the md5_string() function. The DLL should work for older versions of SQL Server too, although I have not tested that. The source was compiled and tested on Microsoft Visual C++ 6.0 and .NET 2003.
Installation
- Extract or build the DLL file xp_md5.dll and place it in C:\Program Files\Microsoft SQL Server\MSSQL\Binn (or wherever appropriate). A precompiled DLL is in the Release directory of the source distribution.
- Create an Extended Stored Procedure called
xp_md5 in the "master" database. Right-click "Extended Stored Procedures" under the master database in the Server Manager and click "New Extended Stored Procedure...". Enter xp_md5 for the "Name" and for the "Path", enter the full path to xp_md5.dll.
Note: If you want to add it manually: USE master;
EXEC sp_addextendedproc 'xp_md5', 'xp_md5.dll'
- Create a user-defined function for each database in which you plan to use the MD5 procedure. Right-click "User Defined Functions" under the appropriate database(s) and click "New User Defined Function...". Enter the following:
CREATE FUNCTION [dbo].[fn_md5] (@data TEXT)
RETURNS CHAR(32) AS
BEGIN
DECLARE @hash CHAR(32)
EXEC master.dbo.xp_md5 @data, -1, @hash OUTPUT
RETURN @hash
END
- (Optional) Create other user-defined functions to let you specify the data length (for substrings,
BINARY and other fixed-width types). In this particular function, we take an IMAGE for input and an optional LENGTH. A negative LENGTH value causes the DLL to try to compute the length of the input automatically (this is the default): CREATE FUNCTION [dbo].[fn_md5x] (@data IMAGE, @len INT = -1)
RETURNS CHAR(32) AS
BEGIN
DECLARE @hash CHAR(32)
EXEC master.dbo.xp_md5 @data, @len, @hash OUTPUT
RETURN @hash
END
Usage:
FN_MD5
The User-Defined Functions can be used as follows:
SELECT dbo.fn_md5('Hello world!');
SELECT dbo.fn_md5x('Hello world!', 12);
Output for both statements: 86fb269d190d2c85f6e0468ceca42a20
XP_MD5: EXEC xp_md5 <@data> [@length = -1] [@hash OUTPUT]
To use the Extended Stored Procedure directly:
EXEC master.dbo.xp_md5 'Hello world!'
Output: 86fb269d190d2c85f6e0468ceca42a20
Or if you want the result saved to a variable instead: DECLARE @hash CHAR(32)
EXEC master.dbo.xp_md5 'Hello world!', -1, @hash OUTPUT
PRINT @hash
Output: 86fb269d190d2c85f6e0468ceca42a20
Examples
SELECT dbo.fn_md5(data) FROM table;
SELECT dbo.fn_md5(CAST(data AS VARCHAR(8000))) FROM table;
SELECT dbo.fn_md5x(data, DEFAULT) FROM table;
SELECT dbo.fn_md5x(data, LEN(data)) FROM table;
SELECT dbo.fn_md5x(CAST(data AS CHAR(4000)), LEN(data)) FROM table;
SELECT dbo.fn_md5x(data, 12) FROM table;
SELECT dbo.fn_md5x(CAST(data AS VARCHAR(8000)), DATALENGTH(data)) FROM table;
Miscellaneous
For purposes of speed, I did not include any real input data verification (e.g., type checking, data length checking, etc.). I opted to exclude that in order to maximize speed, as I originally wrote this for use in an application that inserts millions of rows at a time. I also know that I'm always calling the procedure properly. If you want to make it more robust - like if you do not know what kind of data will be passed to the function - then I highly recommend you add those safeguards.
One last thing, I added the linker option /OPT:NOWIN98 to minimize the binary size. This may cause a performance hit on non-NT systems (e.g., Win95, Win98, etc.). If you're using the DLL on such a system, I would recompile it without that linker option.
Cheers.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 84 (Total in Forum: 84) (Refresh) | FirstPrevNext |
|
|
 |
|
|
 |
|
|
hello
i hv a field having comma saparated int values n i want to find d count of particular value in d table
example :
jobtypes ------------ 1,2,3,4, 2,7,9, 1,4,6,7,
n i want to find count of 1
expected answer=2;
plz help
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You have posted the question in the wrong area...
but since you ask,try this:
SELECT COUNT(n) FROM d WHERE n like '%1,%'
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
SQL server supports an function for MD5 encryption, why not just use that 
HashBytes( <algorithm>, <input> ) algorithm = 'MD2' | 'MD4' | 'MD5' | 'SHA' | 'SHA1' input types = varchar | nvarchar | varbinary
0xA5BA0D8F38FB421F472CCE3C566467A7 = SELECT HashBytes('MD2','CodeProject.com') 0xBEC58CDEEB04C7B366E4FDD4F35D4603 = SELECT HashBytes('MD4','CodeProject.com') 0x1BA4C8D9E8F1B1DD8BC05379ED3292EB = SELECT HashBytes('MD5','CodeProject.com') 0x0EF9E6456730DBAD24BFE4FEC6DB85032EB0DBB0 = SELECT HashBytes('SHA','CodeProject.com') 0x0EF9E6456730DBAD24BFE4FEC6DB85032EB0DBB0 = SELECT HashBytes('SHA1','CodeProject.com')
Or
INSERT INTO Users(Username,Password) VALUES('Administrator', HashBytes('SHA1','P@55w0rd'))
Or
Look here at Microsoft http://msdn2.microsoft.com/en-us/library/ms174415.aspx[^]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Well, Convert your system to SQL 2005 or use the lighter version of hashing on the SQL 2000 server called pwdencrypt
I think i know what i linked to, so i wont use my time to read it again.
But the fact is you can encrypt on a SQL 2000 server, the function although, is hidden and called pwdencrypt and it has its weakness but google it and find some info on it http://www.google.com/search?q=pwdencrypt[^]
|
| Sign In·View Thread·PermaLink | 1.33/5 (2 votes) |
|
|
|
 |
|
|
How do I pass a SecureString var. to SQLS Stored Procedure?
Both from C#, and SQL Server side?
I'm assuming varbinary on the SS side, will handle this as Input parameter to stored procedure.
I'm just not sure of the allowable code to handle this.
F.Z. Atlanta
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
like some guy told earlier, there are some collision problems with md5 and it is recommended to use a salt for passwords -its no big deal for cheksums. is it possible to use salt with this code??
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hi Vic Mackey,
you call the srv_paraminfo two times
srv_paraminfo(pSrvProc, 1, &cType, &uMaxLen, &uLen, NULL, &fNull); pData = (BYTE*)malloc(uLen); srv_paraminfo(pSrvProc, 1, &cType, &uMaxLen, &uLen, pData, &fNull);  uDataLen = uLen;
it make no sence for me, because all the same param.
Regards Marc
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hmm,
sry it is my fault. I haven't seen the differnce. But it become clear. I have seen the pData for the buffer param.
So it is clear now...
Thanks Marc
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi,
I think in SQL2005, you have a function which gives you back a md5, so you didn't need this.
Regards Marc
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Hello,
I am a bit surprised that there is more then just one result after a md5 hash. RFC 1321 describes a 128 bit output after application of their algorithm. What is different in your code? The reference (link to the algorithm) is not available anymore.
So, I am sure that this is definitely an encription function but is it MD5?
Martin
|
| Sign In·View Thread·PermaLink | 3.50/5 (2 votes) |
|
|
|
 |
|
|
128 bits is 16 bytes, but those are in binary. This outputs that 16 bit binary string in hexadecimal, which gives 32 bytes.
Yes, this is MD5.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hello guys, i need ur help... I have one table with much rows, one row is, passwords...(pass of 800 users) and i like update my DB to MD5. The row passwords of 800 users to MD5 encription. Please need SQL script to convert the row passwords (with the data pass of 800 users) to MD5...
 Somebody help me?
Sorry my english and thanks all
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
select a , b , c , dbo.fn_md5(c) as EncPassword from table_a
or
update table_a set c = dbo.fn_md5(c)
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi,
I'm using the MD5 dll on SQL 2000 servers and it works perfect. However, recently I got the need to use it on a win2003 server and SQL 2005 server, both 64-bit versions. It does not work...
I did like before:
USE master; EXEC sp_addextendedproc 'xp_md5', 'xp_md5.dll'
no complaints here. But when I try this:
EXEC master.dbo.xp_md5 'Hello world!'
I get the following error:
Msg 17750, Level 16, State 0, Procedure xp_md5, Line 1 Could not load the DLL xp_md5.dll, or one of the DLLs it references. Reason: 193(%1 is not a valid Win32 application.).
Which confuses me.. Is this because some missing error messages for the x64 version? Or does it mean that I'm toast? I think I am toast and that I need a 64-bit version.
Any suggestions?
KP
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
that happened because you must compile with the 64 bits version of the library:
1: Srv.h. You must use the 32-bit version that is available in SQL Server 2000. 2: Opends60.dll. This file is available with SQL Server 2000 (64-bit). Opends60.dll is located in the \Mssql\Binn folder. 3: Opends60.lib. This file is available with SQL Server 2000 (64-bit). Opends60.lib is located at the root of the media.
san
|
| Sign In·View Thread·PermaLink | 2.00/5 (3 votes) |
|
|
|
 |
|
|
Hi,
I had the same problem in SQL 2005 x64, too, but didn't want to worry about recompiling xp_md5. I changed to using the built-in HashBytes function, like this:
SELECT Field1, SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', Field2)), 3, 32) AS HashedField2 FROM MyTable;
HashBytes returns a varbinary value, which I used the undocumented function fn_varbintohexstr to convert to a varchar, then the substring to remove the 0x to match the xp_md5 output. Hope this helps!
BS
|
| Sign In·View Thread·PermaLink | 5.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|