PHP計算MD5和SHA1值

2021-06-19 04:04:55 字數 4021 閱讀 2380

最近開發專案,需要用到檔案的md5和sha1值,這裡簡單地記錄下,備忘。md5的計算分為計算字串和檔案的md5值,sha1的計算分為計算字串和檔案的sha1值。

(php 4, php 5)

md5 — 計算字串的 md5 雜湊值

report a bug

string

md5( string$str[, bool$raw_output= false

] )使用 » rsa 資料安全公司的 md5 報文演算法計算str的 md5 雜湊值。

report a bug

str

原始字串。

raw_output

如果可選的raw_output被設定為true,那麼 md5 報文摘要將以16位元組長度的原始二進位制格式返回。

report a bug

以 32 字元十六進製制數字形式返回雜湊值。

report a bug

版本說明5.0.0

新增raw_output引數。

report a bug

example #1 md5()範例

<?php

$str = ;

if (

md5(

$str

) === 

'1f3870be274f6c49b3e31a0c6728957f'

) ?>

report a bug

note:secure password hashing

由於此函式依賴的演算法已不足夠複雜,不推薦使用此函式對明文密碼加密。詳細內容參見這裡。

report a bug

(php 4 >= 4.2.0, php 5)

md5_file — 計算指定檔案的 md5 雜湊值

report a bug

string

md5_file( string$filename[, bool$raw_output= false

] )使用 » rsa 資料安全公司的 md5 報文演算法計算filename檔案的 md5 雜湊值並返回。該雜湊值為 32 字元的十六進製制數字。

report a bug

filename

檔名raw_output

如果被設定為true,那麼報文摘要將以原始的 16 位二進位制格式返回。

report a bug

成功返回字串,否則返回false

report a bug

版本說明5.0.0

新增raw_output引數。

5.1.0

函式改用流 api。這意味著能夠配合封裝器使用該函式,比如 md5_file('')。

report a bug

example #1 md5_file()使用範例

<?php

$file 

= 'php-5.3.0alpha2-win32-vc9-x64.zip'

;echo 

'md5 file hash of ' 

. $file 

. ': ' 

. md5_file

($file

);?>

report a bug

(php 4 >= 4.3.0, php 5)

sha1 — 計算字串的 sha1 雜湊值

report a bug

string

sha1( string$str[, bool$raw_output= false

] )利用» 美國安全雜湊演算法 1 計算字串的 sha1 雜湊值。

report a bug

str

輸入字串。

raw_output

如果可選的raw_output引數被設定為true,那麼 sha1 摘要將以 20 字元長度的原始格式返回,否則返回值是乙個 40 字元長度的十六進製制數字。

report a bug

返回 sha1 雜湊值字串。

report a bug

版本說明5.0.0

新增raw_output引數。

report a bug

example #1 sha1()範例

<?php

$str = ;

if (

sha1

($str

) === 

'd0be2dc421be4fcd0172e5afceea3970e2f3d940'

) ?>

report a bug

note:secure password hashing

由於此函式依賴的演算法已不足夠複雜,不推薦使用此函式對明文密碼加密。詳細內容參見這裡。

report a bug

(php 4 >= 4.3.0, php 5)

sha1_file — 計算檔案的 sha1 雜湊值

report a bug

string

sha1_file( string$filename[, bool$raw_output= false

] )利用» 美國安全雜湊演算法 1,計算並返回由filename指定的檔案的 sha1 雜湊值。該雜湊值是乙個 40 字元長度的十六進製制數字。

report a bug

filename

要雜湊的檔案的檔名。

raw_output

如果被設定為true,sha1 摘要將以 20 字元長度的原始格式返回。

report a bug

成功返回乙個字串,否則返回false

report a bug

example #1 sha1_file()範例

<?php

foreach(

glob

('/home/kalle/myproject/*.php'

) as 

$ent

)echo 

$ent 

. ' (sha1: ' 

. sha1_file

($ent

) . 

')', 

php_eol;}

?>

report a bug

版本說明5.0.0

新增raw_output引數。

5.1.0

改變函式以使用流 api。這意味著可以使用包裝器,比如 sha1_file('')。

report a bug

Android獲取SHA1和MD5值

1.androidstudio簽名及獲取sha1 一 生成簽名檔案 1.在as選單欄中找到 bulid 這一欄,選擇 generate signed apk 2.這裡選擇新建乙個檔案。3.填寫完整的簽名資訊。這裡密碼統一填寫 android 別名寫個 key 即可。其他的資訊根據實際情況和需求填寫,...

SHA1和md5的使用

sha1的全稱是secure hash algorithm 安全雜湊演算法 加密雜湊函式將任意長度的二進位制字串對映為固定長度的小型二進位制字串。加密雜湊函式有這樣乙個屬性 在計算上不大可能找到雜湊為相同的值的兩個不同的輸入 也就是說,兩組資料的雜湊值僅在對應的資料也匹配時才會匹配。資料的少量更改會...

Python實現計算檔案MD5和SHA1的方法示例

不多說,直接原始碼 file md5 import sys import hashlib import os.path def getfilemd5 strfile file none bret false strmd5 strsha1 try file open strfile,rb md5 ha...