Click here to Skip to main content
15,913,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using hash system for storing password.
The problem is peculiar because it only happens if the first alphabet of the userid (used for authentication) is small 'n'.

2 numbers gets changed while saving in sql. (no. (2) and (3))

Below are the related hash sequences i recovered while debugging:
the case used is user: neetu and password: 123456

VB
Public Function fnHashedValue(ByVal value, ByVal authentication)
       'To Convert string into hash for storing and comparision
       Dim sSourceData As String = value + authentication

       'Create a byte array from source data.
       Dim tmpSource() As Byte = ASCIIEncoding.ASCII.GetBytes(sSourceData)

       'Compute hash based on source data.
       Dim tmpHash() As Byte = New SHA1CryptoServiceProvider().ComputeHash(tmpSource)

       Return tmpHash

   End Function


VB
tmpNewHash      ? tmpSavedHash
{Length=20}     {Length=20}
    (0): 54         (0): 54
    (1): 236            (1): 236
    (2): 128            (2): 253
    (3): 223            (3): 255
    (4): 250            (4): 250
    (5): 49         (5): 49
    (6): 33         (6): 33
    (7): 163            (7): 163
    (8): 231            (8): 231
    (9): 207            (9): 207
    (10): 11            (10): 11
    (11): 187           (11): 187
    (12): 221           (12): 221
    (13): 23            (13): 23
    (14): 28            (14): 28
    (15): 193           (15): 193
    (16): 87            (16): 87
    (17): 10            (17): 10
    (18): 103           (18): 103
    (19): 1         (19): 1
Posted

To start with, never use SHA-1 or MD5 for security purposes; both algorithms have been found broken. Use some algorithm from the SHA-2 family (even better SHA-3, as far as I know, is not yet introduced on .NET FCL).

Please see: http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm%28v=vs.110%29.aspx[^].

You need the class SHA256, SHA384 or SHA512.

See also:
http://en.wikipedia.org/wiki/SHA-1[^],
http://en.wikipedia.org/wiki/Sha-2[^],
http://en.wikipedia.org/wiki/Sha-3[^].

I used SHA-2 extensively and never had any problems with it. Please try.

Now, better never use ASCII on any input strings. .NET is based on Unicode which is not covered by ASCII. The user can always enter Unicode password with characters beyond the ASCII domain. Better use UTF-8.

—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 4-Sep-14 2:01am    
Sorry, I don't understand...
—SA
atul sharma 5126 4-Sep-14 9:26am    
thanks for reply!
I have changed the function to use unicode. and also pasted here is the code i am using for checking the password entered. please check.. as same problem again occuring with every user..

Public Function fnHashedValue(ByVal value, ByVal authentication)
'To Convert string into hash for storing and comparision
Dim sSourceData As String = value + authentication

'Create a byte array from source data.
'Dim tmpSource() As Byte = ASCIIEncoding.ASCII.GetBytes(sSourceData)
Dim tmpSource() As Byte = UnicodeEncoding.Unicode.GetBytes(sSourceData)

'Compute hash based on source data.
Dim tmpHash() As Byte = New SHA1CryptoServiceProvider().ComputeHash(tmpSource)
Return tmpHash

End Function

'To Convert password recoved as string from database to byte for comparision
Dim sha1 As Encoding = Encoding.Unicode
Dim tmpSavedHash() As Byte = sha1.GetBytes(DT.Rows(0).Item(0))

'To convert entered password into hash for comparision
Dim tmpNewHash() As Byte = fnHashedValue(txtPassword.Text, txtUser.Text)
Dim bEqual As Boolean = False
If tmpNewHash.Length = tmpSavedHash.Length Then
Dim i As Integer
Do While (i < tmpNewHash.Length) AndAlso (tmpNewHash(i) = tmpSavedHash(i))
i += 1
Loop
If i = tmpNewHash.Length Then
bEqual = True
End If
End If
atul sharma 5126 4-Sep-14 9:55am    
Hey SA,

I have modified the code as you suggested with UTF8 and SHA512.

Pls confirm if the following is the right code of conversion of saved password to byte:

Dim tmpSavedHash() As Byte = UTF8Encoding.UTF8.GetBytes(DT.Rows(0).Item(0))
Sergey Alexandrovich Kryukov 4-Sep-14 10:16am    
Assuming that the argument of GetBytes is correct string, this is correct.
Did you managed to get identical hash from identical data? Is your problem solved?
—SA
atul sharma 5126 4-Sep-14 10:58am    
I did manage to get identical hash from identical data.
But the problem is not solved because the byte converted from the hashed password taken from database is different then the one created by the function.

Resolved!

The issue was related to conversion to string. I used the following function for conversion now.

VB
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
       Dim i As Integer
       Dim sOutput As New StringBuilder(arrInput.Length)
       For i = 0 To arrInput.Length - 1
           sOutput.Append(arrInput(i).ToString("X2"))
       Next
       Return sOutput.ToString()
   End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900