[Python] MD5-based password algorithm in Python

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    I try to call API that specifically uses MD5 hash in one of the steps. In the documentation they specifically show example reference that generates MD5 in the following way

    $ openssl passwd -1 -salt stack overflow
    $1$stack$MVcBmQ3RlrBu5Xoj74NBA0


    or to be more exact, they just use the part after the third $

    $ openssl passwd -1 -salt stack overflow | cut -f 4 -d '$'
    MVcBmQ3RlrBu5Xoj74NBA0


    At first, I tried to use hashlib and got the hexadecimal output that does not resemble the exampla at all.

    salt = b'stack'
    input = b'overflow'
    output = hashlib.md5(salt + input).hexdigest()
    print(output)

    73868cb1848a216984dca1b6b0ee37bc


    I figured that I just need to decode those hex values to characters, but decode does not work for default utf8 or for latin1

    salt = b'stack'
    input = b'overflow'
    output = hashlib.md5(salt + input).digest().decode()
    print(output)

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x86 in position 1: invalid start byte


    I found some help here python version of openssl passwd and here MD5 hash in Python

    I could reproduce this one with crypt

    $ openssl passwd -salt stack overflow
    st22n6QiCXNQY


    salt = 'stack'
    input = 'overflow'
    output = crypt.crypt(input, salt)
    print(output)

    st22n6QiCXNQY


    But as soon openssl passwd -1 is added, which stands for

    -1 MD5-based password algorithm


    I cannot reproduce it anymore.

    How can I recreate MD5-based password algorithm in Python? I would preferably use hashlib if possible.

    Continue reading...

Compartilhe esta Página