<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pgcrypto &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/pgcrypto/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 09 Oct 2017 12:33:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Best way for Password Encryption using pgcrypto&#8217;s Cryptographic functions</title>
		<link>https://www.dbrnd.com/2016/03/postgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions/</link>
					<comments>https://www.dbrnd.com/2016/03/postgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 01 Mar 2016 20:33:12 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[crypt()]]></category>
		<category><![CDATA[cryptography function]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[digest()]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[gen_salt()]]></category>
		<category><![CDATA[hmac()]]></category>
		<category><![CDATA[pgcrypto]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1851</guid>

					<description><![CDATA[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&#8217;s cryptographic functions for PostgreSQL password encryption. Generally, people are using MD5 and [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&amp;linkname=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions%2F&#038;title=PostgreSQL%3A%20Best%20way%20for%20Password%20Encryption%20using%20pgcrypto%E2%80%99s%20Cryptographic%20functions" data-a2a-url="https://www.dbrnd.com/2016/03/postgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions/" data-a2a-title="PostgreSQL: Best way for Password Encryption using pgcrypto’s Cryptographic functions"></a></p><p>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.</p>
<p>I am sharing some of the important pgcrypto&#8217;s cryptographic functions for PostgreSQL password encryption.</p>
<blockquote><p>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.</p></blockquote>
<p>The PostgreSQL provides pgcrypto module or extension with the set of cryptography functions.</p>
<p><strong>First, We need to install the pgcrypto extension in PostgreSQL. </strong></p><pre class="crayon-plain-tag">CREATE EXTENSION pgcrypto;</pre><p>
<strong>Below is a list of hashing algorithm along with its require bit size.</strong></p>
<ul>
<li>MD5 = 128-bit hash value.</li>
<li>SHA1 = 160-bit hash value.</li>
<li>SHA224 = 224-bit hash value.</li>
<li>SHA256 = 256-bit hash value.</li>
<li>SHA384 = 384-bit hash value.</li>
<li>SHA512 = 512-bit hash value.</li>
</ul>
<p><strong>Other Important Hashing Functions:</strong></p>
<blockquote><p><strong>digest() :</strong><br />
It computes a binary hash of the given data. It supports all standard algorithms like: md5, sha1, sha224, sha256, sha384 and sha512.<br />
We can use ENCODE() to convert a binary string into hexadecimal.</p><pre class="crayon-plain-tag">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');</pre><p>
</p></blockquote>
<blockquote>
<p><strong>hmac() :</strong><br />
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.</p><pre class="crayon-plain-tag">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');</pre><p>
</p></blockquote>
<blockquote>
<p><strong>crypt() and gen_salt() :</strong><br />
This is more secure than any other cryptographic functions.<br />
This is specially designed for hashing passwords.<br />
crypt() does the hashing and gen_salt() prepare algorithm parameters for it.</p>
<p>Generally, when we are using another algorithm like md5(), it generates the same string for same password text.</p>
<p><strong><code>The most important point is, it generates a random string even for same password text. </code></strong></p>
<p><strong>Supported algorightms for crypt():</strong><br />
<a href="http://www.postgresql.org/docs/9.4/static/pgcrypto.html" target="_blank">Reference taken from the PostgreSQL official document.</a></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLGen_SaltAlgorithms.png" rel="attachment wp-att-1863"><img fetchpriority="high" decoding="async" class="alignnone wp-image-1863 size-full" title="PostgreSQL Gen_Salt Algorithms" src="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLGen_SaltAlgorithms.png" alt="PostgreSQL Gen_Salt Algorithms" width="660" height="128" srcset="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLGen_SaltAlgorithms.png 660w, https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLGen_SaltAlgorithms-300x58.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLGen_SaltAlgorithms-150x29.png 150w" sizes="(max-width: 660px) 100vw, 660px" /></a></p>
<p><strong>Below is a small demonstration of this:<br />
</strong></p>
<p><strong>Create a sample table:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_TestPassword (MyPassword TEXT);</pre><p>
<p><strong>Insert a sample encrypted passwords:</strong></p><pre class="crayon-plain-tag">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')));</pre><p>
<p><strong>Check the result and identify different string for even a same password:</strong></p><pre class="crayon-plain-tag">SELECT *FROM tbl_TestPassword;</pre><p>
<a href="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLCryptoPassword.png" rel="attachment wp-att-1862"><img decoding="async" class="alignnone wp-image-1862 size-full" title="PostgreSQL Crypto Password" src="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLCryptoPassword.png" alt="PostgreSQL Crypto Password" width="279" height="88" srcset="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLCryptoPassword.png 279w, https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLCryptoPassword-150x47.png 150w" sizes="(max-width: 279px) 100vw, 279px" /></a></p>
<p><strong>Check and authenticate given password: </strong></p><pre class="crayon-plain-tag">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.</pre><p>
</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/03/postgresql-best-way-for-password-encryption-using-pgcryptos-cryptographic-functions/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
