Click here to Skip to main content
15,902,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to convert the password into hash value to further save and then match it for login. But while coding itself the above error is showing. This is my first time at hashing. Thanks in advance.
error is at line:         Dim tmpSource As Byte = ASCIIEncoding.ASCII.GetBytes(sSourceData)

VB
Imports System
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Module Hash

    Public Function fnEncrypedValue(ByVal value)

        Dim sSourceData As String = value
        '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)

        tmpHash = ByteArrayToString(tmpHash)

        Return tmpHash

    End Function


    'Console.WriteLine(ByteArrayToString(tmpHash))

    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

End Module
Posted
Updated 6-Mar-14 23:46pm
v2

1 solution

Simply:
VB
Public Function fnEncrypedValue(ByVal value)

    Dim sSourceData As String = value
    '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)

    tmpHash = ByteArrayToString(tmpHash)

    Return tmpHash

End Function


GetBytes() and ComputeHash() methods return a byte array; you have to take that in account in your variables declarations.
 
Share this answer
 
v2
Comments
CPallini 7-Mar-14 6:06am    
5.
phil.o 7-Mar-14 6:16am    
Thanks :)
atul sharma 5126 7-Mar-14 11:34am    
Thanks Phil. It removed the message. But similar message is now showing in following line:
tmpHash = ByteArrayToString(tmpHash)

I tried similar solution in the second function but did not work.
phil.o 7-Mar-14 14:39pm    
tmpHash variable is declared as an array of bytes. The line you show tries to assign a string to it. That cannot work.
Create a string variable, and affect the result of the ByteArrayToString() method to it.
You should also specify the return type of fnEncryptedValue() method (string, actually).
(I do not update the solution because I feel here that you need to put some thinking of your own into this issue; that's an important matter, and you should not pass through it without clearly understanding what is going on. Practicing is the best way)

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