I am attempting to replicate some postgres code that uses the pgcrypto digest function into Java. I was assuming that the following two commands would generate the same result:
select digest('Hi there' || 'Abc' || decode('00','hex'), 'sha256');
select digest('Hi thereAbc' || decode('00','hex'), 'sha256');
However, this is not the case... Those two commands produce different results:
postgres=# select digest('Hi there' || 'Abc' || decode('00','hex'), 'sha256');
digest
--------------------------------------------------------------------
\xd4321124595112a1e8cd8d90709b8cf58aabdd14ad09ff972e6fe0f75bd25d97
postgres=# select digest('Hi thereAbc' || decode('00','hex'), 'sha256');
digest
--------------------------------------------------------------------
\xf2477e4fcaf6ea37ab985fc1fa029a99fb331657e7a3e349ffc844e9d068f250
Any idea why those give different results?
On the other hand, doing the concatenation without the bytes on the end works as expected:
postgres=# select digest('Hi thereAbc', 'sha256');
digest
------------------------------------------------------------------
\x3e1d7fed02b93824aba03835b93314f0293026276c8e50f4b70935f988e0c0bf
postgres=# select digest('Hi there' || 'Abc', 'sha256');
digest
------------------------------------------------------------------
\x3e1d7fed02b93824aba03835b93314f0293026276c8e50f4b70935f988e0c0bf
(1 row)
Thanks!
Edit: Answer update As per @ferhat elmas's answer, the two inputs to digest are as follows:
select 'Hi thereAbc' || decode('00', 'hex');
\x486920746865726541626300
select 'Hi there' || 'Abc' || decode('00', 'hex');
Hi thereAbc\x00
I was assuming those two values were equivalent - just one as bytes and the other as a string, but they are not - the backslash x 0 0 are literal characters in the string, not a single 0 byte.
('Hi there' || 'Abc') || decode('00','hex')?