\Gishiki\Security\Encryption\AsymmetricPrivateKey

This class represents a private key for the asymmetric encryption engine.

Note: This class uses OpenSSL for strong encryption

Summary

Methods
Properties
Constants
generate()
__construct()
__destruct()
exportPublicKey()
export()
isLoaded()
__toString()
__invoke()
No public properties found
RSA512
RSA1024
RSA2048
RSA4096
RSAEXTREME
No protected methods found
No protected properties found
N/A
No private methods found
$key
N/A

Constants

RSA512

RSA512

RSA1024

RSA1024

RSA2048

RSA2048

RSA4096

RSA4096

RSAEXTREME

RSAEXTREME

Properties

$key

$key : resource

Type

resource — the private key ready to be used by OpenSSL

Methods

generate()

generate(integer  $keyLength = self::RSA4096) : string

Create a random private key of the given length (in bits).

You can use predefined constants to have valid keylength values.

The higher the key length, the higher the security the higher the required time to generate the key.

Usage example:

//give a name to the file containing the generation result $filename = APPLICATION_DIR."newkey.private.pem";

//generate the new key $serailized_key = PrivateKey::generate(PrivateKey::RSA4096);

//export to file the serialized key file_put_contents($filename, $serailized_key); //NOTE: this example is really BAD because the file is NOT encrypted

//yes, you can load private keys directly from file $private_key = new PrivateKey("file://".$filename);

Parameters

integer $keyLength

the length (in bits) of the private key to e generated

Throws

\InvalidArgumentException

the given key length is not an integer power of two

\Gishiki\Security\Encryption\Asymmetric\AsymmetricException

the error occurred while generating and exporting the new private key

Returns

string —

the serialized private key

__construct()

__construct(string  $customKey, string  $customKeyPassword = '') 

Used to create a private key from the given serialized key.

Parameters

string $customKey

the private key serialized as a string

string $customKeyPassword

the password to decrypt the serialized private key (if necessary)

Throws

\InvalidArgumentException

the given key and/or password isn't a valid string

\Gishiki\Security\Encryption\Asymmetric\AsymmetricException

the given key is invalid

__destruct()

__destruct() 

Free resources used to hold this private key.

exportPublicKey()

exportPublicKey() : string

Export the public key corresponding to this private key.

Returns

string —

the public key exported from this private key

export()

export(string  $keyPassword = '') : string

Export this private key in a string format.

The resulting string can be used to construct another PrivateKey instance:

use Gishiki\Security\Encryption\Asymmetric\PrivateKey;

//this is the exported private key $exported_key = "...";

//rebuild the private key $privateKey = new PrivateKey($exported_key);

Parameters

string $keyPassword

the private key password

Throws

\InvalidArgumentException

the given password is not a string

\Gishiki\Security\Encryption\Asymmetric\AsymmetricException

the given key is invalid

Returns

string —

the serialized private key

isLoaded()

isLoaded() : boolean

Check if the key has been loaded.

Returns

boolean —

true if the key has been loaded

__toString()

__toString() : string

Proxy call to the export() function.

Returns

string —

the serialized key

__invoke()

__invoke() : array

Export a reference to the native private key and its length in bits.

Throws

\Gishiki\Security\Encryption\Asymmetric\AsymmetricException

the key cannot be exported

Returns

array —

the array that contains the key and its legth (in bytes)