2


I need to replace some plain text passwords with SHA256 Base64 passwords. Apparentely the hash generated by the database functions does not match with the ones used by the application. For example: my application uses this function for generating the hash:

$ echo -n "admin" | openssl dgst -sha256 -binary | openssl base64
jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=

Now, the same password with the database:

select TO_BASE64(SHA2('admin',256));
OGM2OTc2ZTViNTQxMDQxNWJkZTkwOGJkNGRlZTE1ZGZiMTY3YTljODczZmM0YmI4YTgxZjZmMmFiNDQ4YTkxOA== 

As you can see it does not match! Any help ?
My DB Version: Server version: 10.0.23-MariaDB MariaDB Server

Francesco Marchioni
  • 4,091
  • 1
  • 25
  • 40

2 Answers2

3

Remove -binary from echo -n "admin" | openssl dgst -sha256 -binary | openssl base64 and you will get the same results.

with -binary it returns the actual binary data of the sha256 string, if thats what you want, you have to convert result from SHA2('admin',256) to binary and then apply TO_BASE64 to it.

ogres
  • 3,660
  • 1
  • 17
  • 16
  • 2
    The exact query to get the hash you want is select TO_BASE64(UNHEX(SHA2('admin',256))) This answer gives a little more info on how SHA2 works http://stackoverflow.com/a/22121627/947240 – dMcNavish Mar 22 '16 at 15:50
1
echo -n "admin" | openssl dgst -sha256 | awk '{ print $2; }' | openssl base64
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Bachvaroff
  • 11
  • 1