NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

<?php

class CSRF {

/**
* The default token name
*/
const TOKEN_NAME = "_csrf_token_645a83a41868941e4692aa31e7235f2";

/**
* (Re-)Generate a token and write it to session
*
* @param string $token_name - defaults to the default token name
* @return void
*/
public static function generateToken($token_name = self::TOKEN_NAME)
{
// generate as random of a token as possible
$salt = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : uniqid();
Session::instance()->set($token_name, sha1(uniqid(sha1($salt), true)));
}

/**
* Get the token. If it's not defined, this will go ahead and generate one.
*
* @param string $token_name - defaults to the default token name
* @return string
*/
public static function getToken($token_name = self::TOKEN_NAME)
{
if ( ! $token_value = Session::instance()->get($token_name)) {
static::generateToken($token_name);
}

return $token_value;
}

/**
* Get the token name. This is just a CRUD method to make your code cleaner.
*
* @param string $token_name
* @return string
*/
public static function getTokenName($token_name = self::TOKEN_NAME)
{
return $token_name;
}

/**
* Validate the token. If there's not one yet, it will set one and return false.
*
* @param array $request_data - your whole POST/GET array - will index in with the token name to get the token.
* @param string $token_name - defaults to the default token name
* @return bool
*/
public static function validate($request_data = array(), $token_name = self::TOKEN_NAME)
{
if ( ! Session::instance()->get($token_name)) {
static::generateToken($token_name);
return false;
} elseif (empty($request_data[$token_name])) {
return false;
} else {
return static::compare($request_data[$token_name], static::getToken($token_name));
}
}

/**
* Get a hidden input string with the token/token name in it.
*
* @param string $token_name - defaults to the default token name
* @return string
*/
public static function getHiddenInputString($token_name = self::TOKEN_NAME)
{
return sprintf('<input type="hidden" name="%s" value="%s"/>', $token_name, static::getToken($token_name));
}

/**
* Get a query string mark-up with the token/token name in it.
*
* @param string $token_name - defaults to the default token name
* @return string
*/
public static function getQueryString($token_name = self::TOKEN_NAME)
{
return sprintf('%s=%s', $token_name, static::getToken($token_name));
}

/**
* Get an array with the token (useful for form libraries, etc.)
*
* @param string $token_name
* @return array
*/
public static function getTokenAsArray($token_name = self::TOKEN_NAME)
{
return array(
$token_name => self::getToken($token_name)
);
}

/**
* Constant-time string comparison. This comparison function is timing-attack safe
*
* @param string $hasha
* @param string $hashb
* @return bool
*/
public static function compare($hasha = "", $hashb = "")
{
// we want hashes_are_not_equal to be false by the end of this if the strings are identical

// if the strings are NOT equal length this will return true, else false
$hashes_are_not_equal = strlen($hasha) ^ strlen($hashb);

// compare the shortest of the two strings (the above line will still kick back a failure if the lengths weren't equal. this just keeps us from over-flowing our strings when comparing
$length = min(strlen($hasha), strlen($hashb));
$hasha = substr($hasha, 0, $length);
$hashb = substr($hashb, 0, $length);

// iterate through the hashes comparing them character by character
// if a character does not match, then return true, so the hashes are not equal
for ($i = 0; $i < strlen($hasha); $i++) {
$hashes_are_not_equal += !(ord($hasha[$i]) === ord($hashb[$i]));
}

// if not hashes are not equal, then hashes are equal
return !$hashes_are_not_equal;
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.