PHP တွင် hash_final function ကို hash ` hash_init မှဖန်တီးထားသော) ၏နောက်ဆုံးအဆင့်ကိုပြီးစီးရန်နှင့်တွက်ချက်ထားသည့် hash value ကိုပြန်ပို့ရန်အသုံးပြုသည်။ ၎င်းသည် PHP ၏ hash extension နှင့်သက်ဆိုင်ပြီးပုံမှန်အားဖြင့် hash_init , hash_update_fr_stream နှင့် hash_update_file တို့ ဖြင့်အသုံးပြုသည်။
ဒါကြောင့် Hash Values ကိုတွက်ချက်ဖို့ hash_final ကို သုံးတဲ့အခါ, ဒီလိုမျိုးဒေတာပုံစံတွေ (ဥပမာ strings, binary data တွေ, ဖိုင်တွေစတာတွေစသဖြင့်) ဘယ်လိုအမျိုးအစားလဲ။ ဤဆောင်းပါးကိုအသေးစိတ်ဆွေးနွေးပါမည်။
အခြေခံဥပမာကိုအရင်ကြည့်ကြစို့။
<?php
$context = hash_init('sha256');
hash_update($context, 'Hello World');
$hash = hash_final($context);
echo $hash;
?>
ဤကုဒ်သည် SHA-256 helsh hello world ၏ Hello World (Hexadecimal-World) ၏ Sha-256 hash တန်ဖိုးကိုထုတ်ပေးလိမ့်မည်။
သတိပြုရန်:
Hash_update မှရရှိသောဒုတိယ parameter သည် string တစ်ခုဖြစ်သည်။
မည်သည့်စာအမျိုးအစားကိုမဆိုမည်သည့်နေရာတွင်မျှထောက်ပံ့ပါစေ (string တစ်ခုသို့ကူးပြောင်းနိုင်သည်) နောက်ဆုံးတွင်၎င်းကို byte sequence တစ်ခုအဖြစ်ဆောင်ရွက်လိမ့်မည်။
သင် (ascii (သို့) UTF-8 encoding) ကိုဖြတ်လျှောက်သောအခါ PHP သည်၎င်းကို byte sequences တွင်တိုက်ရိုက်လုပ်ဆောင်လိမ့်မည်။
ဥပမာအားဖြင့်:
hash_update($context, "abc"); // အဘယ်အရာကိုကိုင်တွယ်ဖြေရှင်းနေသည် '61 62 63' သုံး bytes
မှတ်စု: Multi-byte ဇာတ်ကောင်များ (ဥပမာတရုတ်ကဲ့သို့) ကိုသူတို့၏အခြေခံ byte sequence ကို "ဇာတ်ကောင်" granulareity တွင်ထက် hash ကို 0 င်ခြင်းဖြင့်တွက်ချက်သည်။
အကယ်. သင်သည် binary data များသို့သွားပါက File_get_contents မှရရှိသော binary stream, ၎င်းကို bytes မှတိုက်ရိုက်ကျွေးမွေးသည်။
$data = file_get_contents('https://gitbox.net/image.png');
hash_update($context, $data);
ဤနေရာတွင် ဒေါ်လာအချက်အလက်များ တွင် \ 0 (null byte), မြင့်မားသော byte စသည်ဖြင့်ပါ 0 င်နိုင်သည်။
အကယ်. သင်သည်ကြီးမားသောဖိုင်များအတွက် hash ကိုတွက်ချက်လိုပါက hash_update_stream ကို အသုံးပြုရန်အကြံပြုသည်။
$fp = fopen('https://gitbox.net/largefile.zip', 'rb');
$context = hash_init('sha256');
hash_update_stream($context, $fp);
$hash = hash_final($context);
fclose($fp);
ဤနေရာတွင်ဖိုင်အကြောင်းအရာကိုအတုံးများတွင်ဖတ်ပြီးဖိုင်တစ်ခုလုံးကိုမှတ်ဉာဏ်ထဲသို့မထည့်စေရန် Hash အခြေအနေတွင်ကျွေးမွေးသည်။
အကယ်. သင် hash ကိုဖိုင်တစ်ခုပေါ်တွင်သင်တိုက်ရိုက်တွက်ချက်လိုပါက Hash_File ကို သင်အသုံးပြုနိုင်သည်။ သို့သော်သင် hash_ * function မိသားစုကိုအသုံးပြုလိုပါကသင်လိုအပ်သည်။
$context = hash_init('sha256');
hash_update_file($context, 'https://gitbox.net/document.pdf');
$hash = hash_final($context);
hash_update_file ဖိုင်ကိုအလိုအလျောက်ဖွင့်သည်, ၎င်း၏အကြောင်းအရာများကိုဖတ်ပြီးအခြေအနေကိုအသစ်ပြောင်းသည်။
hash_final ရုံစုဆောင်းဒေတာပြီးအောင်။
Data သည်အခြေအနေတွင် ( hash_update , hash_update_stream , hash_update_file ) ကို byte sequences တွင်ဖတ်ရှုပြီးမူရင်း "ဒေတာအမျိုးအစား" ကိုဂရုမစိုက်ပါ။
သင်ဖြတ်သန်းသွားသောအချက်အလက်များသည်သင်၌ရှိသည် (အထူးသဖြင့် encoding သို့မဟုတ် binary လုံခြုံရေးပြ issues နာများ) ကိုသင်သေချာစေရန်သေချာစေရန်လိုအပ်သည်။