မျက်မှောက်ခေတ် web application development တွင်အသုံးပြုသူစကားဝှက်များကိုလုံခြုံသောသိုလှောင်မှုသည်အလွန်အရေးကြီးသည်။ PHP ၏ built-in crypt () function သည် function encryption algorithm အပါအ 0 င်စာဝှက်စနစ် algorithms အမျိုးမျိုးကိုထောက်ပံ့သည်။ Blowfish algorithm သည်၎င်း၏မြင့်မားသောပြင်းထန်မှုနှင့်စိတ်ကြိုက်ပြုလုပ်သောလုပ်ငန်းအချက်များအတွက်စကားဝှက်ကိုလျှော်ခြင်းဖြင့်ကျယ်ကျယ်ပြန့်ပြန့်အသုံးပြုသည်။ ဤဆောင်းပါးသည် PHP ၏ crypt () function ကိုအသုံးပြုရမည်ကိုအသေးစိတ်ဖော်ပြရန်မည်သို့အသုံးပြုရမည်ကိုအသေးစိတ်ဖော်ပြထားသည် (identifier $ 2Y $ 2Y $ ) ။
Blowish သည်အဟောင်းများကိုစာဝှက်စနစ် algorithms ကိုအစားထိုးရန်အထူးဒီဇိုင်းပြုလုပ်ထားသောအချိုးကျသော key encryithm ဖြစ်သည်။ Password Hashing အတွက် Blowish သည်၎င်း၏ညှိနှိုင်းမှုဆိုင်ရာအချက်များနှင့်အတူ blowish ကောင်းသော brute-protront cracking စွမ်းရည်ကိုထောက်ပံ့ပေးသည်။ အလုပ်အကြောင်းပိုမိုမြင့်မားလေလေစာဝှက်စနစ်ကိုတွက်ချက်ရန်ကြာကြာသည်, စကားဝှက်ကိုလုံခြုံရေးလုံခြုံအောင်ပြုလုပ်ခြင်းသည်ကြာကြာဖြစ်သည်။
PHP's Crypt () function သည် PHP 5.3.7 နှင့်အထက်တွင်ဖော်ပြထားသော format ကိုကိုယ်စားပြုရန် $ 2Y $ အမှတ်အသားကိုအသုံးပြုသည်။
Blowfish algorithm ၏ဆားသည် Az , AZ , 0 မှ 9 တွင် ပါ 0 င်သည့် 22-charac ဇာတ်ကောင် string တစ်ခုဖြစ်သည် ။ စုစုပေါင်း 64 ဇာတ်ကောင်အတွက်။ PHP ၏ crypt () မှလိုအပ်သောဆားပုံစံသည်အောက်ပါအတိုင်းဖြစ်သည် -
$2y$ + နှစ်ခုအလုပ်လုပ်အချက်များ(cost) + $ + 22 ဆားနည်းနည်းဆား
ဥပမာအားဖြင့်:
$salt = '$2y$12$' . substr(strtr(base64_encode(random_bytes(16)), '+', '.'), 0, 22);
ဤနေရာတွင် 12. ^ 12 hash တွက်ချက်မှုများကိုကိုယ်စားပြုသောအလုပ်လုပ်သည့်အချက်မှာအလုပ်ချိန် 4096 ခန့်သည်အလွန်နှေးကွေးသည်။
$password = 'your_password_here';
$hash = crypt($password, $salt);
ပြန်လာသော $ hash သည်အပြည့်အ 0 blowfish hash string ကို database သို့တိုက်ရိုက်သိမ်းဆည်းထားနိုင်သည်။
စကားဝှက်ကိုအတည်ပြုသည့်အခါ Crypt () ကို Call () ကိုနှိုင်းယှဉ်ရန်မူရင်း hash ကိုဆားအဖြစ်အသုံးပြုပါ။
function verify_password($password, $hash) {
return crypt($password, $hash) === $hash;
}
<?php
// လုံခြုံသောဆားထုတ်လုပ်ပါ
function generate_blowfish_salt($cost = 12) {
if ($cost < 4 || $cost > 31) {
throw new InvalidArgumentException("အဆိုပါအလုပ်လုပ်အချက်ဖြစ်ရမည်4ရောက်လာ31အကြား");
}
$randomBytes = random_bytes(16);
$base64Salt = substr(strtr(base64_encode($randomBytes), '+', '.'), 0, 22);
return sprintf('$2y$%02d$%s', $cost, $base64Salt);
}
// encryption password
function encrypt_password($password, $cost = 12) {
$salt = generate_blowfish_salt($cost);
return crypt($password, $salt);
}
// စကားဝှက်ကိုစစ်ဆေးပါ
function verify_password($password, $hash) {
return crypt($password, $hash) === $hash;
}
// စမ်းသပ်မှု
$password = 'mypassword123';
$hash = encrypt_password($password);
echo "စကားဝှက် hash: $hash\n";
if (verify_password('mypassword123', $hash)) {
echo "စကားဝှက်ကိုစစ်ဆေးခြင်းအောင်မြင်ခဲ့သည်!\n";
} else {
echo "စကားဝှက်ကိုစစ်ဆေးခြင်းမအောင်မြင်ပါ!\n";
}
?>
PHP တရားဝင်စာရွက်စာတမ်းများ https://www.m66.net/manual/zh/function.Crypt.php