<?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]; $string = "This\nis\na\ntest."; /* Create a new literal data packet */ $data = new OpenPGP_LiteralDataPacket($string, array('format' => 'u', 'filename' => 'stuff.txt')); $data->normalize(true); // Clearsign-style normalization of the LiteralDataPacket /* Create a signer from the key */ $sign = new OpenPGP_Crypt_RSA($wkey); /* The message is the signed data packet */ $m = $sign->sign($data); /* Generate clearsigned data */ $packets = $m->signatures()[0]; echo "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\n"; // Output normalised data. You could convert line endings here // without breaking the signature, but do not add any // trailing whitespace to lines. echo preg_replace("/^-/", "- -", $packets[0]->data) . "\n"; echo OpenPGP::enarmor($packets[1][0]->to_bytes(), "PGP SIGNATURE");
/** */ public function __toString() { $bytes = $this->message->to_bytes(); if (!strlen($this->_armor)) { return $bytes; } return OpenPGP::enarmor($bytes, 'PGP ' . $this->_armor, array_merge($this->headers, array('Version' => $this->armorVersion))); }
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(); }