public function pemDecode($pem_data)
{
$beg_ec_text = '-----BEGIN EC PRIVATE KEY-----';
$end_ec_text = '-----END EC PRIVATE KEY-----';
$decoded = '';
$ecpemstruct = array();
$pem_data = str_ireplace($beg_ec_text, '', $pem_data);
$pem_data = str_ireplace($end_ec_text, '', $pem_data);
$pem_data = str_ireplace("\r", '', trim($pem_data));
$pem_data = str_ireplace("\n", '', trim($pem_data));
$pem_data = str_ireplace(' ', '', trim($pem_data));
$decoded = bin2hex(base64_decode($pem_data));
if (strlen($decoded) < 230) {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$ecpemstruct = array('oct_sec_val' => substr($decoded, 14, 64), 'obj_id_val' => substr($decoded, 86, 10), 'bit_str_val' => substr($decoded, 106));
if ($ecpemstruct['obj_id_val'] != '2b8104000a') {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$private_key = $ecpemstruct['oct_sec_val'];
$public_key = $ecpemstruct['bit_str_val'];
if (strlen($private_key) < 64 || strlen($public_key) < 128) {
throw new \Exception('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
}
$this->pemDecoded = array('private_key' => $private_key, 'public_key' => $public_key);
return $this->pemDecoded;
}