Hashing a String to a Numeric Value in PostgreSQL

前端 未结 4 1368
情歌与酒
情歌与酒 2020-12-07 23:16

I need to Convert Strings stored in my Database to a Numeric value. Result can be Integer (preferred) or Bigint. This conversion is to be done at Database side in a PL/pgSQL

4条回答
  •  渐次进展
    2020-12-07 23:42

    Must it be an integer? The pg_crypto module provides a number of standard hash functions (md5, sha1, etc). They all return bytea. I suppose you could throw away some bits and convert bytea to integer.

    bigint is too small to store a cryptographic hash. The largest non-bytea binary type Pg supports is uuid. You could cast a digest to uuid like this:

    select ('{'||encode( substring(digest('foobar','sha256') from 1 for 16), 'hex')||'}')::uuid;
                     uuid                 
    --------------------------------------
     c3ab8ff1-3720-e8ad-9047-dd39466b3c89
    

提交回复
热议问题