Jump to content

[VB.NET 2005] MD5 HASH


Weasel

Recommended Posts

Boas, eu estou aqui a converter umas aplicações que eu tinha feito em php para VB.NET...

Agora deparei-me com um problema, no PHP temos o comando md5() que dá-nos a hash md5 do texto que for dado, por exemplo md5(teste); dá-nos a Hash da palavra teste, mas em VB.NET como posso criar uma hash em md5 ?

Já procurei pelo google mas as várias "functions" apresentadas dá um valor totalmente diferente da MD5 criada pelo php, não é suposto uma Hash MD5 ser igual seja em que plataforma seja criada ?

Knowledge to the masses


Link to comment
Share on other sites

Bem, resolvi usando a função dada pelo ht

aqui: http://www.portugal-a-programar.pt/forums/topic/0-find-topic/?do=findComment&comment=99080

Public Function MD5_Hash(ByVal SourceText As String) As String
        Try
            Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim rawBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(SourceText)
            Dim myHash As Byte() = md5.ComputeHash(rawBytes)
            Dim Capacity As Integer = (myHash.Length * 2 + (myHash.Length / 8))
            Dim result As New System.Text.StringBuilder(Capacity)
            Dim i As Integer
            For i = 0 To myHash.Length - 1
                result.Append(BitConverter.ToString(myHash, i, 1))
            Next i
            Return result.ToString().TrimEnd(New Char() {" "c}).ToLower
        Catch ex As Exception
            Return "0"
        End Try
    End Function

Mas não a percebi muito bem :dontgetit:

Se alguém a quiser explicar..  🙂

Knowledge to the masses


Link to comment
Share on other sites

Dim resultado as Byte() = MD5.Create().ComputeHash(Encoding.Default.GetBytes("tuaString"))

Acho que desta maneira mais simples.

Basicamente pegas numa String ("tuaString") e convertes para bytes. Calculas a Hash (por MD5) e obtens os bytes.

Agora com esses bytes podes fazer o que quizeres. Se quizeres converter para String podes fazer com algo do genero.

Dim sb As New StringBuilder()
Dim i As Integer
For i = 0 To resultado.Length - 1
     sb.Append(resultado(i).ToString("x2"))
Next i

Dim stringFinal as String = sb.ToString()

"Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !"

Sign on a computer system consultant's desk

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.