Jump to content

Inserting binary string to MySQL BINARY column


anson5

Recommended Posts

I'm planning on storing RIPEMD-160 passphrase hash's raw binary data in MySQL as BINARY(20).  Right now, I'm not sure if one of the following INSERT approach is more preferrable and more efficient:

 

Approach (1) - Using mysqli::real_escape_string()

=================================================

 

$binhash = $mysqli->real_escape_string(hash('ripemd160', $passphrase, true));
$mysqli->query("INSERT INTO testtbl (Passphrase) VALUES ('" . $binhash . "')");

 

This approach saves hash() from converting raw binary data to hexadecimal format, but need to process and escape the binary string.

 

Approach (2) - Using UNHEX() on MySQL

=====================================

 

$hexhash = hash('ripemd160', $passphrase);
$mysqli->query("INSERT INTO testtbl (Passphrase) values (UNHEX('" . $hexhash . "'))");

 

This approach needs hash() to convert binary data to hexadecimal but no need for escaping the string and it put extra load on MySQL to convert the hexadecimal back to binary.

 

Link to comment
Share on other sites

There are two reasons for storing hash in binary format:

[*]It saves half the storage size, for a 160-bit hash, it's BINARY(20) vs CHAR(40)

[*]Operations on BINARY is faster according to benchmarks

 

So my question will be which one will be faster:

[*]mysqli->real_escape_string() on a 20 byte binary string (note that I used hash($algo, $passwd, true) to output raw binary data), or

[*]telling hash() to output hexadecimal character string (conversion from binary needed) plus UNHEX on MySQL?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.