PostgreSQL 使用 SHA256 和预定义的盐对列进行哈希处理

PostgreSQL hash a column using SHA256 and a pre-defined salt

我正在使用:x86_64-pc-linux-gnu 上的 PostgreSQL 11.6,由 gcc (GCC) 4.9.3 编译,64 位 运行 在 AWS 上作为 Aurora (RDS)。

我已经成功实现了以下功能

SELECT encode(digest('email@example.com', 'sha256'),'hex');

但我需要使用第三方提供的盐。假设为了争论起见,盐是 'this_is_my_salt'。我该如何使用它?我只能找到使用算法生成盐的示例。

供应商希望我们使用他们的哈希值将他们数据库中的电子邮件地址与我们的进行比较。他们没有指定他们的数据库系统,但向我展示了他们的查询:

SELECT 'email@example.com' as unhashed_email, sha2('shared_salt_value' || lower('email@example.com')) as hashed_email

这对我使用以下答案之一尝试 postgres 中的示例产生了不同的散列:

SELECT encode(digest('email@example.com' || 'shared_salt_value', 'sha256'),'hex');

我的哈希以 db17e 开头.... 他们的哈希值以 b6c84 开头....

可能是编码或其他原因造成的差异?

这很简单,只需将字符串与盐连接起来即可:

SELECT encode(digest('this_is_my_salt' || 'email@example.com', 'sha256'),'hex');