function body() { $bytes = parent::body() . chr($this->s2k_useage); $secret_material = NULL; if ($this->s2k_useage == 255 || $this->s2k_useage == 254) { $bytes .= chr($this->symmetric_algorithm); $bytes .= $this->s2k->to_bytes(); } if ($this->s2k_useage > 0) { $bytes .= $this->encrypted_data; } else { $secret_material = ''; foreach (self::$secret_key_fields[$this->algorithm] as $f) { $f = $this->key[$f]; $secret_material .= pack('n', \OpenPGP\Util::bitlength($f)); $secret_material .= $f; } $bytes .= $secret_material; // 2-octet checksum $chk = 0; for ($i = 0; $i < strlen($secret_material); $i++) { $chk = ($chk + ord($secret_material[$i])) % 65536; } $bytes .= pack('n', $chk); } return $bytes; }
function to_bytes() { $bytes = chr($this->type); switch ($this->type) { case 0: $bytes .= chr($this->hash_algorithm); break; case 1: $bytes .= chr($this->hash_algorithm); $bytes .= $this->salt; break; case 3: $bytes .= chr($this->hash_algorithm); $bytes .= $this->salt; $bytes .= chr(\OpenPGP\Util::encode_s2k_count($this->count)); break; } return $bytes; }
static function encode_s2k_count($iterations) { if ($iterations >= 65011712) { return 255; } $count = $iterations >> 6; $c = 0; while ($count >= 32) { $count = $count >> 1; $c++; } $result = $c << 4 | $count - 16; if (\OpenPGP\Util::decode_s2k_count($result) < $iterations) { return $result + 1; } return $result; }
function fingerprint_material() { switch ($this->version) { case 3: $material = array(); foreach (self::$key_fields[$this->algorithm] as $i) { $material[] = pack('n', \OpenPGP\Util::bitlength($this->key[$i])); $material[] = $this->key[$i]; } return $material; case 4: $head = array(chr(0x99), NULL, chr($this->version), pack('N', $this->timestamp), chr($this->algorithm)); $material = array(); foreach (self::$key_fields[$this->algorithm] as $i) { $material[] = pack('n', \OpenPGP\Util::bitlength($this->key[$i])); $material[] = $this->key[$i]; } $material = implode('', $material); $head[1] = pack('n', 6 + strlen($material)); $head[] = $material; return $head; } }
function body() { switch ($this->version) { case 2: case 3: $body = chr($this->version) . chr(5) . chr($this->signature_type); foreach ((array) $this->unhashed_subpackets as $p) { if ($p instanceof \OpenPGP\Packets\Signatures\CreationTimePacket) { $body .= pack('N', $p->data); break; } } foreach ((array) $this->unhashed_subpackets as $p) { if ($p instanceof \OpenPGP\Packets\Signatures\IssuerPacket) { for ($i = 0; $i < strlen($p->data); $i += 2) { $body .= chr(hexdec($p->data[$i] . $p->data[$i + 1])); } break; } } $body .= chr($this->key_algorithm); $body .= chr($this->hash_algorithm); $body .= pack('n', $this->hash_head); foreach ($this->data as $mpi) { $body .= pack('n', \OpenPGP\Util::bitlength($mpi)) . $mpi; } return $body; case 4: if (!$this->trailer) { $this->trailer = $this->calculate_trailer(); } $body = substr($this->trailer, 0, -6); $unhashed_subpackets = ''; foreach ((array) $this->unhashed_subpackets as $p) { $unhashed_subpackets .= $p->to_bytes(); } $body .= pack('n', strlen($unhashed_subpackets)) . $unhashed_subpackets; $body .= pack('n', $this->hash_head); foreach ((array) $this->data as $mpi) { $body .= pack('n', \OpenPGP\Util::bitlength($mpi)) . $mpi; } return $body; } }