Solved

Function to encode MD5 in SQL-Server 2012

Posted on 2014-10-27
3
602 Views
Last Modified: 2014-10-27
Hi.

I have created this function to get the MD5 code of a text:

FUNCTION [dbo].[fn_md5] (@data_a_codificar varchar) 
RETURNS CHAR(32) AS
BEGIN
  RETURN convert(varchar,hashbytes('MD5',@data_a_codificar),2)
END

Open in new window


Then I execute these two sentences:

select dbo.fn_md5('Hello');

select convert(char,hashbytes('MD5','Hello'),2)

Open in new window


Why aren't they returning the same values?
0
Comment
Question by:gplana
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
johny_bravo1 earned 500 total points
ID: 40406163
Change your function

ALTER FUNCTION [dbo].[fn_md5] (@data_a_codificar varchar(32))
RETURNS CHAR(32) AS
BEGIN
  RETURN convert(varchar(32),hashbytes('MD5',@data_a_codificar),2)
END

             
select dbo.fn_md5('Hello');

select convert(varchar(32),hashbytes('MD5','Hello'),2)
0
 
LVL 15

Author Comment

by:gplana
ID: 40406178
Thanks, but still different values. Here is the result of the two selects:

1A8E9B7EA00C8125B37DC3F231564C72
8B1A9953C4611296A827ABF8C47804D7
0
 
LVL 15

Author Closing Comment

by:gplana
ID: 40406181
I have mispelled your solution. It works fine.

Thank you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now