လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> opensssl function ကိုနှင့်အတူပေါင်းစပ် hash_final လုံခြုံရေးအလေ့အကျင့်

opensssl function ကိုနှင့်အတူပေါင်းစပ် hash_final လုံခြုံရေးအလေ့အကျင့်

gitbox 2025-05-19

PHP တွင် Hash_final သည် hash constructions ၏အစိတ်အပိုင်းတစ်ခုဖြစ်ပြီး Hash တွက်ချက်မှုများကိုပြီးစီးရန်နှင့်နောက်ဆုံးအစာကြေငြာရန်အသုံးပြုသည်။ Openssllsl_encrypt , OpenStsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decryptsl_decrypts ဤဆောင်းပါးသည်ဤကိရိယာနှစ်ခုကိုဘေးကင်းစွာပေါင်းစပ်ရန်,

1 ?? hash_final ၏အခြေခံအသုံးပြုမှုကိုနားလည်ပါ

Hash_Final ကို Hash ကိုတွက်ချက်ရန် hash ကိုတွက်ချက်ရန် hash_init နှင့် hash_update နှင့် တွဲဖက်. အသုံးပြုသည်။

 $context = hash_init('sha256');
hash_update($context, 'data part 1');
hash_update($context, 'data part 2');
$hash = hash_final($context);

? သတိပြုရန် :

  • hash_final ဟုခေါ်သည်နှင့်တစ်ပြိုင်နက်အခြေအနေကိုမွမ်းမံရန် အသုံးပြု. မရပါ။

  • ရရှိသော $ hash သည် စစ်မှန်သော parameter တစ်ခုမကျပါက binary form တွင်ရှိသည်။

ဥပမာ -

 $hash = hash_final($context, true); // raw binary

အဘယ်ကြောင့် binary ကိုအာရုံစိုက်? ဘာဖြစ်လို့လဲဆိုတော့ Openssll ရဲ့လုပ်ဆောင်ချက်တွေ (ဥပမာ openssl_encrypt ) ဆိုတဲ့ functions တွေကတော့သော့ချက် (သို့) IV

2 ?? Openssll function ကိုလုံခြုံစွာပေါင်းစပ်

သင် hash_final ကို အသုံးပြုသောအခါသော့ () ကိုထုတ်လုပ်ရန်နှင့်၎င်းကို openssl ထဲသို့ကူးယူရန်၎င်းကိုဖွင့်ရန်အထူးဂရုပြုရန်လိုအပ်သည်။

? တူညီသောအရှည်သေချာ

  • ဥပမာ AES-128 သည် 16-byte key တစ်ခုလိုအပ်သည်။ သင့်တွင် Sha256 ရှိပါက Substr သည် ပထမ 16 bytes ကိုကြားဖြတ်ရန်လိုအပ်သည်။

? မူရင်း binary mode ကိုသုံးပါ

  • default hexadecimal output (64 bytes) ကိုသော့အဖြစ်မသုံးပါနှင့်။ OpenSSL_encrypt သည် string မဟုတ်ဘဲအမှန်တကယ် byte အရှည်လိုအပ်သည်။

ဥပမာ -

 $context = hash_init('sha256');
hash_update($context, 'my secret passphrase');
$rawKey = hash_final($context, true); // 32 bytes (sha256 output)

$key = substr($rawKey, 0, 16); // For AES-128
$iv = substr($rawKey, 16, 16); // Use next 16 bytes as IV

3 ?? အကြံပြုထားသည့်လုံခြုံရေးကုဒ်အလေ့အကျင့်များ

ပြည့်စုံသောလုံခြုံရေးဥပမာ - သော့ကိုရယူရန် hash_final ကို အသုံးပြုပါ

 <?php

$passphrase = 'super_secret_password';
$data = 'Sensitive data to encrypt';

// Derive key + IV from passphrase
$context = hash_init('sha256');
hash_update($context, $passphrase);
$rawHash = hash_final($context, true); // 32 bytes

$key = substr($rawHash, 0, 16); // AES-128 key
$iv = substr($rawHash, 16, 16); // IV

// Encrypt data
$ciphertext = openssl_encrypt(
    $data,
    'AES-128-CBC',
    $key,
    OPENSSL_RAW_DATA,
    $iv
);

// Encode ciphertext for transport (e.g., base64)
$encoded = base64_encode($ciphertext);
echo "Encrypted: $encoded\n";

// Decrypt
$decoded = base64_decode($encoded);
$decrypted = openssl_decrypt(
    $decoded,
    'AES-128-CBC',
    $key,
    OPENSSL_RAW_DATA,
    $iv
);
echo "Decrypted: $decrypted\n";
?>

ဒီမှာအသုံးပြုတဲ့ URL (သင်စာဝှက်ထားတဲ့ဒေတာတွေကိုပို့ဖို့လိုအပ်မယ်ဆိုရင်)

 https://gitbox.net/api/submit