Open In App

PostgreSQL – MD5() Function

Last Updated : 06 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The PostgreSQL MD5() function is a useful tool for evaluating the MD5 hash of a given string and returning the result in hexadecimal form. This function is often used for data integrity checks and secure password storage.

Let’s look into the syntax, and usage of the MD5() function in PostgreSQL with detailed examples.

What is the MD5() Function in PostgreSQL?

The MD5() function computes the MD5 hash of a string and returns the result as a TEXT data type in hexadecimal form. The MD5 (Message-Digest Algorithm 5) is widely used for creating hash values that can verify data integrity and secure data.

How Does MD5() Work?

The MD5() function processes the input string through the MD5 hashing algorithm and outputs a 32-character hexadecimal string. This can be useful for storing hashed passwords, checking data integrity, and more.

Syntax

MD5(string)

Let’s analyze the above syntax:

  • The string argument is the string of which the MD5 hash is calculated.
  • The MD5() function is used to return a string in TEXT data type form.

PostgreSQL MD5() Function Examples

Let us look into some of the examples of MD5 Function in PostgreSQL to better understand the concept.

Example 1: Basic Usage with a Simple Message

The following statement shows the use of the MD5() function to return the MD5 hash of the message ‘GeeksForGeeks MD5’:

SELECT MD5('GeeksForGeeks MD5');

Output:

PostgreSQL MD5 Function Example

Explanation: Here, the MD5() function converts the input string ‘GeeksForGeeks MD5’ into its MD5 hash in hexadecimal form.

Example 2: Hashing a Longer Message

The following statement shows the use of the MD5() function to return the MD5 hash of the message ‘This is going to be converted into hexadecimal form’:

SELECT MD5('This is going to be converted into hexadecimal form');

Output:

PostgreSQL MD5 Function Example

Explanation: In this example, the MD5() function processes a longer string and returns its MD5 hash.

Important Points About PostgreSQL MD5() Function

  • The MD5() function always returns a 32-character hexadecimal string.
  • The input string is case-sensitive, meaning ‘abc’ and ‘ABC’ will produce different MD5 hashes.
  • MD5 hashing is a one-way process and cannot be reversed to retrieve the original string.
  • While MD5 is useful for checksums and non-critical applications, it is not recommended for security-critical applications due to vulnerabilities.


Similar Reads

three90RightbarBannerImg