What I have tried so far
SELECT md5(text) will return text (hex strings) .
After that We need to xor them
SELECT x'hex_string' # x'hex_string';
But the above results in binary values. How do I again convert them into hex string?
Is there anyway to xor md5 values in postgresql and convert this into hexadecimal values again ?
Those binary values are in fact of type bit varying
, which differs significantly from bytea
.
bit varying
comes with built-in support for XOR and such, but PostgreSQL doesn't provide a cast from bit varying
to bytea
.
You could write a function that does the cast, but it's not trivial and probably not the more efficient way in your case.
It would make more sense to XOR the md5 digests directly. PostgreSQL doesn't provide the XOR operator for bytea
either, but it can be easily written in a function, especially when assumed that the operands have an equal length (16 bytes in the case of md5 digests):
CREATE FUNCTION xor_digests(_in1 bytea, _in2 bytea) RETURNS bytea
AS $$
DECLARE
o int; -- offset
BEGIN
FOR o IN 0..octet_length(_in1)-1 LOOP
_in1 := set_byte(_in1, o, get_byte(_in1, o) # get_byte(_in2, o));
END LOOP;
RETURN _in1;
END;
$$ language plpgsql;
Now the built-in postgresql md5
function that produces an hex string is not the best fit for post-processing, either. The pgcrypto
module provides this function instead:
digest(data text, type text) returns bytea
Using this function and getting the final result as an hex string:
select encode(
xor_digest ( digest('first string', 'md5') ,
digest('second string', 'md5')),
'hex');
produces the result: c1bd61a3c411bc0127c6d7ab1238c4bd
with type text
.
If pgcrypto
can't be installed and only the built-in md5
function is available, you could still combine encode
and decode
to achieve the result like this:
select
encode(
xor_digest(
decode(md5('first string'), 'hex'),
decode(md5('second string'), 'hex')
),
'hex'
);
Result:
c1bd61a3c411bc0127c6d7ab1238c4bd
来源:https://stackoverflow.com/questions/17739887/how-to-xor-md5-hash-values-and-cast-them-to-hex-in-postgresql