Ask the Expert

Utilizing a hash function algorithm to help secure data

Could you provide an example of a one-way hash function of the Dynamic SHA-2 algorithm?

    Requires Free Membership to View

SHA stands for Secure Hash Algorithm, and SHA-2 is a set of cryptographic hash functions with different digest sizes (SHA-224, SHA-256, SHA-384, and SHA-512) designed by the National Security Agency. SHA-2 was published in 2001 by NIST as part of the U.S. Federal Information Processing Standards (FIPS). A hash function algorithm is used as a unique value of fixed size to represent a piece of data such as a password or Word document file. Any change to the data, no matter how small, results in a large unpredictable change in the hash. Hashes of two sets of data should match if and only if the corresponding data also matches.

The following is an example, written in Visual Basic, of how to compute the SHA-256 hash for the data string "myPassword" and display it in a label called Label1:

Imports System.Text
Imports System.Security.Cryptography

Public Class Form1

Private Function ComputeSHA256Hash(ByVal Password As String) As String
 ' Convert Password into a byte array.
Dim passwordBytes As Byte()
passwordBytes = Encoding.UTF8.GetBytes(Password)

' Initialize the SHA256 hashing algorithm class
Dim shaM As New SHA256Managed()

' Compute hash value of the password.
Dim hashBytes As Byte()
hashBytes = shaM.ComputeHash(passwordBytes)

' Convert hash into a base64-encoded string.
ComputeSHA256Hash = Convert.ToBase64String(hashBytes)

 End Function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = ComputeSHA256Hash("myPassword")
 End Sub
End Class

The ComputeSHA256Hash function uses the .NET Framework Class Library System.Security.Cryptography class. It takes the string Password and turns it into a byte-array. It then runs those bytes through the SHA256Managed computation function provided by the class and returns a 44-bit string of the hash that's created. It finally displays via Labe1.Text. The hash for myPassword using this program is dlSbgn7EbnBf0DgxgT+lIXIzjw38vXEe1EuBqW2sUcY=. If you change the input to mzPassword, so just one character different, the hash changes to something completely different - XkdNLN2VuBgcMjhI/Dg0ioj4Eds6FLDF3lRlBxt+I4U=.

For security reasons, if you are using hash values to store passwords, you may well want to use a salt, as this makes a hashed-password less immune to dictionary attacks. Not only would the hacker have to develop a hash for every commonly known password, but also for every commonly known password multiplied by the nearly infinite number of possible salts.

This was first published in July 2010

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: