【发布时间】:2021-12-05 21:19:13
【问题描述】:
我正在学习使用 pgcrypto 中的 crypt() 函数来加密我的密码并将其存储到用户表中。
但是我不明白如果我们将现有密码作为第二个参数传递,crypt 函数如何生成相同的密码。
我想了解它是如何做到的。
do $$
declare
generated_salt text;
hashResult text;
authenticationPassword text;
begin
-- select the number of actors from the actor table
select public.gen_salt('bf')
into generated_salt;
select public.crypt('password', generated_salt)
into hashResult;
select public.crypt('password', hashResult)
into authenticationPassword;
-- show the number of actors
raise notice 'Generated salt : %', generated_salt;
raise notice 'Hash result : %', hashResult;
raise notice 'authenticationPassword : %', authenticationPassword;
end; $$
在上面的示例中,如果我执行并存储变量的值。
我得到: generated_salt 的“$2a$06$.lub5s4Eqz4.epcg5zW4Ke” hashResult 的“$2a$06$.lub5s4Eqz4.epcg5zW4KervErzw//uARn8F2gchj2wM11ok.MJLK” authenticationPassword
的“$2a$06$.lub5s4Eqz4.epcg5zW4KervErzw//uARn8F2gchj2wM11ok.MJLK”为什么 authenticationPassword 与 hashResult 相同,我原以为它会用 hashResult 作为盐再次对其进行哈希处理。
它如何判断它是否必须识别和计算相同的密码或基于盐生成新的哈希?
P.S : 我了解如何使用该功能来存储/检索密码,但我不明白它是如何做到的。
感谢您的帮助,
【问题讨论】:
标签: sql postgresql hash cryptography pgcrypto