Exemple #1
0
 public function hashcashVerify($hashcashStr, $resource, $bits = null)
 {
     #$this->log('debug', 'hashcash: '.$hashcashStr);
     if ($bits === null) {
         $bits = static::HASHCASH_BITS_MIN;
     }
     $hashcash = new Hashcash();
     $hashcash->setExpiration(static::HASHCASH_EXPIRATION);
     try {
         if ($hashcash->verify($hashcashStr)) {
             #$this->log('debug', 'bits: '.$hashcash->getBits());
             $added = false;
             if ($hashcash->getVersion() >= 1 && $hashcash->getBits() >= $bits && $hashcash->getResource() == $resource && ($added = $this->getHashcashDb()->addHashcash($hashcash))) {
                 #$this->log('debug', 'hashcash: OK');
                 return true;
             } else {
                 $this->log('error', 'hashcash verification failed');
                 $this->log('error', 'hashcash version: ' . $hashcash->getVersion());
                 $this->log('error', 'hashcash bit: ' . $hashcash->getBits() . ' (min: ' . $bits . ')');
                 $this->log('error', 'hashcash resource: ' . $hashcash->getResource() . ' (' . $resource . ')');
                 $this->log('error', 'hashcash added: ' . ($added ? 'yes' : 'no'));
             }
         }
     } catch (Exception $e) {
         $this->log('warning', $e->getMessage());
     }
     // @codeCoverageIgnoreEnd
     $this->log('debug', 'hashcash: ' . $hashcashStr . ' failed');
     return false;
 }
Exemple #2
0
} catch (Exception $e) {
    print 'ERROR 5: ' . $e->getMessage() . "\n";
}
// Example 4a: use a certain date
$hashcash = new Hashcash(20, '*****@*****.**');
$hashcash->setDate('870221');
$stamp = '';
try {
    $stamp = $hashcash->mint();
    print "stamp4: '" . $stamp . "'\n";
} catch (Exception $e) {
    print 'ERROR 6: ' . $e->getMessage() . "\n";
}
// Example 4a: verify a stamp, ignore expiration
$hashcash = new Hashcash();
$hashcash->setExpiration(0);
// Never expire.
try {
    print "stamp4 verify: '" . ($hashcash->verify($stamp) ? 'Ok' : 'failed') . "'\n";
} catch (Exception $e) {
    print 'ERROR 7: ' . $e->getMessage() . "\n";
}
// Example 5: use 1 attempt, must fail with this configuration
$hashcash = new Hashcash(19, '*****@*****.**');
$hashcash->setDate('140427');
$hashcash->setSalt('axfcrlV1hxLvF6J9BeDiLw==');
$hashcash->setMintAttemptsMax(1);
$stamp = '';
try {
    $stamp = $hashcash->mint();
    print "stamp5: '" . $stamp . "'\n";
Exemple #3
0
 public function testSave3()
 {
     #fwrite(STDOUT, __METHOD__.''."\n");
     $db = new HashcashDb('test_data/test_hashcashs3.yml');
     $ts = time();
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setResource('res1');
     $hashcash->mint();
     $this->assertTrue($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setDate(date(Hashcash::DATE_FORMAT, $ts - 3600 * 24 * 90));
     $hashcash->setResource('res2');
     $hashcash->mint();
     $this->assertFalse($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setDate(date(Hashcash::DATE_FORMAT, $ts - 3600 * 24 * 90));
     $hashcash->setResource('res3');
     $hashcash->setExpiration(3600 * 24 * 120);
     $hashcash->mint();
     $this->assertTrue($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setDate(date(Hashcash::DATE_FORMAT, $ts - 3600 * 24 * 10));
     $hashcash->setResource('res4');
     $hashcash->mint();
     $this->assertTrue($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setDate(date('ymdHis', $ts - 60));
     $hashcash->setResource('res5');
     $hashcash->setExpiration(30);
     $hashcash->mint();
     $this->assertFalse($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     $hashcash = new Hashcash();
     $hashcash->setVersion(1);
     $hashcash->setBits(5);
     $hashcash->setDate(date(Hashcash::DATE_FORMAT12, $ts - 60));
     $hashcash->setResource('res6');
     $hashcash->mint();
     $this->assertTrue($hashcash->verify());
     $this->assertTrue($db->addHashcash($hashcash));
     #fwrite(STDOUT, __METHOD__.': save '.$db->save()."\n");
     $this->assertTrue($db->save() > 0);
     $this->assertFileExists('test_data/test_hashcashs3.yml');
 }
Exemple #4
0
 public function testVerify()
 {
     #$this->markTestIncomplete('This test has not been implemented yet.');
     #$this->assertTrue(true); return;
     $hashcash = new Hashcash();
     $hashcash->setExpiration(0);
     $this->assertTrue($hashcash->verify('1:20:140422:mint2::ArrRIabEj3nZrOcM:0000000000007u1E'));
     $this->assertTrue($hashcash->verify('1:24:140422:mint2:ext1:Nde2ffWsRoe3DXVQ:00000001M+iu'));
     $this->assertTrue($hashcash->verify('1:20:140422:mint2:ext2:salt2:256507'));
     $this->assertTrue($hashcash->verify('1:28:140422:::s15xXleWocBKSA95Zw4e1Q==:245861178'));
     $this->assertTrue($hashcash->verify('1:21:870221:thefox::2B6kv/rFiCdJRzqhH7P2eA==:995214'));
     $this->assertFalse($hashcash->verify('1:20:140422:mint3::ArrRIabEj3nZrOcM:0000000000007u1E'));
     $hashcash->setExpiration(3600 * 24 * 365);
     $this->assertFalse($hashcash->verify('1:21:870221:thefox::2B6kv/rFiCdJRzqhH7P2eA==:995214'));
     $hashcash1 = new Hashcash();
     $hashcash1->setBits(10);
     $hashcash2 = new Hashcash();
     $this->assertTrue($hashcash2->verify($hashcash1->mint()));
 }