PHP - hash() 函式



定義和用法

hash() 函式根據演算法(如 md5、sha256)返回給定資料的雜湊值。返回值是一個包含十六進位制值的字串。

語法

hash ( string $algo , string $data [, bool $raw_output = FALSE ] ) : string

引數

序號 引數及描述
1

algo

雜湊演算法的名稱。hash 函式可以使用很多演算法,一些重要的演算法包括 md5、sha256 等。
要獲取支援的完整演算法列表,請使用雜湊函式 hash_algos()

2

data

要生成雜湊值的資料。請注意,一旦生成雜湊值,就無法反轉。

3

raw_output

預設值為 false,因此它返回小寫十六進位制值。如果值為 true,則返回原始二進位制資料。

返回值

PHP hash() 函式返回一個包含小寫十六進位制值的字串。如果 raw_output 設定為 true,則返回原始二進位制資料。

PHP 版本

此函式可在 PHP 5.1.2 以上版本中使用。

示例 1

使用 md5 演算法生成雜湊值:

<?php
   echo "The hash of Welcome to Tutorialspoint is - ". hash('md5', 'Welcome to Tutorialspoint');
?>

輸出

這將產生以下結果:

The hash of Welcome to Tutorialspoint is - 8ab923b97822bd258bf882e41de6ebff

示例 2

使用 sha256 演算法生成雜湊值:

<?php
   echo "The hash of Welcome to Tutorialspoint is - ". hash('sha256', 'Welcome to Tutorialspoint');
?>

輸出

這將產生以下結果:

The hash of Welcome to Tutorialspoint is - a6baf12546b9a5cf6df9e22ae1ae310b8c56be2da2e9fd2c91c94314eb0e5a2e

示例 3

使用 crc32b 演算法生成雜湊值:

<?php
   echo "The hash of Welcome to Tutorialspoint is - ". hash('crc32b', 'Welcome to Tutorialspoint');
?>

輸出

這將產生以下結果:

The hash of Welcome to Tutorialspoint is - cd12151c

示例 4

將 raw_output 設定為 true 生成雜湊值:

<?php
   echo "The hash of Welcome to Tutorialspoint is - ". hash('md5', 'Welcome to Tutorialspoint', true);
?>

輸出

這將產生以下結果:

The hash of Welcome to Tutorialspoint is - ��#�x"�%�������
php_function_reference.htm
廣告