public function load() { #fwrite(STDOUT, __CLASS__.'->'.__FUNCTION__.''."\n"); if (parent::load()) { if (isset($this->data['hashcashs']) && $this->data['hashcashs']) { foreach ($this->data['hashcashs'] as $hashcashId => $hashcashAr) { $this->hashcashsId = $hashcashId; #fwrite(STDOUT, __CLASS__.'->'.__FUNCTION__.': '.$this->hashcashsId."\n"); $hashcash = new Hashcash(); if ($hashcash->verify($hashcashAr['stamp'])) { $this->hashcashs[$hashcashId] = $hashcash; } } } unset($this->data['hashcashs']); return true; } return false; }
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; }
{ global $exit; $exit = true; } $sig = pcntl_signal(SIGALRM, 'sigNext'); print 'signal setup: ' . ($sig ? 'ok' : 'failed') . "\n"; $sig = pcntl_signal(SIGINT, 'sigExit'); print 'signal setup: ' . ($sig ? 'ok' : 'failed') . "\n"; $sig = pcntl_signal(SIGTERM, 'sigExit'); print 'signal setup: ' . ($sig ? 'ok' : 'failed') . "\n"; fwrite(STDOUT, 'bits: ' . BITS . "\n"); fwrite(STDOUT, 'tests: ' . TESTS . "\n"); $diffMin = TIME_MAX; $diffMax = 0; for ($testno = 1; $testno <= TESTS && !$exit; $testno++) { $hashcash = new Hashcash(BITS, '*****@*****.**'); $stamp = ''; $start = time(); fwrite(STDOUT, 'mint ' . $testno . '/' . TESTS . ': '); pcntl_alarm(TIME_MAX); $stamp = $hashcash->mint(); pcntl_alarm(null); $diff = time() - $start; if ($diff > $diffMax) { $diffMax = $diff; } if ($diff < $diffMin) { $diffMin = $diff; } fwrite(STDOUT, $diff . ' sec "' . $stamp . '" ' . $hashcash->getAttempts() . "\n"); $times[] = $diff;
public function testDoublespend2() { #fwrite(STDOUT, __METHOD__."\n"); $db = new HashcashDb(); $hashcash = new Hashcash(); $hashcash->setVersion(1); $hashcash->setBits(5); $hashcash->setResource('thefox'); $hashcash->setSuffix(1); $this->assertTrue($db->addHashcash($hashcash)); $hashcash = new Hashcash(); $hashcash->setVersion(1); $hashcash->setBits(5); $hashcash->setResource('thefox'); $hashcash->setSuffix(1); $this->assertFalse($db->addHashcash($hashcash)); $this->assertEquals(1, count($db->getHashcashs())); }
// 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"; } catch (Exception $e) { print 'ERROR 8: ' . $e->getMessage() . "\n"; } // Example 6: use infinite attempts $hashcash = new Hashcash(19, '*****@*****.**'); $hashcash->setDate('140427'); $hashcash->setSalt('axfcrlV1hxLvF6J9BeDiLw=='); $hashcash->setMintAttemptsMax(0); // Use infinite attempts $stamp = ''; try { $stamp = $hashcash->mint(); print "stamp6: '" . $stamp . "'\n"; } catch (Exception $e) { print 'ERROR 9: ' . $e->getMessage() . "\n"; }
/** * @expectedException InvalidArgumentException * @expectedExceptionCode 2 */ public function testVerifyInvalidArgumentException2() { #$this->assertTrue(true); $hashcash = new Hashcash(); $hashcash->verify('1:20:140422:mint2:ext2:22060'); }