PHP တွင် hash_final function သည် incremental hash context ကိုဖြည့်စွက်ပြီးနောက်ဆုံး hash တန်ဖိုးကိုပြန်ပို့ရန်အသုံးပြုသောကိရိယာတစ်ခုဖြစ်သည်။ ၎င်းကို hash တွက်ချက်မှုများကိုပြုလုပ်ရန်သို့မဟုတ် streaming data များ၏ hash တွက်ချက်မှုများကိုကိုင်တွယ်ရန် Hash_init , hash_update စသည့်လုပ်ဆောင်မှုများနှင့် တွဲဖက်. အသုံးပြုလေ့ရှိသည်။ သို့သော် developer များစွာသည် hash_final ကို အသုံးပြုသောအခါပြ problems နာအချို့ကိုကြုံတွေ့ရလိမ့်မည်။ ဤဆောင်းပါးသည်ဤပြ problems နာများကိုအသေးစိတ်ခွဲခြမ်းစိတ်ဖြာပြီးဖြေရှင်းနည်းများပေးလိမ့်မည်။
hash_final ၏အဓိပ္ပါယ်သည်အောက်ပါအတိုင်းဖြစ်သည် -
string hash_final ( HashContext $context [, bool $raw_output = FALSE ] )
$ Context : hash_init ကဖန်တီး hash အခြေအနေ။
$ Raw_output : အကယ်. စစ်မှန်သော , output ကုန်ကြမ်း binary data ကိုသတ်မှတ်လျှင်, ဒီလိုမှမဟုတ်ရင် output အသေး hexadecimal string ကို။
ရိုးရှင်းသောဥပမာ -
$context = hash_init('sha256');
hash_update($context, 'hello');
$finalHash = hash_final($context);
echo $finalHash;
ဖြစ်ရပ် -
hash_final ဟုခေါ်သည်နှင့်ပြီးသည်နှင့်ဆက်စပ်သော $ အခြေအနေတွင် မမှန်ကန်ပါ။ အကယ်. သင်သည်တူညီသောအခြေအနေကိုထပ်မံအသုံးပြုရန်ကြိုးစားပါက -
$context = hash_init('sha256');
hash_update($context, 'hello');
$final1 = hash_final($context);
$final2 = hash_final($context); // အမှား!
သတိပေးချက်တစ်ခုသို့မဟုတ်အမှားတစ်ခုဖြစ်ပေါ်စေလိမ့်မယ်။
ဖြေရှင်းချက် -
သင်အခြေအနေကိုပြန်လည်အသုံးပြုရန်လိုအပ်ပါက hash_final မတိုင်မီ hash_copy ကို ခေါ်ပါ။
$context = hash_init('sha256');
hash_update($context, 'hello');
$contextCopy = hash_copy($context);
$final1 = hash_final($context);
$final2 = hash_final($contextCopy);
ဖြစ်ရပ် -
လူအတော်များများသည်ပုံမှန်အားဖြင့် string output ကိုသာယူပြီး ၎င်းကိုဒေါ်လာ raw_output ကို လျစ်လျူရှုသည်။ အကယ်. သင်သည် output ကိုတိုက်ရိုက်ထုတ်လွှင့်ရန် ECHO ကိုအသုံးပြုပါကသင် barbled code ကိုရလိမ့်မည်။
$context = hash_init('sha256');
hash_update($context, 'hello');
$final = hash_final($context, true);
echo $final; // အမှိုက်ပုံးများကိုပြသနိုင်သည်
ဖြေရှင်းချက် -
ဖတ်လို့ရသည့်အကြောင်းအရာများကိုပြသရန် bin2hex သို့မဟုတ် base64_encode ကို သုံးနိုင်သည်။
$final = hash_final($context, true);
echo bin2hex($final);
ဖြစ်ရပ် -
Hash () ကို အသုံးပြု. ကြီးမားသောဖိုင်များသို့မဟုတ်ကြီးမားသောအချက်အလက်များစီးဆင်းမှုအတွက် hash ကို အသုံးပြုသည့်အခါ Memory ကိုတိုက်ရိုက်စားသုံးသည်။
$hash = hash('sha256', file_get_contents('largefile.dat'));
ဖြေရှင်းချက် -
hashh_init + hash_update ကို စမ်းသုံးပါ။
$context = hash_init('sha256');
$handle = fopen('largefile.dat', 'rb');
while (!feof($handle)) {
$data = fread($handle, 8192);
hash_update($context, $data);
}
fclose($handle);
$finalHash = hash_final($context);
ဖြစ်ရပ် -
တူညီသော input data များသည် PHP တွင်ပါ 0 င်သောတန်ဖိုးများကိုအခြားဘာသာစကားများဖြင့်တွက်ချက်သည်။
ဖြေရှင်းချက် -
သေချာအောင်လုပ်ပါ:
input encoding သည်တသမတ်တည်း (UTF-8 vs Utf-16) ။
အပိုဆောင်းလိုင်းချိုးသို့မဟုတ်နေရာများရှိပါသည်ရှိမရှိ။
တူညီသောနည်းလမ်း (မူရင်း vs encoded စာသား) ကိုတွက်ချက်။
ဥပမာ - PHP ကိုအသုံးပြုပြီး UTF-8 encoded string ၏ Sha-256 hash ကိုတွက်ချက်သည်။
$context = hash_init('sha256');
hash_update($context, mb_convert_encoding($input, 'UTF-8'));
$finalHash = hash_final($context);
ဖြစ်ရပ် -
URL ကို hash စိစစ်အတည်ပြုသည့်အခါပုံမှန်အားဖြင့်ပုံမှန်အတိုင်း hashes ကွဲပြားခြားနားသော hashes ကိုရရှိခြင်းဖြစ်သည်။
$url1 = 'https://gitbox.net/page';
$url2 = 'https://gitbox.net/page/';
ဖြေရှင်းချက် -
တွက်ချက်မှုမပြုမီ URL ကိုပုံမှန်ပုံမှန် -
function normalizeUrl($url) {
$parsed = parse_url($url);
$scheme = $parsed['scheme'] ?? 'http';
$host = $parsed['host'] ?? '';
$path = rtrim($parsed['path'] ?? '/', '/');
return "$scheme://$host$path";
}
$context = hash_init('sha256');
hash_update($context, normalizeUrl('https://gitbox.net/page/'));
$finalHash = hash_final($context);