Private Const HashSalt As String = "##########"
Public Shared Function EncryptSHA(ByVal Value As String) As String
'CREATE BYTES OF VALUE/SALT
Dim valBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(Value)
Dim saltBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(HashSalt)
Dim ValAndSalt(valBytes.Length + saltBytes.Length) As Byte
Dim i As Integer = 0
Dim b As Byte
'ADD BYTES INTO NEW BYTES-VARIABLE
For Each b In valBytes
ValAndSalt(i) = valBytes(i)
i += 1
Next
For Each b In saltBytes
ValAndSalt(i) = saltBytes(i)
i += 1
Next
'CREATE HASH_OBJ
Dim Hash As New SHA1Managed()
'CREATE HASH
Dim hashByte() As Byte = Hash.ComputeHash(ValAndSalt)
'GET HASH_STRING
Dim hashString As String = System.Convert.ToBase64String(hashByte)
'RETURN
Return hashString
End Function
Public Shared Function ComputeSHA1Hash(ByVal Value As String, ByVal HashedString As String) As Boolean
'HASH THE VALUE AND COMPRE WITH CURRENT HASH
Return EncryptSHA(Value) = HashedString
End Function
Har skrivit denna kod för att kryptera lösenord. Är den "så säker som möjligt", eller ska man göra något ytterligare?
Såg tex. på:
http://www.obviex.com/samples/hash.aspx
att de gör lite extra saker...