Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 March PostgreSQL: Best way for Password Encryption using pgcrypto’s Cryptographic functions

PostgreSQL: Best way for Password Encryption using pgcrypto’s Cryptographic functions

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

This is going to be one of the most important article for the PostgreSQL community because I am going to share, what should be our best practice to store encrypted passwords into PostgreSQL Database Server.

I am sharing some of the important pgcrypto’s cryptographic functions for PostgreSQL password encryption.

Generally, people are using MD5 and SHA algorithm for password encryption, but both are easy to break and vulnerable, so we should not use this in our general practice.

The PostgreSQL provides pgcrypto module or extension with the set of cryptography functions.

First, We need to install the pgcrypto extension in PostgreSQL.

1
CREATE EXTENSION pgcrypto;

Below is a list of hashing algorithm along with its require bit size.

  • MD5 = 128-bit hash value.
  • SHA1 = 160-bit hash value.
  • SHA224 = 224-bit hash value.
  • SHA256 = 256-bit hash value.
  • SHA384 = 384-bit hash value.
  • SHA512 = 512-bit hash value.

Other Important Hashing Functions:

digest() :
It computes a binary hash of the given data. It supports all standard algorithms like: md5, sha1, sha224, sha256, sha384 and sha512.
We can use ENCODE() to convert a binary string into hexadecimal.

1
2
3
4
5
6
SELECT ENCODE(DIGEST('AnveshPassword','md5'),'hex');
SELECT ENCODE(DIGEST('AnveshPassword','sha1'),'hex');
SELECT ENCODE(DIGEST('AnveshPassword','sha224'),'hex');
SELECT ENCODE(DIGEST('AnveshPassword','sha256'),'hex');
SELECT ENCODE(DIGEST('AnveshPassword','sha384'),'hex');
SELECT ENCODE(DIGEST('AnveshPassword','sha512'),'hex');

hmac() :
It calculates hashed MAC for data with the key. This is similar to digest(), but more secure because it requires the key.We can use ENCODE() to convert a binary string into hexadecimal.

1
2
3
4
5
6
SELECT ENCODE(HMAC('AnveshPassword','mykey','md5'),'hex');
SELECT ENCODE(HMAC('AnveshPassword','mykey','sha1'),'hex');
SELECT ENCODE(HMAC('AnveshPassword','mykey','sha224'),'hex');
SELECT ENCODE(HMAC('AnveshPassword','mykey','sha256'),'hex');
SELECT ENCODE(HMAC('AnveshPassword','mykey','sha384'),'hex');
SELECT ENCODE(HMAC('AnveshPassword','mykey','sha512'),'hex');

crypt() and gen_salt() :
This is more secure than any other cryptographic functions.
This is specially designed for hashing passwords.
crypt() does the hashing and gen_salt() prepare algorithm parameters for it.

Generally, when we are using another algorithm like md5(), it generates the same string for same password text.

The most important point is, it generates a random string even for same password text.

Supported algorightms for crypt():
Reference taken from the PostgreSQL official document.

PostgreSQL Gen_Salt Algorithms

Below is a small demonstration of this:

Create a sample table:

1
CREATE TABLE tbl_TestPassword (MyPassword TEXT);

Insert a sample encrypted passwords:

1
2
3
INSERT INTO tbl_TestPassword VALUES (CRYPT('AnveshPassword', GEN_SALT('md5')));
INSERT INTO tbl_TestPassword VALUES (CRYPT('AnveshPassword', GEN_SALT('md5')));
INSERT INTO tbl_TestPassword VALUES (CRYPT('MyNewPassword', GEN_SALT('md5')));

Check the result and identify different string for even a same password:

1
SELECT *FROM tbl_TestPassword;

PostgreSQL Crypto Password

Check and authenticate given password:

1
2
3
4
5
6
7
SELECT *FROM tbl_TestPassword WHERE MyPassword = (CRYPT('AnveshPassword', MyPassword));
 
-- It returns two records.
 
SELECT *FROM tbl_TestPassword WHERE MyPassword = (CRYPT('MyNewPassword', MyPassword));
 
-- It returns one record.

Mar 1, 2016Anvesh Patel
SQL Server: The Awesome TOP clause WITH TIES optionDatabase Theory: The truth about Universally Unique Identifier - UUID
Comments: 8
  1. Ashish
    October 25, 2016 at 5:58 am

    Very useful information.
    Thanx a lot.

  2. Ashvin patel
    December 17, 2016 at 11:54 am

    nice !!
    It’s very useful for me.
    Thanks a lot.

  3. Abhishek Singh
    July 7, 2017 at 4:37 am

    Nice explanation at one go every thing is there to understand.

  4. Kabelo
    October 14, 2017 at 11:22 am

    Thanks man

  5. anonymous
    December 27, 2017 at 3:49 am

    Is it a good idea to do user authentication with database; One SQL injection is enough to compromise all users credentials. Also sending plain text password over un-encrypted connection is bad practice.

  6. Bikash Sarkar
    March 17, 2018 at 10:06 am

    I need decryption logic also .

  7. Shariful
    June 26, 2019 at 10:00 am

    I need decryption logic also .

  8. Anonymous
    December 12, 2019 at 11:43 am

    Anvesh can we decrypt md5 encrypted password?.

    If yes could you please share the logic

Anvesh Patel
Anvesh Patel

Database Engineer

March 1, 2016 PostgreSQLAnvesh Patel, crypt(), cryptography function, database, database research and development, dbrnd, digest(), encryption, extension, gen_salt(), hmac(), pgcrypto, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....