Table of Contents
回复内容:
Home Backend Development PHP Tutorial hash - 如何解决PHP哈希函数的碰撞

hash - 如何解决PHP哈希函数的碰撞

Jun 06, 2016 pm 08:48 PM
hash php

最近想使用Hash函数来根据一个字符串来生成一个惟一的值,但是没找到好用的Hash函数,像PHP内置的MD5、SHA等哈希函数生成的值太长,我没法用,想截取,这样做又担心会出现哈希碰撞,特来拜求高人们用的是什么哈希函数

回复内容:

最近想使用Hash函数来根据一个字符串来生成一个惟一的值,但是没找到好用的Hash函数,像PHP内置的MD5、SHA等哈希函数生成的值太长,我没法用,想截取,这样做又担心会出现哈希碰撞,特来拜求高人们用的是什么哈希函数

虽然说采用什么哈希算法要看数据而定,但连MD5再嫌长,可真的有些难办了。其实我很想听听为什么“哈希函数生成的值太长,我没法用”。如果是没有任何评估,单凭感觉就这么说,那真的太过分了。

你可能需要的不是一个低冲撞的哈希算法,而是将哈希算法的输出压缩的字符串压缩算法。由于哈希算法输出的字符集不过16个,而ASCII的显示字符扣掉空格也剩94个,所以单从压缩字符串长度来看,这个想法应该能够让你满意。

输出变短(也就是哈希值的取值空间变小)必然增加哈希碰撞概率,不要有任何不切实际的幻想。也就是说,无论你的算法是什么,只要范围等同的减小,哈希碰撞的概率就会等同的增大。所以如果真的需要短输出的哈希函数,也就不必再费尽心思去找,截取这个办法已经足够有效了。

标题起的也不好。哈希碰撞的出现是必然的,“解决”哈希碰撞本身就不存在。

哈希碰撞虽然是小概率事件,但绝对不能怕,更不能躲,尤其不能当作“不存在”。一定要根据应用的需求,有明确的方法对待之。我的建议,要么加长哈希算法的取值空间,要么增加其他的比较特征,作为在哈希算法之外额外的补充。

长度越长,碰撞的几率越小。减少长度必然增加碰撞几率。 因为你是把原文空间隐射到哈希生成串的空间,串长度决定了空间的大小。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles