2

I have created hash value using below function in sql

SQL query

Select hashbytes('MD5', PNumber+CONVERT(VARCHAR(50),cast(datestamp as  binary),1))   
From dbo.Events

Now I need to get the equivalent C# function in order to get the hash value and pass it to a stored procedure.

I am using below code to get the c# equivalent. But values are not matching

C# Code

var strDate = policyEventFromQueue.DateStamp.ToString();
var binaryvalue = Encoding.Unicode.GetBytes(strDate);
var hashkey = GetMD5Hash(PNumber + binaryvalue);

public static byte[] GetMD5Hash(string input)
{
    System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] bs = System.Text.Encoding.Unicode.GetBytes(input);

    bs = x.ComputeHash(bs);
    return bs;
}

Here are test results:

From SQL Server:

PNumber ='4272535529'
DateStamp ='2016-06-30 12:19:35.257961'

HashValue : 0x104E09499B76CB59420AEEEDBBE187F8

In C# I am getting the value from DB Field value as below

[0]: 16
[1]: 78
[2]: 9
[3]: 73
[4]: 155
[5]: 118
[6]: 203
[7]: 89
[8]: 66
[9]: 10
[10]: 238
[11]: 237
[12]: 187
[13]: 225
[14]: 135
[15]: 248

From C# GetMD5Hash Function i am getting value as below

[0]: 30
[1]: 153
[2]: 105
[3]: 203
[4]: 34
[5]: 124
[6]: 20
[7]: 12
[8]: 207
[9]: 113
[10]: 210
[11]: 144
[12]: 18
[13]: 145
[14]: 22
[15]: 36

Any suggestion will be appreciated.

7
  • 2
    Start by getting the same date value before it is being hashed. Commented Aug 3, 2016 at 14:40
  • 2
    Are you sure the hashing function isn't working the same, or is the string you're hashing different? What exactly does PNumber+CONVERT(VARCHAR(50),cast(datestamp as binary),1) look like in SQL compared to PNumber + Encoding.Unicode.GetBytes(strDate)? Commented Aug 3, 2016 at 14:40
  • What is the type of datestamp? Is it a string? Commented Aug 3, 2016 at 14:50
  • Use BitConverter.ToString(bs); then replace all "-" with empty string. Commented Aug 3, 2016 at 14:54
  • i have edited my question with test result Commented Aug 3, 2016 at 15:02

2 Answers 2

2

In c# it looks like the format for a DateTime ToString() is more like this:

DateTime.Now.ToString()
//--------
"8/3/2016 4:11:14 PM"

I think you're hashing two different strings. For the hashes to match you'll need to format the dates the same before computing the hash value.

Also it looks like on sql server you're hashing something like:

select '12313135' + CONVERT(VARCHAR(50),cast(sysdatetime() as  binary),1)
//------------
123131350x077B7127E688B23B0B000000000000000000000000000000

Another issue could be this:

var binaryvalue = Encoding.Unicode.GetBytes(DateTime.Now.ToString());
var hashkey = "123456" + binaryvalue;
Console.WriteLine(hashkey)
//----------
123456System.Byte[]

your conversions between strings and bytes is off.

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

1 Comment

I found the issue . But i don't know the solution. Problem is with the sql datetime column. It is taking different format. I have posted a separate question for this. Please have a look at that. stackoverflow.com/questions/38761149/…
0

.Net 8 - 2024

There was not much recent data out there when I was looking for an answer and none of them were even close. After a few tweaks to a co-pilot query, here is what I found that works like a champ.

public static byte[] HashPassword5(string userName, string password)
{
  var bytes = Encoding.Unicode.GetBytes($"{userName}{Your-Salt}{password}");

  return SHA256.HashData(bytes);
}

Of course your bytes value may be constructed differently. But in any case, replace Your-Salt with your actual salt string.

What the method returns are the raw bytes, something like the following:

ABzmoBS3yRiwzkDGjUEesAPgQHlw5eJxMd8cM6gfpf0=

To see the actual result, you need to do this:

var rawBytes = Utilities.HashPassword("someUserName", "somePassword");
Convert.ToHexString(rawBytes );

Also, the result from SQL Server is:

0x001CE6A014B7C918B0CE40C68D411EB003E0407970E5E27131DF1C33A81FA5FD

The C# result is:

001CE6A014B7C918B0CE40C68D411EB003E0407970E5E27131DF1C33A81FA5FD.

Note that he SQL result has the 0x prefix as the only difference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.