当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > sha1() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 sha1() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 475 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The sha1() function calculates the SHA-1 hash of a string.
sha1()函数的作用是:计算字符串的SHA-1 hash。

The sha1() function uses the US Secure Hash Algorithm 1.
sha1()函数使用的是美国 Secure Hash 运算法则1。

From RFC 3174 - The US Secure Hash Algorithm 1: "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."
RFC1321的解释 -美国 Secure Hash 运算法则1:“SHA-1创造出了一个名为信息散列的160位结果。举个例子来说,这个信息散列可以输进一个用来产生和验证信息签名的签名档运算法则中。给信息散列进行署名并不意味着对信息进行改进,或是增强信息的处理效率。这是因为:信息散列在通常状况下都会比原信息来的小。相同的hash运算法则必须通过一个数字签名验证器(这个数字签名验证器是给数据签名的创建者使用的)来使用,”

This function returns the calculated SHA-1 hash on success, or FALSE on failure.
如果这个函数成功执行,将返回计算之后的SHA-1 hash,如果执行失败,将返回False。

Syntax
语法

sha1(string,raw)

Parameter参数 Description描述
string Required. The string to be calculated
必要参数。指定需要参与计算的字符串
raw Optional. Specify hex or binary output format:
可选参数。指定输出结果的格式(包括十六进制和二进制)
  • TRUE - Raw 20 character binary format
    TRUE – 最原始的由20个字符表示的二进制格式
  • FALSE - Default. 40 character hex number
    FALSE – 默认值。由40位字符表示的十六进制数

Note: This parameter was added in PHP 5.0
注意:这个参数仅在PHP5.0以上版本中支持



Example 1
案例1

<?php
$str = 'Hello';
echo sha1($str);
?> 

The output of the code above will be:
上述代码将输出下面的结果:

f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0


Example 2
案例2

In this example we will print the result of sha1() and then test it:
在下面的案例中,我们将输出sha1()函数的结果并对其进行测试:

<?php
$str = 'Hello';
echo sha1($str);
if (sha1($str) == 'f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0') { echo "<br />Hello world!"; exit; }
?> 

The output of the code above will be:
上述代码将输出下面的结果:

f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
Hello world!

评论 (0) All

登陆 | 还没注册?