/** * Test to make sure that binary garble won't mess up the hash. */ public function testCrapHashData() { $fp = fopen('/dev/urandom', 'rb'); if ($fp !== FALSE) { $binary = fread($fp, 16); fclose($fp); } // Generate a nonce with this data $key = NonceModel::Generate('10 minutes', $binary); $this->assertTrue($key != '' && strlen($key) >= 40); // And retrieve it to check. $nonce = new NonceModel($key); // The data in should NOT be the data out $this->assertFalse($nonce->get('hash') == $binary); // But it *should* match validation still. $this->assertTrue($nonce->isValid($binary)); }
/** * Shorthand function to generate and save a valid Nonce key. * * @param string $expires A human-readable string of an expire time in the future, ie: 2 seconds, 10 days, etc. * @param string|null|mixed $hash Can be used to validate this nonce from an external verification check. * @param mixed $data Any data that is needed to be stored along with this nonce. * @return string */ public static function Generate($expires = '2 hours', $hash = null, $data = null) { $nonce = new NonceModel(); $nonce->_generateAndSetKey(); $nonce->setHash($hash); // Determine the datetime that this nonce expires. $date = new CoreDateTime(); $date->modify($expires); $nonce->set('expires', $date->getFormatted('U', Time::TIMEZONE_GMT)); $nonce->set('data', $data); $nonce->save(); return $nonce->get('key'); }