public function testSigningMessages() { $wkey = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/helloKey.gpg')); $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); $sign = new OpenPGP_Crypt_RSA($wkey); $m = $sign->sign($data)->to_bytes(); $reparsedM = OpenPGP_Message::parse($m); $this->assertSame($sign->verify($reparsedM), $reparsedM->signatures()); }
<?php require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; /* Parse secret key from STDIN, the key must not be password protected */ $wkey = OpenPGP_Message::parse(file_get_contents('php://stdin')); $wkey = $wkey[0]; /* Create a new literal data packet */ $data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt')); /* Create a signer from the key */ $sign = new OpenPGP_Crypt_RSA($wkey); /* The message is the signed data packet */ $m = $sign->sign($data); /* Output the raw message bytes to STDOUT */ echo $m->to_bytes();
/** */ public function sign($text, $key, $mode, $opts = array()) { $rsa = new OpenPGP_Crypt_RSA($key->message); $pkey = $rsa->key(); $text = $this->_getMessageOb($text)->packets[0]; switch ($pkey->algorithm) { case 1: case 2: case 3: // RSA $hash = $opts['sign_hash'] ?: 'SHA256'; $result = $rsa->sign($text, $hash); break; case 17: // DSA; use SHA1 by default, since that is what DSA/DSS was // designed for. $hash = $opts['sign_hash'] ?: 'SHA1'; $sig = new OpenPGP_SignaturePacket($text, 'DSA', $hash); $sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket(substr($pkey->fingerprint, -16)); $dsa = new Horde_Pgp_Crypt_DSA($pkey); $sig->sign_data(array('DSA' => array($hash => function ($data) use($dsa, $hash) { return $dsa->sign($data, $hash); }))); $result = new OpenPGP_Message(array($sig, $text)); break; } switch ($mode) { case 'clear': $sm = new Horde_Pgp_Element_SignedMessage(new OpenPGP_Message(array($result[1], $result[0]))); $sm->headers['Hash'] = $hash; return $sm; case 'detach': foreach ($result as $val) { if ($val instanceof OpenPGP_SignaturePacket) { return new Horde_Pgp_Element_Signature(new OpenPGP_Message(array($val))); } } break; case 'message': return new Horde_Pgp_Element_Message($this->_compressMessageOb($result, $opts['compress'])); } }
public function signDocument() { $document = new \StdClass(); foreach ($this->resourceData as $term => $value) { $document->{$term} = $this->normalizeData($value); } unset($document->digital_signature); unset($document->_id); unset($document->_rev); unset($document->doc_id); unset($document->publishing_node); unset($document->update_timestamp); unset($document->node_timestamp); unset($document->create_timestamp); $bencoder = new \LearningRegistry\Bencode\LearningRegistryBencodeEncoderTrial(); $document = (array) $document; $bencodedDocument = utf8_encode($bencoder->encode($document)); $hashedDocument = hash('SHA256', $bencodedDocument); global $loader; if (!isset($loader)) { $loader = $this->getLoader(); } spl_autoload_unregister(array($loader, 'loadClass')); require_once dirname(__FILE__) . '/../OpenPGP/openpgp.php'; require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_rsa.php'; require_once dirname(__FILE__) . '/../OpenPGP/openpgp_crypt_symmetric.php'; $keyASCII = $this->getKey(); $keyEncrypted = \OpenPGP_Message::parse(\OpenPGP::unarmor($keyASCII, 'PGP PRIVATE KEY BLOCK')); foreach ($keyEncrypted as $p) { if (!$p instanceof \OpenPGP_SecretKeyPacket) { continue; } $key = \OpenPGP_Crypt_Symmetric::decryptSecretKey($this->getPassPhrase(), $p); } $data = new \OpenPGP_LiteralDataPacket($hashedDocument, array('format' => 'u')); $sign = new \OpenPGP_Crypt_RSA($key); $m = $sign->sign($data); $packets = $m->signatures()[0]; $message = "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\n"; $message .= $packets[0]->data . "\n"; $message .= "-----BEGIN PGP SIGNATURE-----\n\n"; $signed_data = str_replace("-----BEGIN -----", "", str_replace("-----END -----", "", \OpenPGP::enarmor($packets[1][0]->to_bytes(), ""))); $signature = str_split(trim($signed_data), 65); foreach ($signature as $line) { $message .= $line . "\n"; } $message .= "-----END PGP SIGNATURE-----\n"; $this->setSigFields(array('signature' => $message, 'key_owner' => $this->getKeyOwner(), 'key_location' => array($this->getPublicKeyPath()), 'signing_method' => "LR-PGP.1.0")); spl_autoload_register(array($loader, 'loadClass')); $this->document = $this->createDocument(); }