user3206440
user3206440

Reputation: 5059

SHA-256 Hash and base64 encoding

With the string "123456Adrian Wapcaplet" I need to

  1. generate the SHA-256 hash
  2. take the least significant 128 bits of the hash
  3. represent these bits in base64 encoding

I need help with #1 and #3. Below is how I generate this in Linux

$ echo -n "123456Adrian Wapcaplet" | shasum -a 256 | cut -c33-64 | xxd -r -p | base64

jNWe9TyaqmgxG3N2fgl15w==

Upvotes: 2

Views: 4359

Answers (1)

teppic
teppic

Reputation: 7286

Install the pgcrypto extension:

create extension pgcrypto;

Then you can use the digest function to do sha256. The rest of the functions you'll need are built in:

select encode(substring(digest('123456Adrian Wapcaplet', 'sha256') from 17), 'base64');

Note that there's an implicit cast from text to bytea performed here when calling digest. It will use the default encoding for the database. To avoid problems due to environmental differences you can specify the encoding for the conversion:

digest(convert_to('123456Adrian Wapcaplet', 'utf8'), 'sha256')

Upvotes: 2

Related Questions