INSERT encrypted string into MariaDB

I got this MariaDB string that works, it give a nice password hash. But I want python to do the job. So how to translate this MariaDB command to Python?
ENCRYPT(‘MyGoodPasswd#01’, CONCAT(‘ 6 ’, SUBSTRING(SHA(RAND()), -16)))

Thx in advance,
Nico

It depends on which SHA hash you want to use.

SHA256?

from hashlib import sha256

passphrase = input('Please enter your passphrase: ')
hashed_passphrase = sha256(passphrase.encode('utf-8')).hexdigest()

print(hashed_passphrase)

And the OP wanted some “random” data. I’ve got this sitting around:

 from random import randint

 def make_randblock(size):
     ''' Generate a pseudorandom chunk of bytes of the specified size.
     '''
     return bytes(randint(0, 255) for _ in range(size))

which I use for making some randomish gibberish for some tests.

Since this is for cryptography, it would be much better to use secrets.token_bytes instead. It’s also easier.

(For non-cryptographic situations where you need a bunch of random bytes, random.rand_bytes() is more convenient, though it’s new in Python 3.9 so it might not be available for everyone. But this is crypto, so use the secrets module which has been here since 3.6.)

1 Like

The meaning of this all is a password storage for Dovecot.
Dovecot explain password storage

Then you definitely want the secrets module for the random stuff, not
my little function whose purpose is just to generate “fuzz” type data.