MD5-encryption - "translate" VB to SQL

  • Following VB-code transforms string "123456" to "e10adc3949ba59abbe56e057f20f883e". Does anyone have any idea of how to do the same in TSQL??

    --------------------

    Dim txt As String = "123456"

    Dim md5serv As MD5 = MD5CryptoServiceProvider.Create()

    Dim strbld As New StringBuilder()

    Dim ascenc As New ASCIIEncoding()

    ' convert string into array of bytes

    Dim buffer As Byte() = ascenc.GetBytes(txt)

    ' Compute the hash for the string

    Dim hash As Byte() = md5serv.ComputeHash(buffer)

    For Each b As Byte In hash

    strbld.Append(b.ToString("x2"))

    Next

    MsgBox(strbld.ToString()) 'Result = e10adc3949ba59abbe56e057f20f883e

  • declare @val varbinary(max)

    SELECT @val = HashBytes('MD5','123456')

    print @val

    --0xE10ADC3949BA59ABBE56E057F20F883E

    SELECT

    CASE

    WHEN @val = HashBytes('MD5','123456')

    THEN 'True'

    ELSE 'FALSE'

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply