private function removeIn(RedBlackTree $t, $key)
 {
     if ($this->compare($key, $t->key()) < 0) {
         if (!$this->isRed($t->left()) && !$this->isRed($t->left()->left())) {
             $t = $this->moveRedLeft($t);
         }
         $t = new self($t->key(), $t->value(), $t->left()->remove($key), $t->right(), $t->color(), $t->isRoot());
     } else {
         if ($this->isRed($t->left())) {
             $t = $this->rotateRight($t);
         }
         if ($this->compare($key, $t->key()) === 0 && $t->right()->isEmpty()) {
             return self::createEmpty();
         }
         if (!$this->isRed($t->right()) && !$this->isRed($t->right()->left())) {
             $t = $this->moveRedRight($t);
         }
         if ($this->compare($key, $t->key()) === 0) {
             $min = $this->minIn($t->right());
             $t = new self($min->key(), $min->value(), $t->left(), $this->removeMinIn($t->right()), $t->color(), $t->isRoot());
         } else {
             $t = new self($t->key(), $t->value(), $t->left(), $t->right()->remove($key), $t->color(), $t->isRoot());
         }
     }
     return $this->balance($t);
 }
 function sign($packet, $hash = 'SHA256', $keyid = NULL)
 {
     if (!is_object($packet)) {
         if ($this->key) {
             $packet = new OpenPGP_LiteralDataPacket($packet);
         } else {
             $packet = OpenPGP_Message::parse($packet);
         }
     }
     if ($packet instanceof OpenPGP_SecretKeyPacket || $packet instanceof Crypt_RSA || $packet instanceof ArrayAccess && $packet[0] instanceof OpenPGP_SecretKeyPacket) {
         $key = $packet;
         $message = $this->message;
     } else {
         $key = $this->key;
         $message = $packet;
     }
     if (!$key || !$message) {
         return NULL;
     }
     // Missing some data
     if ($message instanceof OpenPGP_Message) {
         $sign = $message->signatures();
         $message = $sign[0][0];
     }
     if (!$key instanceof Crypt_RSA) {
         $key = new self($key);
         if (!$keyid) {
             $keyid = substr($key->key()->fingerprint, -16, 16);
         }
         $key = $key->private_key($keyid);
     }
     $key->setHash(strtolower($hash));
     $sig = new OpenPGP_SignaturePacket($message, 'RSA', strtoupper($hash));
     $sig->hashed_subpackets[] = new OpenPGP_SignaturePacket_IssuerPacket($keyid);
     $sig->sign_data(array('RSA' => array($hash => function ($data) use($key) {
         return array($key->sign($data));
     })));
     return new OpenPGP_Message(array($sig, $message));
 }
 public static function map($filename, $callback, $option = null)
 {
     $it = new self($filename, $option);
     while ($it->valid()) {
         $key = $it->key();
         $value = $it->current();
         if (is_callable($callback)) {
             $ret = call_user_func($callback, $key, $value);
             if (!$ret) {
                 break;
             }
         }
         $it->next();
     }
     //$it->__destruct();
     unset($it);
 }