Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raven-rock/3731ec14c8719aa2d7939a4930758844 to your computer and use it in GitHub Desktop.
Save raven-rock/3731ec14c8719aa2d7939a4930758844 to your computer and use it in GitHub Desktop.
postgresql_hashes_stored_as_binary_cheatsheet.sql
WITH xs as (
select '' as x -- empty string
union all select 'a' -- lowercase letter
union all select 'A' -- uppercase
union all select 'aa' -- two ascii letters
union all select 'á' -- 'a' with accent
union all select '的' -- one CJK character, stored as multiple bytes
union all select 'deadbeef'
union all select 'DEADBEEF'
)
select
x
, length(x) as readable_char_len
, octet_length(x) as byte_len
-- , md5(x)::bytea as md5_bin_THIS_IS_WRONG
, decode(md5(x), 'hex') as md5_bin -- RIGHT
, octet_length(decode(md5(x), 'hex')) as md5_bin_bytelen
, encode(decode(md5(x), 'hex'),'hex') as md5hex_to_md5bin_to_md5hex_roundtrip
, md5(x) as md5_hex
, md5(x)=encode(decode(md5(x), 'hex'),'hex') unittest
from xs
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment