public function hashcashMint($bits = null) { $stamp = null; if ($bits === null) { $bits = static::HASHCASH_BITS_MIN; } if ($this->getLocalNode()) { $hashcash = new Hashcash($bits, $this->getLocalNode()->getIdHexStr()); $hashcash->setDate(date(Hashcash::DATE_FORMAT12)); try { $stamp = $hashcash->mint(); } catch (Exception $e) { $this->log('error', $e->getMessage()); } // @codeCoverageIgnoreEnd } return $stamp; }
// 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"; }
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'); }
/** * @expectedException RuntimeException */ public function testMintAttemptsMaxRuntimeException() { $hashcash = new Hashcash(5, '*****@*****.**'); $hashcash->setDate('140427'); $hashcash->setSalt('axfcrlV1hxLvF6J9BeDiLw=='); $hashcash->setMintAttemptsMax(1); $hashcash->mint(); }