You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
namespace HydraterLicense;
|
|
|
|
class KeyGenerator
|
|
{
|
|
/**
|
|
* Generates an RSA private key and saves it to a file.
|
|
*
|
|
* @param string path Output path for private key (e.g., "private.pem")
|
|
* @param int bits RSA key size (default: 2048)
|
|
* @return bool
|
|
*/
|
|
public function generatePrivateKey(string path, int bits = 2048) -> bool
|
|
{
|
|
|
|
if unlikely !extension_loaded("openssl") {
|
|
echo "OpenSSL extension not loaded";
|
|
return false;
|
|
}
|
|
|
|
var config, privateKey, exported, result;
|
|
|
|
let config = [
|
|
"private_key_type": OPENSSL_KEYTYPE_RSA,
|
|
"private_key_bits": bits,
|
|
"digest_alg": "sha256"
|
|
];
|
|
|
|
let privateKey = openssl_pkey_new(config);
|
|
|
|
let exported = "";
|
|
if unlikely !openssl_pkey_export(privateKey, exported) {
|
|
echo "Failed to export pkey";
|
|
return false;
|
|
}
|
|
|
|
let result = file_put_contents(path, exported);
|
|
if !result {
|
|
echo "Unable to save";
|
|
}
|
|
return result !== false;
|
|
}
|
|
|
|
/**
|
|
* Extracts public key from private key and saves it to file.
|
|
*
|
|
* @param string privatePath Path to private.pem
|
|
* @param string publicPath Output path for public.pem
|
|
* @return bool
|
|
*/
|
|
public function generatePublicKey(string privatePath, string publicPath) -> bool
|
|
{
|
|
var privatePem, privateKey, pubKeyDetails, pubKey, written;
|
|
|
|
let privatePem = file_get_contents(privatePath);
|
|
if privatePem === false {
|
|
return false;
|
|
}
|
|
|
|
let privateKey = openssl_pkey_get_private(privatePem);
|
|
|
|
let pubKeyDetails = openssl_pkey_get_details(privateKey);
|
|
if typeof pubKeyDetails !== "array" || !isset pubKeyDetails["key"] {
|
|
return false;
|
|
}
|
|
|
|
let pubKey = pubKeyDetails["key"];
|
|
let written = file_put_contents(publicPath, pubKey);
|
|
return written !== false;
|
|
}
|
|
}
|
|
|
|
|