Home > Net >  Python MD5 Function to .Net
Python MD5 Function to .Net

Time:03-24

I am trying to convert a Python script to .net, this is the function that takes an input password and converts it into a password string that can be used to log in to an IP camera. This I believe works fine from my testing;

    md5 = hashlib.md5(bytes(password, "utf-8")).digest()
    chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    return "".join([chars[sum(x) % 62] for x in zip(md5[::2], md5[1::2])])

I'm afraid I don't really understand a huge amount about Python and the only other example I have is in Perl which I know zero about. I've got to here which is not very far - I don't really understand what the last line is actually doing - some sort of substring and then taking some sort of inverse value?

Private Function Calculate_Hash(Password As String) As String

    Dim DVR_Hash As MD5 = MD5.Create()

    Dim DVR_Hash_Bytes() As Byte = DVR_Hash.ComputeHash(Encoding.UTF8.GetBytes("password"))

    Log(BitConverter.ToString(DVR_Hash_Bytes).Replace("-", String.Empty))

    Return "" 'needs to be implemented

End Function

Is there anyone that can help me please?

Thanks

CodePudding user response:

Suppose you're quite good at vb.net and the problem is you don't know the python syntax.
So let me rewrite the python script in a non-pythonic way.

pythonic

res = "".join([chars[sum(x) % 62] for x in zip(md5[::2], md5[1::2])])

non-pythonic

res = ''
i = 0
while i < len(md5):
    sum_value = md5[i]   md5[i   1]
    res  = chars[sum_value % 62]
    i  = 2

vb.net

    Sub Main()
        Dim md5Hash As MD5 = MD5.Create()
        Dim md5Bytes() As Byte = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password))
        Dim chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray()
        Dim res As String
        Dim i = 0
        Dim sum As Integer
        While i < md5Bytes.Length
            sum = CInt(md5Bytes(i))   CInt(md5Bytes(i   1))
            res  = chars(sum Mod 62)
            i  = 2
        End While
        Console.WriteLine(res)
    End Sub
  • Related