function sign($data, $privateKeyRaw = NULL) { if ($privateKeyRaw == null) { global $thisEntity; // Load the private key if needed (from a hex string): if (!isset($thisEntity['PrivateKeyBytes'])) { // Load it now: $thisEntity['PrivateKeyBytes'] = hex2bin($thisEntity['PrivateKey']); } // Grab the raw private key (bytes): $privateKeyRaw = $thisEntity['PrivateKeyBytes']; } // Get the double hash of the data: $msg32 = hash('sha256', hash('sha256', $data, true), true); // Create a context: $ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); // Sign: $signature; if (secp256k1_ecdsa_sign($ctx, $msg32, $privateKeyRaw, $signature) != 1) { // This is a 500 error. Unable to sign. serverError(); } // Serialize the signature: $serialized = ''; secp256k1_ecdsa_signature_serialize_der($ctx, $signature, $serialized); return $serialized; }
/** * @param PrivateKeyInterface $privateKey * @param Buffer $messageHash * @param RbgInterface $rbgInterface * @return Signature * @throws \Exception */ public function sign(Buffer $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbgInterface = null) { $sigStr = ''; $ret = \secp256k1_ecdsa_sign($messageHash->getBinary(), $privateKey->getBuffer()->getBinary(), $sigStr); if ($ret !== 1) { throw new \Exception('Secp256k1-php failed to sign data'); } return SignatureFactory::fromHex(bin2hex($sigStr)); }
/** * @param BufferInterface $msg32 * @param PrivateKey $privateKey * @return Signature */ private function doSign(BufferInterface $msg32, PrivateKey $privateKey) { /** @var resource $sig_t */ $sig_t = ''; if (1 !== secp256k1_ecdsa_sign($this->context, $msg32->getBinary(), $privateKey->getBinary(), $sig_t)) { throw new \RuntimeException('Secp256k1: failed to sign'); } $derSig = ''; secp256k1_ecdsa_signature_serialize_der($this->context, $sig_t, $derSig); $rL = ord($derSig[3]); $r = (new Buffer(substr($derSig, 4, $rL), $rL, $this->math))->getInt(); $sL = ord($derSig[4 + $rL + 1]); $s = (new Buffer(substr($derSig, 4 + $rL + 2, $sL), $rL, $this->math))->getInt(); return new Signature($this, $r, $s, $sig_t); }
/** * @dataProvider getErroneousTypeVectors * @expectedException \PHPUnit_Framework_Error_Warning */ public function testErroneousTypes($context, $msg32, $private) { $sig = ''; \secp256k1_ecdsa_sign($context, $sig, $msg32, $private); }