လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> Crypt ကို အသုံးပြု. စကားဝှက်ကိုစာဝှက်နည်း

Crypt ကို အသုံးပြု. စကားဝှက်ကိုစာဝှက်နည်း

M66 2025-05-30

မျက်မှောက်ခေတ် web application development တွင်အသုံးပြုသူစကားဝှက်များကိုလုံခြုံသောသိုလှောင်မှုသည်အလွန်အရေးကြီးသည်။ PHP သည်စကားဝှက်စာဝှက်ခြင်းနည်းလမ်းအမျိုးမျိုးကိုထောက်ပံ့ပေးသည် ဤဆောင်းပါးသည် PHP ၏ crypt () function ကို အသုံးပြု. အသုံးပြုသူစကားဝှက်များကိုလုံလုံခြုံခြုံစာဝှက်ရန်နှင့်အတည်ပြုရန်နှင့်ဘုံလုံခြုံရေးထောင်ချောက်များကိုမည်သို့ရှောင်ရှားရမည်ကိုမိတ်ဆက်ပေးလိမ့်မည်။

1 ။ Crypt () function ကိုနားလည်ပါ

Crypt () သည်စကားဝှက် hash function ကို encryption algorithms ကိုထောက်ပံ့သည်။ သတ်သတ်မှတ်မှတ် algorithm သည် "ဆား" တွင်မူ "ဆား" တွင်မူတည်သည်။ ကွဲပြားခြားနားသော algorithms သည် SHA-256 , Sha-512 ကဲ့သို့သောဆားပုံစံများရှိသည်။

 string crypt(string $password, string $salt);
  • $ စကားဝှက် သည်အသုံးပြုသူမှထည့်သွင်းထားသော plaintext password ဖြစ်သည်။

  • encryption တစ်ခုစီ၏ရလဒ်များကိုသေချာစေရန် alcryption algorithm နှင့်ကျပန်းဆားတန်ဖိုးကိုသတ်မှတ်ရန်အသုံးပြုသည်

2 ။ လုံခြုံစွာဆားတန်ဖိုးများကိုထုတ်လုပ်ပါ

ဆား၏လုပ်ဆောင်ချက်သည်သက်တန့်စားပွဲတင်တိုက်ခိုက်မှုများကိုကာကွယ်ရန်ဖြစ်သည်။ စကားဝှက်တစ်ခုစီသည်ထူးခြားသောဆားတန်ဖိုးရှိသင့်သည်။ ခေတ်သစ် algorithms အတွက်ဆားတွင်ဆားများ, algorithm ဖော်ထုတ်ခြင်း,

အောက်ပါဥပမာသည် Sha-512 algorithm အပေါ် အခြေခံ. ဆားကိုထုတ်ပေးသည်။

 function generateSalt($length = 16) {
    $randomBytes = random_bytes($length);
    return '$6$rounds=5000$' . substr(strtr(base64_encode($randomBytes), '+', '.'), 0, 16) . '$';
}
  • $ 6 $ Sha-512 ကိုအသုံးပြုသည်။

  • Rounds = 5000 သည် Hash Itherations အရေအတွက်ကိုထိန်းချုပ်သည်။ ပိုမိုကြီးမားသောလေယာဉ်သုံးစွဲမှုပိုကြီးသည်။

  • ကျပန်းကြိုးဆားထူးခြားမှုကိုသေချာစေ။

3. Crypt ကို သုံး. စကားဝှက်ကိုစာဝှက်ပါ။

အထက်တွင်ထုတ်ပေးသောဆားကိုပေါင်းစပ်ပြီးဤကဲ့သို့သောစကားဝှက်ကို encrypt လုပ်နိုင်သည်။

 $password = 'user_password_here';
$salt = generateSalt();
$hash = crypt($password, $salt);

$ hash သည်ဒေတာဘေ့စ်တွင်သိမ်းဆည်းထားသောစကားဝှက် hash string ကိုဖြစ်သည်။

4 ။ အသုံးပြုသူစကားဝှက်ကိုစစ်ဆေးပါ

စိစစ်အတည်ပြုစဉ်အတွင်းသင်သည်စကားဝှက်နှင့် hash ကိုတိုက်ရိုက်နှိုင်းယှဉ်။ မရပါ လုပ်ဆောင်ချက်သည်ဆားကိုအလိုအလျောက်ထုတ်ယူပြီးစကားဝှက်ကို encrypt လုပ်လိမ့်မည်။

 function verifyPassword($inputPassword, $storedHash) {
    $hash = crypt($inputPassword, $storedHash);
    return hash_equals($hash, $storedHash);
}

အချိန်ဇယားကိုတိုက်ခိုက်မှုများကိုကာကွယ်ရန်နှင့်အတော်အတန်လုံခြုံမှုရှိစေရန် hash_equals () ကို သုံးပါ။

5 ။ ပြီးပြည့်စုံသောဥပမာ

 <?php
// ဆား function ကိုထုတ်လုပ်ပါ
function generateSalt($length = 16) {
    $randomBytes = random_bytes($length);
    return '$6$rounds=5000$' . substr(strtr(base64_encode($randomBytes), '+', '.'), 0, 16) . '$';
}

// မှတ်ပုံတင်သည့်အခါစာဝှက်ထားတဲ့စကားဝှက်
function encryptPassword($password) {
    $salt = generateSalt();
    return crypt($password, $salt);
}

// logging လုပ်တဲ့အခါစကားဝှက်ကိုစစ်ဆေးပါ
function verifyPassword($inputPassword, $storedHash) {
    $hash = crypt($inputPassword, $storedHash);
    return hash_equals($hash, $storedHash);
}

// နမူနာ
$userPassword = 'MySecurePass123';
$storedHash = encryptPassword($userPassword);

echo "encrypted hash: " . $storedHash . "\n";

$inputPassword = 'MySecurePass123';
if (verifyPassword($inputPassword, $storedHash)) {
    echo "စကားဝှက်ကိုစစ်ဆေးခြင်းအောင်မြင်ခဲ့သည်!";
} else {
    echo "စကားဝှက်!";
}
?>

6 ။ အပိုဆောင်းအကြံဥာဏ်

  • phot_hash () နှင့် password_verify () ၏ php justifyify () philverifierify (value_verify) ကို သုံးရန် ကြိုးစားပါ

  • ပုံမှန်စကားဝှက်ကိုဆေးထည့်သောနည်းဗျူဟာကိုအသစ်ပြောင်းရန်နည်းလမ်းများကိုတိုးမြှင့်ပေးရန်။

  • ဒေတာဘေ့စ်တွင်သိမ်းထားသော hash string တွင်ဆားနှင့် algorithm သတင်းအချက်အလက်များပါ 0 င်ပြီးဆားသိုလှောင်ထားရန်မလိုအပ်ပါ။