Exemplo n.º 1
0
 public function encrypt()
 {
     #print __CLASS__.'->'.__FUNCTION__.''."\n";
     #print __CLASS__.'->'.__FUNCTION__.': "'.$this->getPassword().'"'."\n";
     $rv = false;
     if (!$this->getSsl()) {
         throw new RuntimeException('ssl not set.', 1);
     }
     if (!$this->getDstSslPubKey()) {
         throw new RuntimeException('dstSslPubKey not set.', 2);
     }
     $text = $this->getText();
     $password = base64_encode(Rand::data(256));
     $passwordEncrypted = '';
     $signAlgo = OPENSSL_ALGO_SHA1;
     #fwrite(STDOUT, 'password: '******'password' => $passwordBase64, 'sign' => $sign, 'signAlgo' => $signAlgo));
             $gzdata = gzencode($jsonStr, 9);
             $passwordEncrypted = base64_encode($gzdata);
             $this->setPassword($passwordEncrypted);
         } else {
             throw new RuntimeException('openssl_public_encrypt failed: "' . openssl_error_string() . '"', 101);
         }
     } else {
         throw new RuntimeException('openssl_sign failed.', 102);
     }
     // @codeCoverageIgnoreEnd
     if ($passwordEncrypted) {
         $signRv = openssl_sign($text, $sign, $this->getSsl(), $signAlgo);
         if ($signRv) {
             $sign = base64_encode($sign);
             $subjectBase64 = base64_encode($this->getSubject());
             $textBase64 = base64_encode($text);
             $srcUserNickname = base64_encode($this->getSrcUserNickname());
             $jsonStr = json_encode(array('subject' => $subjectBase64, 'text' => $textBase64, 'sign' => $sign, 'signAlgo' => $signAlgo, 'srcUserNickname' => $srcUserNickname, 'ignore' => $this->getIgnore()));
             $data = gzencode($jsonStr, 9);
             $iv = substr(hash('sha512', mt_rand(0, 999999), true), 0, 16);
             $data = openssl_encrypt($data, 'AES-256-CBC', $password, 0, $iv);
             if ($data !== false) {
                 $iv = base64_encode($iv);
                 $jsonStr = json_encode(array('data' => $data, 'iv' => $iv));
                 $data = gzencode($jsonStr, 9);
                 $data = base64_encode($data);
                 $this->setBody($data);
                 $checksum = $this->createCheckSum($this->getVersion(), $this->getId(), $this->getSrcNodeId(), $this->getDstNodeId(), $this->getDstSslPubKey(), $text, $this->getTimeCreated(), $password);
                 $this->setChecksum($checksum);
                 #fwrite(STDOUT, 'checksum: /'.$checksum.'/'."\n");
                 #fwrite(STDOUT, 'version: /'.$this->getVersion().'/'."\n");
                 #fwrite(STDOUT, 'id: /'.$this->getId().'/'."\n");
                 #fwrite(STDOUT, 'src node id: /'.$this->getSrcNodeId().'/'."\n");
                 #fwrite(STDOUT, 'dst node id: /'.$this->getDstNodeId().'/'."\n");
                 #fwrite(STDOUT, 'dst ssl pub key: /'.$this->getDstSslPubKey().'/'."\n");
                 #fwrite(STDOUT, 'subject: /'.$this->getSubject().'/'."\n");
                 #fwrite(STDOUT, 'text: /'.$text.'/'."\n");
                 #fwrite(STDOUT, 'time created: /'.$this->getTimeCreated().'/'."\n");
                 #fwrite(STDOUT, 'password: /'.$password.'/'."\n");
                 $rv = true;
             }
         }
     } else {
         // @codeCoverageIgnoreStart
         throw new RuntimeException('Can\'t create password.', 103);
         // @codeCoverageIgnoreEnd
     }
     return $rv;
 }
Exemplo n.º 2
0
 public function mintAll()
 {
     #fwrite(STDOUT, __METHOD__.': '.$this->getBits()."\n");
     $stamps = array();
     $rounds = pow(2, $this->getBits());
     $bytes = $this->getBits() / 8 + (8 - $this->getBits() % 8) / 8;
     $salt = $this->getSalt();
     $baseStamp = $this->getVersion() . ':' . $this->getBits();
     $baseStamp .= ':' . $this->getDate();
     $baseStamp .= ':' . $this->getResource() . ':' . $this->getExtension() . ':' . $salt . ':';
     #fwrite(STDOUT, __METHOD__.': '.$this->getBits().', '.$bytes."\n");
     if (!$salt) {
         $salt = base64_encode(Rand::data(16));
     }
     #fwrite(STDOUT, 'bits: '.$this->getBits()."\n");
     #fwrite(STDOUT, "\t".' rounds: '.$rounds."\n");
     #fwrite(STDOUT, "\t".' baseStamp: '.$baseStamp."\n");
     for ($round = 0; $round < $rounds; $round++) {
         $testStamp = $baseStamp . $round;
         $found = $this->checkBitsFast(substr(hash('sha1', $testStamp, true), 0, $bytes), $bytes, $this->getBits());
         #$percent = sprintf('%.4f', $round / $rounds * 100).' %';
         #if($round % 1000000 == 0 || $found)
         #if($found)
         #fwrite(STDOUT, "\t".' round '.$round.' '.$percent.' - '.hash('sha1', $testStamp)."\n");
         if ($found) {
             $stamps[] = $testStamp;
         }
     }
     return $stamps;
 }
Exemplo n.º 3
0
 public function testData()
 {
     $this->assertEquals(21, strlen(Rand::data(21)));
 }