Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encrypt passwords with PostgreSQL?

I have some problems with encoding passwords,how can I do it. Type of encoding md5

digest(data text, type text) returns bytea;
CREATE OR REPLACE FUNCTION md(bytea) returns text AS $$
    SELECT encode(digest($1, 'sha1'), 'md5')
$$ LANGUAGE SQL STRICT IMMUTABLE;

INSERT INTO "login"(login, password, employee_id)
VALUES ( 'email',crypt('password', md('md5')), 1);

*** Error ***

ERROR: syntax error at or near "digest"
SQL state: 42601
Character: 1
like image 403
Karen Manukyan Avatar asked Sep 06 '13 11:09

Karen Manukyan


People also ask

Does PostgreSQL support encryption?

PostgreSQL offers encryption at several levels, and provides flexibility in protecting data from disclosure due to database server theft, unscrupulous administrators, and insecure networks. Encryption might also be required to secure sensitive data such as medical records or financial transactions.

Where does PostgreSQL store passwords?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .

What encryption does PostgreSQL use?

Transparent Data Encryption, or TDE, is used to secure the data at rest. In other words, it encrypts the data in a database to prevent an attacker from reading the data if they break the first line of defense.

What is data type for password in PostgreSQL?

This module implements a data type chkpass that is designed for storing encrypted passwords. Each password is automatically converted to encrypted form upon entry, and is always stored encrypted.


1 Answers

digest(data text, type text) returns bytea; is not valid syntax.

I recommend using bcrypt instead. No additional function definitions are required:

INSERT into "login" (login, password, employee_id) 
     VALUES ('email',crypt('password', gen_salt('bf'));

Later...

UPDATE table SET password = crypt('password',gen_salt('bf'))

And checking the password:

SELECT ... FROM table 
    WHERE password is NOT NULL 
      AND password = crypt('password-to-test',password);

Bcrypt is recommended by Crafted Software and Jeff Atwood. The official pgcrypto docs may also be of interest.

like image 114
Mark Stosberg Avatar answered Sep 18 '22 05:09

Mark Stosberg