Home > Net >  How i do store public key and private key in Laravel Storage app folder
How i do store public key and private key in Laravel Storage app folder

Time:01-13

In PHP , i would do this

<?php

//create new private and public key
$new_key_pair = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));

openssl_pkey_export($new_key_pair, $private_key_pem);

$details = openssl_pkey_get_details($new_key_pair);
$public_key_pem = $details['key'];
//save for later
file_put_contents('keys/private_key.pem', $private_key_pem);
file_put_contents('keys/public_key.pem', $public_key_pem);

This in turn will create a new folder call keys in the root directory & save both the public key and private key in the keys folder.

enter image description here

How do I do the same thing in laravel ?

I have tried this but it does not work

private function createKeys() {
    $new_key_pair = openssl_pkey_new(array(
        "private_key_bits" => 2048,
        "private_key_type" => OPENSSL_KEYTYPE_RSA,
    ));
    
    openssl_pkey_export($new_key_pair, $private_key_pem);

    $details = openssl_pkey_get_details($new_key_pair);
    $public_key_pem = $details['key'];


    $path = storage_path('app\keys');
    file_put_contents($path, $private_key_pem);


}

I am trying to save it in storage -> app -> keys

Thank you very much.

CodePudding user response:

use laravel Storage Facade

Storage::put('keys/private_key.pem', $content);

don't forget to import namespace

use Illuminate\Support\Facades\Storage;
  • Related