How hash Function works in PHP ? - GeeksforGeeks (2024)

Last Updated : 29 Jan, 2024

Summarize

Comments

Improve

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.

Table of Content

  • Hashing with md5() Function
  • Hashing with sha1() Function
  • Password Hashing with password_hash() Function
  • Hashing Data with hash() Function

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";

?>

Output

Original 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";

?>

Output

Original 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";

?>

Output

Original Password: mySecurePasswordHashed 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";

?>

Output

Original Data: Hello, World!SHA-256 Hash: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f


How hash Function works in PHP ? - GeeksforGeeks (1)

Improve

Please Login to comment...

How hash Function works in PHP ? - GeeksforGeeks (2024)
Top Articles
CVE - Search Results
Fair Credit Reporting Act | Bureau of Justice Assistance
3 Tick Granite Osrs
Ups Customer Center Locations
Xre-02022
Libiyi Sawsharpener
Jailbase Orlando
Craigslist Benton Harbor Michigan
Poplar | Genus, Description, Major Species, & Facts
Craigslist Dog Sitter
Tanger Outlets Sevierville Directory Map
Cinepacks.store
Does Pappadeaux Pay Weekly
Aita Autism
Purple Crip Strain Leafly
Med First James City
Craigslist Alabama Montgomery
Dumb Money
Labor Gigs On Craigslist
Cinebarre Drink Menu
Richland Ecampus
Wsop Hunters Club
Hewn New Bedford
Hannaford To-Go: Grocery Curbside Pickup
Mta Bus Forums
Restored Republic
Imagetrend Elite Delaware
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
Angel del Villar Net Worth | Wife
APUSH Unit 6 Practice DBQ Prompt Answers & Feedback | AP US History Class Notes | Fiveable
Promatch Parts
Kaiser Infozone
Emiri's Adventures
Puerto Rico Pictures and Facts
Despacito Justin Bieber Lyrics
Labyrinth enchantment | PoE Wiki
2020 Can-Am DS 90 X Vs 2020 Honda TRX90X: By the Numbers
Nba Props Covers
Appraisalport Com Dashboard Orders
13 Fun &amp; Best Things to Do in Hurricane, Utah
Makes A Successful Catch Maybe Crossword Clue
Haunted Mansion (2023) | Rotten Tomatoes
Gary Vandenheuvel Net Worth
Sandra Sancc
Missed Connections Dayton Ohio
Fallout 76 Fox Locations
Deviantart Rwby
Lsreg Att
Bomgas Cams
Inloggen bij AH Sam - E-Overheid
Escape From Tarkov Supply Plans Therapist Quest Guide
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6410

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.