Table of Contents
Use of cryptographic hash function" >Use of cryptographic hash function
Traverse all supported algorithm types" >Traverse all supported algorithm types
Salted S2K algorithm generates password digest" >Salted S2K algorithm generates password digest
Summary" >Summary
Home Backend Development PHP Problem How to use Mhash function in PHP

How to use Mhash function in PHP

Jun 02, 2021 pm 06:01 PM
php

This article will introduce to you how to use the Mhash function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use Mhash function in PHP

#What we are going to learn this time is another Hash encryption extension. However, this extension Mhash has been integrated into the Hash extension. At the same time, it should be noted that this extension is no longer recommended. We should directly use the functions in the Hash extension to perform Hash encryption operations. Therefore, we still understand for the purpose of learning today. Regarding the content of Hash extension, we can check out the previous article: PHP Hash Information Summary Extension Framework.

Use of cryptographic hash function

$hash = mhash(MHASH_MD5, "测试Mhash");
echo $hash, PHP_EOL;
echo bin2hex($hash), PHP_EOL;
// /�8�><�۠�P4q�j�
// 2fcb38e93e3cc8dba09f503471846a9d

$hash = hash(&#39;md5&#39;, "测试Mhash");
echo $hash, PHP_EOL;
// 2fcb38e93e3cc8dba09f503471846a9d

$hash = mhash(MHASH_MD5, "测试Mhash", &#39;hmac secret&#39;);
echo $hash, PHP_EOL;
echo bin2hex($hash), PHP_EOL;
// �k�<F�m �OM����
// b86bb83c46b76d09be4f4daf18ebfe85
Copy after login

As can be seen from the code, the use of mhash() function and hash() are very similar. Of course, their functions Same thing. However, what is encrypted by the mhash() function is directly binary. After we convert this content into hexadecimal through bin2hex(), we can see that the structure encrypted by the ordinary hash() function is exactly the same.

When performing hmac encryption, just add key directly to the third parameter.

Traverse all supported algorithm types

Of course, just like Hash encryption, Mhash encryption can also choose different algorithms. We can also directly use the relevant functions to see the encryption algorithms supported in the current environment.

echo mhash_count(), PHP_EOL;

$nr = mhash_count(); // 33

for ($i = 0; $i <= $nr; $i++) {
    echo sprintf("Hash:%s,块大小为: %d\n",
        mhash_get_hash_name($i),
        mhash_get_block_size($i));
}
// Hash:CRC32,块大小为: 4
// Hash:MD5,块大小为: 16
// Hash:SHA1,块大小为: 20
// Hash:HAVAL256,块大小为: 32
// Hash:,块大小为: 0
// Hash:RIPEMD160,块大小为: 20
// Hash:,块大小为: 0
// Hash:TIGER,块大小为: 24
// Hash:GOST,块大小为: 32
// Hash:CRC32B,块大小为: 4
// Hash:HAVAL224,块大小为: 28
// Hash:HAVAL192,块大小为: 24
// Hash:HAVAL160,块大小为: 20
// Hash:HAVAL128,块大小为: 16
// Hash:TIGER128,块大小为: 16
// Hash:TIGER160,块大小为: 20
// Hash:MD4,块大小为: 16
// Hash:SHA256,块大小为: 32
// Hash:ADLER32,块大小为: 4
// Hash:SHA224,块大小为: 28
// Hash:SHA512,块大小为: 64
// Hash:SHA384,块大小为: 48
// Hash:WHIRLPOOL,块大小为: 64
// Hash:RIPEMD128,块大小为: 16
// Hash:RIPEMD256,块大小为: 32
// Hash:RIPEMD320,块大小为: 40
// Hash:,块大小为: 0
// Hash:SNEFRU256,块大小为: 32
// Hash:MD2,块大小为: 16
// Hash:FNV132,块大小为: 4
// Hash:FNV1A32,块大小为: 4
// Hash:FNV164,块大小为: 8
// Hash:FNV1A64,块大小为: 8
// Hash:JOAAT,块大小为: 4
Copy after login

PHP also provides a lot of constants to represent these algorithms, such as the MHASH_MD5 we used in the previous code. In fact, just add MHASH_ in front of the content we traverse. The specific list of supported constants can be found in the official manual, so we will not copy and paste it here.

Salted S2K algorithm generates password digest

In addition, Mhash also provides us with a very convenient Salted S2K algorithm that can be used to easily generate a very convenient set of Password encrypts content.

// OpenPGP 指定的 Salted S2K 算法
$hashPassword = mhash_keygen_s2k(MHASH_SHA1, &#39;我的密码&#39;, random_bytes(2), 4);
echo $hashPassword, PHP_EOL;
echo bin2hex($hashPassword), PHP_EOL;
// �-!=
// 101ab899
Copy after login

Of course, this algorithm is also relatively safe. It has a salt parameter, and it can specify the length of the returned data. It also returns binary data. If you need to save standard text content, you need to convert it into hexadecimal form. But relatively speaking, I think it is safer to directly generate binary content.

Summary

Different functions have different application scenarios, but in fact, Mhash has no special application scenarios. After all, the related functions in the Hash extension are fully capable It has replaced its function, and it is richer and easier to use. If you see the use of these functions in old projects, you can slowly replace them with new functions through reconstruction.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202007/source/PHP%E7%9A%84Mhash%E6%89%A9%E5%B1%95%E5%87%BD%E6%95%B0%E7%9A%84%E5%AD%A6%E4%B9%A0.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to use Mhash function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles