class PrivateKey extends DSA implements PrivateKey (View source)

DSA Private Key

Traits

Password Protected Trait for Private Keys

Constants

ALGORITHM

Algorithm Name

Properties

static protected BigInteger $zero Precomputed Zero from  AsymmetricKey
static protected BigInteger $one Precomputed One from  AsymmetricKey
protected string $format Format of the loaded key from  AsymmetricKey
protected Hash $hash Hash function from  AsymmetricKey
static protected bool[] $engines Available Engines from  AsymmetricKey
protected BigInteger $p DSA Prime P from  DSA
protected BigInteger $q DSA Group Order q from  DSA
protected BigInteger $g DSA Group Generator G from  DSA
protected BigInteger $y DSA public key value y from  DSA
protected string $sigFormat Signature Format from  DSA
protected string $shortFormat Signature Format (Short) from  DSA
protected BigInteger $x DSA secret exponent x

Methods

__construct()

Constructor

from  DSA
static 
initialize_static_variables()

Initialize static variables

static AsymmetricKey
load(string $key, string $password = false)

Load the key

PrivateKey
loadPrivateKey(string|array $key, string $password = '')

Loads a private key

PublicKey
loadPublicKey(string|array $key)

Loads a public key

AsymmetricKey
loadParameters(string|array $key)

Loads parameters

static AsymmetricKey
loadFormat(string $type, string $key, string $password = false)

Load the key, assuming a specific format

PrivateKey
loadPrivateKeyFormat(string $type, string $key, string $password = false)

Loads a private key

PublicKey
loadPublicKeyFormat(string $type, string $key)

Loads a public key

AsymmetricKey
loadParametersFormat(string $type, string|array $key)

Loads parameters

static mixed
validatePlugin(string $format, string $type, string $method = NULL)

Validate Plugin

static array
getSupportedKeyFormats()

Returns a list of supported formats.

static bool
addFileFormat(string $fullname)

Add a fileformat plugin

mixed
getLoadedFormat()

Returns the format of the loaded key.

null|string
getComment()

Returns the key's comment

static 
useBestEngine()

Tests engine validity

static 
useInternalEngine()

Flag to use internal engine only (useful for unit testing)

string
__toString()

__toString() magic method

withHash(string $hash)

Determines which hashing function should be used

getHash()

Returns the hash algorithm currently being used

string
computek(string $h1)

Compute the pseudorandom k for signature generation, using the process specified for deterministic DSA.

bits2int(string $in)

Bit String to Integer

static DSA|bool
createParameters(int $L = 2048, int $N = 224)

Create DSA parameters

from  DSA
static PrivateKey
createKey(int[] ...$args)

Create public / private key pair.

from  DSA
static bool
onLoad(array $components)

OnLoad Handler

from  DSA
array
getLength()

Returns the key size

from  DSA
string
getEngine()

Returns the current engine being used

from  DSA
mixed
getParameters()

Returns the parameters

from  DSA
withSignatureFormat(string $format)

Determines the signature padding mode

from  DSA
getSignatureFormat()

Returns the signature format currently being used

from  DSA
withPassword(string|bool $password = false)

Sets the password

getPublicKey()

Returns the public key

sign($message)

Create a signature

toString($type, array $options = [])

Returns the private key

Details

protected __construct()

Constructor

PublicKey and PrivateKey objects can only be created from abstract RSA class

static protected initialize_static_variables()

Initialize static variables

static AsymmetricKey load(string $key, string $password = false)

Load the key

Parameters

string $key
string $password optional

Return Value

AsymmetricKey

PrivateKey loadPrivateKey(string|array $key, string $password = '')

Loads a private key

Parameters

string|array $key
string $password optional

Return Value

PrivateKey

PublicKey loadPublicKey(string|array $key)

Loads a public key

Parameters

string|array $key

Return Value

PublicKey

AsymmetricKey loadParameters(string|array $key)

Loads parameters

Parameters

string|array $key

Return Value

AsymmetricKey

static AsymmetricKey loadFormat(string $type, string $key, string $password = false)

Load the key, assuming a specific format

Parameters

string $type
string $key
string $password optional

Return Value

AsymmetricKey

PrivateKey loadPrivateKeyFormat(string $type, string $key, string $password = false)

Loads a private key

Parameters

string $type
string $key
string $password optional

Return Value

PrivateKey

PublicKey loadPublicKeyFormat(string $type, string $key)

Loads a public key

Parameters

string $type
string $key

Return Value

PublicKey

AsymmetricKey loadParametersFormat(string $type, string|array $key)

Loads parameters

Parameters

string $type
string|array $key

Return Value

AsymmetricKey

static protected mixed validatePlugin(string $format, string $type, string $method = NULL)

Validate Plugin

Parameters

string $format
string $type
string $method optional

Return Value

mixed

static array getSupportedKeyFormats()

Returns a list of supported formats.

Return Value

array

static bool addFileFormat(string $fullname)

Add a fileformat plugin

The plugin needs to either already be loaded or be auto-loadable. Loading a plugin whose shortname overwrite an existing shortname will overwrite the old plugin.

Parameters

string $fullname

Return Value

bool

See also

\self::load()

mixed getLoadedFormat()

Returns the format of the loaded key.

If the key that was loaded wasn't in a valid or if the key was auto-generated with RSA::createKey() then this will throw an exception.

Return Value

mixed

See also

\self::load()

null|string getComment()

Returns the key's comment

Not all key formats support comments. If you want to set a comment use toString()

Return Value

null|string

static useBestEngine()

Tests engine validity

static useInternalEngine()

Flag to use internal engine only (useful for unit testing)

string __toString()

__toString() magic method

Return Value

string

withHash(string $hash)

Determines which hashing function should be used

Parameters

string $hash

getHash()

Returns the hash algorithm currently being used

protected string computek(string $h1)

Compute the pseudorandom k for signature generation, using the process specified for deterministic DSA.

Parameters

string $h1

Return Value

string

protected BigInteger bits2int(string $in)

Bit String to Integer

Parameters

string $in

Return Value

BigInteger

static DSA|bool createParameters(int $L = 2048, int $N = 224)

Create DSA parameters

Parameters

int $L
int $N

Return Value

DSA|bool

static PrivateKey createKey(int[] ...$args)

Create public / private key pair.

This method is a bit polymorphic. It can take a DSA/Parameters object, L / N as two distinct parameters or no parameters (at which point L and N will be generated with this method)

Returns the private key, from which the publickey can be extracted

Parameters

int[] ...$args

Return Value

PrivateKey

static protected bool onLoad(array $components)

OnLoad Handler

Parameters

array $components

Return Value

bool

array getLength()

Returns the key size

More specifically, this L (the length of DSA Prime P) and N (the length of DSA Group Order q)

Return Value

array

string getEngine()

Returns the current engine being used

Return Value

string

See also

\self::useInternalEngine()
\self::useBestEngine()

mixed getParameters()

Returns the parameters

A public / private key is only returned if the currently loaded "key" contains an x or y value.

Return Value

mixed

See also

\self::getPublicKey()

withSignatureFormat(string $format)

Determines the signature padding mode

Valid values are: ASN1, SSH2, Raw

Parameters

string $format

getSignatureFormat()

Returns the signature format currently being used

withPassword(string|bool $password = false)

Sets the password

Private keys can be encrypted with a password. To unset the password, pass in the empty string or false. Or rather, pass in $password such that empty($password) && !is_string($password) is true.

Parameters

string|bool $password

See also

\self::createKey()
\self::load()

getPublicKey()

Returns the public key

If you do "openssl rsa -in private.rsa -pubout -outform PEM" you get a PKCS8 formatted key that contains a publicKeyAlgorithm AlgorithmIdentifier and a publicKey BIT STRING. An AlgorithmIdentifier contains an OID and a parameters field. With RSA public keys this parameters field is NULL. With DSA PKCS8 public keys it is not - it contains the p, q and g variables. The publicKey BIT STRING contains, simply, the y variable. This can be verified by getting a DSA PKCS8 public key:

"openssl dsa -in private.dsa -pubout -outform PEM"

ie. just swap out rsa with dsa in the rsa command above.

A PKCS1 public key corresponds to the publicKey portion of the PKCS8 key. In the case of RSA the publicKey portion /is/ the key. In the case of DSA it is not. You cannot verify a signature without the parameters and the PKCS1 DSA public key format does not include the parameters.

See also

\self::getPrivateKey()

sign($message)

Create a signature

Parameters

$message

See also

\self::verify()

toString($type, array $options = [])

Returns the private key

Parameters

$type
array $options