Solved

Function to encode MD5 in SQL-Server 2012

Posted on 2014-10-27
3
584 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

746 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

15 Experts available now in Live!

Get 1:1 Help Now