OpenPGP_Crypt_RSA::sign_key_userid PHP Method

sign_key_userid() public method

TODO: merge this with the normal sign function
public sign_key_userid ( $packet, $hash = 'SHA256', $keyid = NULL )
    function sign_key_userid($packet, $hash = 'SHA256', $keyid = NULL)
    {
        if (is_array($packet)) {
            $packet = new OpenPGP_Message($packet);
        } else {
            if (!is_object($packet)) {
                $packet = OpenPGP_Message::parse($packet);
            }
        }
        $key = $this->private_key($keyid);
        if (!$key || !$packet) {
            return NULL;
        }
        // Missing some data
        if (!$keyid) {
            $keyid = substr($this->key->fingerprint, -16);
        }
        $key->setHash(strtolower($hash));
        $sig = NULL;
        foreach ($packet as $p) {
            if ($p instanceof OpenPGP_SignaturePacket) {
                $sig = $p;
            }
        }
        if (!$sig) {
            $sig = new OpenPGP_SignaturePacket($packet, 'RSA', strtoupper($hash));
            $sig->signature_type = 0x13;
            $sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_KeyFlagsPacket(array(0x1, 0x2));
            $sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket($keyid);
            $packet[] = $sig;
        }
        $sig->sign_data(array('RSA' => array($hash => function ($data) use($key) {
            return array($key->sign($data));
        })));
        return $packet;
    }

Usage Example

Example #1
0
<?php

require_once dirname(__FILE__) . '/../lib/openpgp.php';
require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php';
$rsa = new Crypt_RSA();
$k = $rsa->createKey(512);
$rsa->loadKey($k['privatekey']);
$nkey = new OpenPGP_SecretKeyPacket(array('n' => $rsa->modulus->toBytes(), 'e' => $rsa->publicExponent->toBytes(), 'd' => $rsa->exponent->toBytes(), 'p' => $rsa->primes[1]->toBytes(), 'q' => $rsa->primes[2]->toBytes(), 'u' => $rsa->coefficients[2]->toBytes()));
$uid = new OpenPGP_UserIDPacket('Test <*****@*****.**>');
$wkey = new OpenPGP_Crypt_RSA($nkey);
$m = $wkey->sign_key_userid(array($nkey, $uid));
// Serialize private key
print $m->to_bytes();
// Serialize public key message
$pubm = clone $m;
$pubm[0] = new OpenPGP_PublicKeyPacket($pubm[0]);
$public_bytes = $pubm->to_bytes();