我写了以下函数:
CREATE FUNCTION Validate_Password_Hash() RETURNS trigger AS $$
BEGIN
IF (NEW.Password ~* '^[a-f0-9]{64}$')
THEN
RAISE EXCEPTION 'The password hash is invalid! Please use SHA-256. You tried to insert: %', NEW.Password;
END IF;
RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;
我将它附加到触发器以在INSERT之前触发。但是,即使提供有效的SHA-256哈希,仍然会引发异常,这意味着哈希不匹配该模式。这可能是什么问题?
答案 0 :(得分:1)
扭转逻辑。 如果散列有效,则引发异常。
试试这个:
CREATE FUNCTION Validate_Password_Hash() RETURNS trigger AS $$
BEGIN
IF (NEW.Password !~* '^[a-f0-9]{64}$')
THEN
RAISE EXCEPTION 'The password hash is invalid! Please use SHA-256. You tried to insert: %', NEW.Password;
END IF;
RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;