Solved

Function to encode MD5 in SQL-Server 2012

Posted on 2014-10-27
3
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

695 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