开发者

How to Convert SHA1 and MD5 text-values to binary? (sql)

开发者 https://www.devze.com 2023-04-04 12:44 出处:网络
So I have SHA1 varchar sting like \'LQSVPLQSVPSYKLMTY7P5SVPSYN54CMGU\' = 32 bytes in database. How do I can convert it to binary sting?

So I have SHA1 varchar sting like 'LQSVPLQSVPSYKLMTY7P5SVPSYN54CMGU' = 32 bytes in database. How do I can convert it to binary sting?

开发者_如何学GoSELECT BINARY('LQSVPLQSVPSYKLMTY7P5SVPSYN54CMGU'); 

Is not what I looking for. It will be same 32-byte string, but in BLOB. I need get value from sha1 first. Is there any way in Mysql\Postgresql to do it? I also interested in similar MD5 sting conversion. Is there universal way?

This question related to Storing hexadecimal values as binary in MySQL.


As noted in the comments, basic SHA1 and MD5 values are hex escaped. This can easily be converted into a bytea by:

 select ('\x' || md5('test'))::bytea;

This requires a version of PostgreSQL new enough to support hex encoding of byteas.

MySQL likely has a very different way to do this and given the inconsistencies between the types you can't expect a cross-db solution here.

0

精彩评论

暂无评论...
验证码 换一张
取 消