0

I am trying to create a Hash from an integer using SQL. The purpose of this is to create a function in which I will know if the data received has not been tampered with by the person running the script. I figured I would MD5-hash the integer however I am running into some issues with the MD5 hash not being accurate.

Here is my test code where I try to convert 20 into the MD5

SELECT CONVERT(NVARCHAR(32),HashBytes('MD5', STR(20)),2) as 'MD5'

returns F63258CAB6D44ED830605298043CBEA4 and

SELECT CONVERT(NVARCHAR(32),HashBytes('MD5', '20'),2) as 'MD5'

returns the expected result of 98F13708210194C475687BE6106A3B84

What am I doing wrong with my conversion?

Also I do not have to use MD5. I am open to suggestions of using a different method in which it would be easy for me to decrypt but hard for the average user to reverse-engineer even if they saw the code.

1 Answer 1

3

When you check the result of STR(20) you will notice some leading spaces.

Try LTRIM(20) or cast(20 as varchar(12)) instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.