public function getPrivate() { do { $key = \openssl_random_pseudo_bytes(32); } while (secp256k1_ec_seckey_verify(self::getContext(), $key) == 0); return $key; }
function generateKeyPair() { // Create a context: $ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); do { // Generate random str: $privkey = mcrypt_create_iv(32, \MCRYPT_DEV_URANDOM); // Attempt to verify that it's a valid private key: } while (!(bool) secp256k1_ec_seckey_verify($ctx, $privkey)); $pubkey = null; $pubkeyRef = null; // Create the public key (note: For additional safety, check this equals 1): secp256k1_ec_pubkey_create($ctx, $privkey, $pubkeyRef); // Serialise it: secp256k1_ec_pubkey_serialize($ctx, $pubkeyRef, false, $pubkey); // Done: return array('private' => $privkey, 'public' => $pubkey); }
/** * @param Buffer $privateKey * @return bool */ public function validatePrivateKey(Buffer $privateKey) { $ret = (bool) \secp256k1_ec_seckey_verify($privateKey->getBinary()); return $ret; }
/** * @param BufferInterface $privateKey * @return bool */ public function validatePrivateKey(BufferInterface $privateKey) { return (bool) secp256k1_ec_seckey_verify($this->context, $privateKey->getBinary()); }