Initial Commit
This commit is contained in:
68
inc/helpers/class-hash.php
Normal file
68
inc/helpers/class-hash.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles hashing to encode ids and prevent spoofing due to auto-increments.
|
||||
*
|
||||
* @package WP_Ultimo
|
||||
* @subpackage Helper
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
namespace WP_Ultimo\Helpers;
|
||||
|
||||
use WP_Ultimo\Dependencies\Hashids\Hashids;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Handles hashing to encode ids and prevent spoofing due to auto-increments.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Hash {
|
||||
|
||||
/**
|
||||
* Hash length.
|
||||
*/
|
||||
const LENGTH = 10;
|
||||
|
||||
/**
|
||||
* Static-only class.
|
||||
*/
|
||||
private function __construct() {} // end __construct;
|
||||
|
||||
/**
|
||||
* Encodes a number or ID. Do not use to encode strings.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param integer $number Number to encode.
|
||||
* @param string $group Hash group. Used to increase entropy.
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($number, $group = 'wp-ultimo') {
|
||||
|
||||
$hasher = new Hashids($group, self::LENGTH, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
|
||||
|
||||
return $hasher->encode($number);
|
||||
|
||||
} // end encode;
|
||||
|
||||
/**
|
||||
* Decodes a hash back into an integer.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param string $hash Hash to decode.
|
||||
* @param string $group Hash group. Used to increase entropy.
|
||||
* @return int
|
||||
*/
|
||||
public static function decode($hash, $group = 'wp-ultimo') {
|
||||
|
||||
$hasher = new Hashids($group, 10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
|
||||
|
||||
return current($hasher->decode($hash));
|
||||
|
||||
} // end decode;
|
||||
|
||||
} // end class Hash;
|
Reference in New Issue
Block a user