RANDOM_BYTES or CRYPT_GEN_RANDOM equivalent in MariaDB

Viewed 1648

Question

Is any alternatives of MySQL RANDOM_BYTES(len) or MSSQL CRYPT_GEN_RANDOM(len) available in MariaDB?

I read through their documentation, but I only found is rand(len), which is not cryptographically secure in generating random bytes.

Issues

Currently released version of MariaDB (10.3) does not support RANDOM_BYTES(len) according to this documentation https://mariadb.com/kb/en/library/function-differences-between-mariadb-103-and-mysql-57/

Limitation

If possible, keep the code within the MariaDB, and I don't want to rely on PHP or any other external functions for security reasons.

1 Answers

Unfortunately from talking to MariaDB support there is currently no RANDOM_BYTES equivalent in MariaDB. See https://jira.mariadb.org/browse/MDEV-27002 for a semi-related ticket which has to do with generating random passwords for users.

To get around this, what you can do is create a custom UDF (user-defined function) in C which utilizes a cryptographically secure library to generate a random series of bytes for whatever use case you have. This technically keeps the code 'within MariaDB' though you need to write your own function to call. See https://mariadb.com/kb/en/creating-user-defined-functions/ for more info.

Related