title: PHP pg_unescape_bytea producing different results on different machines
body: <p>OK, so this question is related to one I asked previously:
  <a href="https://stackoverflow.com/questions/15241827/storing-the-output-of-openssl-random-pseudo-bytes-in-postgres-using-php-i-get-a">Storing the output of openssl_random_pseudo_bytes in Postgres using php? I get an error: invalid byte sequence for encoding “UTF8”</a>. Which was answered, but now I've encountered another issue.</p>
  
  <p>I'm using <code>openssl_random_pseudo_bytes</code> to generate a salt, I then add that to the users password and hash the entire thing.</p>
  
  <p>I then use <code>pg_escape_bytea</code> on the salt so that I can insert / store the value into a PostgreSQL database. This much works fine. Now to retrieve the salt when needed, I get it from the database, and use <code>pg_unescape_bytea</code> on the salt to get the original raw value that was used to create the hashed password.</p>
  
  <p>Now I've found that on another machine, the <code>pg_unescape_bytea</code> salt value is different, even though the exact same code is running, with the same version of PHP, from the very same database.</p>
  
  <p>How is this possible? Surely if the same code is running, off the exact same database, it should be identical?</p>
  
  <p><strong>Should I not be unescaping the salt from the database after it is inserted, perhaps it is returned unescaped?</strong> </p>
  
  <p><strong>Or should I be first escaping the salt (using <code>pg_unescape_bytea</code> or <code>base64_encode</code> ?) on the salt BEFORE combining it to the password?</strong></p>
  
  <p>Update with example:</p>
  
  <pre><code>$password = 'mypassword';
  
  $salt = openssl_random_pseudo_bytes(30);
  
  $pepper = 'pepper';
  
  // Perhaps I should be performing a pg_escape_bytea on the salt before doing this?
  $password_hash = hash('sha512', $password . $salt . $pepper);
  
  // Adds the user to the database
  $result = $data-&gt;add($email, $password_hash, pg_escape_bytea($salt), $company_id, $role_id);
  </code></pre>
  
  <p>Then when fetching the salt from the database:</p>
  
  <pre><code>// ...some code 
  
  // ...last step when fetching the salt from the database.
  $row = pg_fetch_assoc($result, 0);
  
  $row = pg_unescape_bytea( $row['salt'] ); // This value outputted produces different results
  </code></pre>
  
  <p>Also, I should note:</p>
  
  <p>On one machine, the salt after being unescaped looks like gobbledygook, like non-UTF characters or something, which is what I'd expect when representing binary.</p>
  
  <p>But on the other machine, unescaped salt looks practically identical to the escaped salt. Minus a <code>\</code></p>
  
  <p><strong>Example:</strong></p>
  
  <p>Escaped salt: \x34e21</p>
  
  <p>Unescaped salt: x34e21</p>
  
  <p><strong>Versions:</strong></p>
  
  <p>PHP version: 5.3.13</p>
  
  <p>PostgreSQL version: 9.1</p>