1

I have written the following function:

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;

And I attach it to a trigger to fire before an INSERT. However, even if a provide a valid SHA-256 hash, the exception still gets raised, which implies the hash doesn't match the pattern. What could possibly be the problem here?

1 Answer 1

2

Reverse the logic. You are raising exception if the hash is valid.

Try this:

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.