1 Replies - 14864 Views - Last Post: 10 March 2012 - 04:51 PM

#1 melicaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-October 08

How to SHA1 Hashing in a String [VB.NET]

Posted 10 March 2012 - 11:03 AM

I would like to hash a string using SHA1. My code is as below:

Dim result As String

Dim sha1 As New HMACSHA1
Dim hashValue As Byte() = sha1.ComputeHash(Encoding.UTF8.GetBytes(signature))

result = GetBytesToHexadeciString(algohashValue)

Everything I re-load the application, it will generate a new hash code. How to overcome this? The data I put in the "signature" is constant, like my name.

Thanks in advances...

Is This A Good Question/Topic? 0
  • +

Replies To: How to SHA1 Hashing in a String [VB.NET]

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4925
  • View blog
  • Posts: 13,317
  • Joined: 18-April 07

Re: How to SHA1 Hashing in a String [VB.NET]

Posted 10 March 2012 - 04:51 PM

This hash algorithm uses a secret key for hashing. If you don't specify one (using the key property) it uses its own. You can then save that key and use it to set the key property again before comparing hashes.

For example....

Dim sha1 As New HMACSHA1
sha1.Key = Encoding.UTF8.GetBytes("my_secret_key")
Dim hashValue As Byte() = sha1.ComputeHash(Encoding.UTF8.GetBytes("my text"))

Here I am using the key "my_secret_key" to generate my hash. Later, when you restart the app and want to compare the hashes, you create another sha1 object, set the key back to "my_secret_key", hash the same text and compare the two.

If you want the program to come up with the key, do the hash like you did, but remember to ask for the key and save it. Then when you want to confirm, you have the key to set the Key property to and can recompute the hash.

In other words, the Key property is your "key" to the solution. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1