How hash Function works in PHP ?
Last Updated :
29 Jan, 2024
Hashing Functions play a crucial role in securing sensitive data by converting it into a fixed-size string of characters, often a hash value or checksum. PHP provides several built-in hashing functions for various purposes, such as password hashing, data integrity verification, and more.
The Hashing technique implements the hash Function to transform the data into a fixed-length string of characters, which represents the hash value. The hash function ensures the data will be encrypted in its true form, without manipulating its original form. Thus, it helps in authenticating and authorizing while the transformation of data takes place. It is a single-way transformation technique, which signifies that it is computationally infeasible to reverse the process & retrieve the original data from the hash.
There are several in-built hash algorithms, such as md5 (), sha1 (), etc, which are most commonly used for the encryption of information, are described below.
Hashing with md5() Function
The md5() function generates a 32-character hexadecimal number representing the hash value of a given string.
Example:
PHP
<?php
$data = "Hello, World!";
$hash = md5($data);
echo "Original Data: $data\n";
echo "MD5 Hash: $hash\n";
?>
OutputOriginal Data: Hello, World!
MD5 Hash: 65a8e27d8879283831b664bd8b7f0ad4
Hashing with sha1() Function
The sha1() function produces a 40-character hexadecimal number representing the SHA-1 hash value of a string.
Example:
PHP
<?php
$data = "Hello, World!";
$hash = sha1($data);
echo "Original Data: $data\n";
echo "SHA-1 Hash: $hash\n";
?>
OutputOriginal Data: Hello, World!
SHA-1 Hash: 0a0a9f2a6772942557ab5355d76af442f8f65e01
Password Hashing with password_hash() Function
The password_hash() function is specifically designed for secure password hashing using bcrypt, which is a strong, adaptive hashing algorithm.
Example:
PHP
<?php
$password = "mySecurePassword";
$hashedPassword = password_hash(
$password, PASSWORD_BCRYPT);
echo "Original Password: $password\n";
echo "Hashed Password: $hashedPassword\n";
?>
OutputOriginal Password: mySecurePassword
Hashed Password: $2y$10$fMakDUqb6p7sEGzYRXaS0ecO73SB3/GTR.r7QJjMbbdtQBVvt.HQm
Hashing Data with hash() Function
The hash() function supports a variety of algorithms, including SHA-256 and SHA-512. It provides more flexibility but requires manual management of salt and options.
Example:
PHP
<?php
$data = "Hello, World!";
$hashedData = hash('sha256', $data);
echo "Original Data: $data\n";
echo "SHA-256 Hash: $hashedData\n";
?>
OutputOriginal Data: Hello, World!
SHA-256 Hash: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read