Debug School

rakesh kumar
rakesh kumar

Posted on

How to generating a public key using Laravel's OpenSSL library and saving it to a file for download

Sure! Here is an example code for generating a public key using Laravel's OpenSSL library and saving it to a file for download:

use Illuminate\Http\Response;

// Generate a public key using OpenSSL
$keyPair = openssl_pkey_new([
    'digest_alg' => 'sha256',
    'private_key_bits' => 2048,
    'private_key_type' => OPENSSL_KEYTYPE_RSA,
]);
$publicKey = openssl_pkey_get_details($keyPair)['key'];

// Save the public key to a file for download
$fileName = 'public_key.pem';
$fileContent = $publicKey;
$fileSize = strlen($fileContent);
$fileHeaders = [
    'Content-Type' => 'application/octet-stream',
    'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
];

// Send the file as a response to download
return response()->streamDownload(function () use ($fileContent) {
    echo $fileContent;
}, $fileName, $fileHeaders, $fileSize);
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

=======================================

 $file = 'privatekey.pem';
  file_put_contents($file, $private_key);
  $to = $admin_email;
  $subject = 'PrivateKey';
  $from = 'my@wiz.com';
  $attachmentName = 'privatekey.pem';
  Mail::send([], [], function ($message) use ($to, $subject, $from, $file, $attachmentName) {
      $message->to($to)
          ->subject($subject)
          ->from($from)
          ->attach($file, ['as' => $attachmentName]);
  });
  Mail::to($privatekey['email'])->send(new privatekey($privatekey, $validToken)); 
  $result = openssl_verify($data, $signature, $public_key, OPENSSL_ALGO_SHA256);
      file_put_contents($merge, $public_key);
      file_put_contents($merge1, $private_key);
      $publicPath = public_path();
      log::info($publicPath);
      file_put_contents(public_path($merge1), $private_key);
      // download function start here
      ob_clean();
      $file = storage_path('app/private_key.pem');
      file_put_contents($file, $private_key);      
      // Download the file
      return response()->download($file, $merge1, [
          'Content-Type' => 'application/octet-stream',
          'Content-Disposition' => 'attachment',
      ]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)