လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> PHP ၏ crypt function ကို အသုံးပြု. Algorithms မျိုးစုံအတွက်ဆားတင်းပလိတ်များကိုမည်သို့ဖန်တီးနိုင်မည်နည်း။

PHP ၏ crypt function ကို အသုံးပြု. Algorithms မျိုးစုံအတွက်ဆားတင်းပလိတ်များကိုမည်သို့ဖန်တီးနိုင်မည်နည်း။

M66 2025-06-11

PHP တွင် crypt function သည် password hashing အတွက်ဂန္ထဝင်လုပ်ဆောင်ချက်ဖြစ်သည်။ ၎င်းသည် Des, MD5, Blowfish, Sha-256 ကဲ့သို့သောစာဝှက်စနစ် algorithms အမျိုးမျိုးကိုထောက်ပံ့သည်။ သင့်တော်သောဆားကိုအသုံးပြုခြင်းသည်စကားဝှက်လုံခြုံရေးကိုပိုမိုကောင်းမွန်စေသည်သာမကစာဝှက်ဖြင့် algorithms နှင့် hashings ၏ရှုပ်ထွေးမှုကိုလည်းထိန်းချုပ်သည်။

ဤဆောင်းပါးသည် algorithms များစွာအတွက်ဆားတင်းပလိတ်များကို ဒယ် နဲလ်တင်းပလိတ်များကိုမည်သို့ဖန်တီးနိုင်မည်ကိုအသေးစိတ်ရှင်းပြပါမည်။

Crypt လုပ်ဆောင်ချက်များ၏အခြေခံအသုံးပြုမှု

 $password = 'mypassword';
$salt = '$6$rounds=5000$usesomesillystring$';  // SHA-512 ဥပမာဆား
$hash = crypt($password, $salt);
echo $hash;

အထက်ဖော်ပြပါဥပမာတွင် ဒေါ်လာ 6 ဒေါ်လာ သည် Sha-512 algorithm ကို အသုံးပြု. Rounds = 5000 သည် encryption aterations အရေအတွက်ကိုကိုယ်စားပြုပြီးအောက်ပါ string သည်ဆားတန်ဖိုး၏ခန္ဓာကိုယ်ဖြစ်သည်။

ကွဲပြားခြားနားသော algorithms နှင့်သက်ဆိုင်သောဆားပုံစံ

algorithm Salt Prefix ဆားအရှည်နှင့်ဖော်ပြချက်
သံ ရှေ့ဆက်မရှိဘူး 2- ဇာတ်ကောင်ဆား
MD5 $ 1 $ 8- ဇာတ်ကောင်ဆား
လေ $ 2A $ သို့မဟုတ် $ 2y $ $ 22 ဇာတ်ကောင်ဆား (ဗားရှင်းနှင့်ကုန်ကျစရိတ် parametersters အပါအ 0 င်)
Sha-256 $ 5 $ optional parameter သည်အခင်း, ဆားခန္ဓာကိုယ် 16 လုံးအထိဆားခန္ဓာကိုယ်
Sha-512 $ 6 $ optional parameter သည်အခင်း, ဆားခန္ဓာကိုယ် 16 လုံးအထိဆားခန္ဓာကိုယ်

ဤအရာများကိုနားလည်ပြီးနောက်ကျွန်ုပ်တို့သည်မတူညီသော algorithms ပေါ်တွင် အခြေခံ. သင့်လျော်သောဆားတင်းပလိတ်များကိုပိုမိုကောင်းမွန်စေသည်။

Salt Templates ကို dynamically dynamicatic လုပ်ခြင်း

ဤတွင် PHP function ၏ဥပမာတစ်ခုမှာ input algorithm name ကို အခြေခံ. သက်ဆိုင်ရာပုံစံဖြင့်ဆားပုံစံကိုထုတ်လုပ်နိုင်သည့်ဥပမာတစ်ခုဖြစ်သည်။

 function generateSalt($algorithm = 'sha512', $rounds = 5000) {
    // ကျပန်းဆားခန္ဓာကိုယ်မျိုးဆက် function ကို,အရှည် algorithm ကန့်သတ်ချက်များအရချိန်ညှိသည်
    function randomSalt($length) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./';
        $salt = '';
        for ($i = 0; $i < $length; $i++) {
            $salt .= $chars[random_int(0, strlen($chars) - 1)];
        }
        return $salt;
    }

    switch (strtolower($algorithm)) {
        case 'des':
            // DES ဆားမတိုင်မီသာ2လူသူမ
            return substr(randomSalt(2), 0, 2);

        case 'md5':
            // MD5 ဆားပုံစံ $1$salt$
            return '$1$' . randomSalt(8) . '$';

        case 'blowfish':
            // Blowfish ပမာဏ $2y$cost$22charsalt
            $cost = 10;  // 2ဂဏန်း,ဖြစ်ရန်အကြံပြုသည်04ရောက်လာ31အကြား
            $costStr = str_pad($cost, 2, '0', STR_PAD_LEFT);
            return '$2y$' . $costStr . '$' . randomSalt(22);

        case 'sha256':
            // SHA-256 ပမာဏ $5$rounds=xxxx$salt$
            return '$5$rounds=' . intval($rounds) . '$' . randomSalt(16) . '$';

        case 'sha512':
        default:
            // SHA-512 ပမာဏ $6$rounds=xxxx$salt$
            return '$6$rounds=' . intval($rounds) . '$' . randomSalt(16) . '$';
    }
}

အသုံးပြုမှုဥပမာ

 $password = 'my_secret_password';

// ရွေးချယ်ရေး algorithm
$algorithm = 'blowfish';

// သက်ဆိုင်ရာဆားတင်းပလိတ်ထုတ်လုပ်ရန်
$salt = generateSalt($algorithm);

// hash generate
$hash = crypt($password, $salt);

echo "algorithm: {$algorithm}\n";
echo "Salt Template: {$salt}\n";
echo "hash generate: {$hash}\n";

ဤနည်းအားဖြင့်သင်သည်စကားဝှက်များကိုစာဝှက်သောအခါအသုံးပြုသော algorithms နှင့်ဆား parameters များကိုအလွန်ပြောင်းလွယ်ပြင်လွယ်ထိန်းချုပ်နိုင်သည်။

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

  • ပိုမိုလုံခြုံသော Password Hashing Scheme ကို အသုံးပြု. PHP ၏ built-in password_hash နှင့် passwords and password ကိုထည့်သွင်းစဉ်းစားရန်အကြံပြုသည်။

  • Salt ကြိုးများ၌ parameters များကိုကိုယ်တိုင်စူးစူးစိုက်စိုက်ရန်ကြိုးစားပါ

  • ပုံမှန်အားဖြင့် ပုံမှန် ကိုပုံမှန်ညှိပါ။ လုံခြုံရေးလိုအပ်ချက်များနှင့်အညီလုံခြုံရေးလိုအပ်ချက်အရ,