openpgp-php/lib/OpenPgP/AsymmetricSessionKeyPacket.php

60 lines
1.3 KiB
PHP
Raw Normal View History

2020-06-07 06:25:59 +00:00
<?php
namespace Leenooks\OpenPGP;
/**
* OpenPGP Public-Key Encrypted Session Key packet (tag 1).
*
* @see http://tools.ietf.org/html/rfc4880#section-5.1
*/
class AsymmetricSessionKeyPacket extends Packet
{
protected $tag = 1;
public $version,$keyid,$key_algorithm,$encrypted_data;
function __construct($key_algorithm='',$keyid='',$encrypted_data='',$version=3)
{
parent::__construct();
$this->version = $version;
$this->keyid = substr($keyid,-16);
$this->key_algorithm = $key_algorithm;
$this->encrypted_data = $encrypted_data;
}
function read()
{
switch ($this->version = ord($this->read_byte())) {
case 3:
$rawkeyid = $this->read_bytes(8);
$this->keyid = '';
// Store KeyID in Hex
for ($i=0;$i<strlen($rawkeyid);$i++) {
$this->keyid .= sprintf('%02X',ord($rawkeyid{$i}));
}
$this->key_algorithm = ord($this->read_byte());
$this->encrypted_data = $this->input;
break;
default:
throw new Exception("Unsupported AsymmetricSessionKeyPacket version: ".$this->version);
}
}
function body()
{
$bytes = chr($this->version);
for ($i=0;$i<strlen($this->keyid);$i+= 2) {
$bytes .= chr(hexdec($this->keyid{$i}.$this->keyid{$i+1}));
}
$bytes .= chr($this->key_algorithm);
$bytes .= $this->encrypted_data;
return $bytes;
}
}