Code Monkey home page Code Monkey logo

hash-int's Introduction

Release GitHub License Downloads DigitalOcean Twitter BrowserStack

Hash Integer

A PHP class to generate an alphanumeric hash of specified length from an integer.

composer require dmhendricks/hash-int

Requirements

  • PHP 5.6 or higher
  • BC Math: apt install php7.x-bcmath

Usage

$num = 456;

/**
 * Create a 7-character hash from integer
 */
$hash = \CloudVerve\HashInt::hash( $num, 7 );

// Unhash the string
$unhashed = \CloudVerve\HashInt::unhash( $hash );

echo 'Hashed: ' . $hash . '<br />';
echo 'Unhashed: ' . $unhashed;

Result

Hashed: p3Wq1aC
Unhashed: 456

Original Author Notes

Paraphrased (source):

I wanted a short, unique, alphanumeric hash with a sequence that is difficult to deduce. I could create it with md5 and trim the first n chars, but storing a truncated checksum in a unique field means that the frequency of collisions will increase geometrically as the number of unique keys for a base 62-encoded integer approaches 62^n. I'd rather do it right than create a time bomb.

Base 62

Hashes are base 62 encoded base 10 integers. 1=1, 10=a, 36=Z, 61=z, 62=10, 72=1a, etc.

62, 3844, 238328, 14776336, 916132832, 56800235584, 3521614606208

1 character = 62 permutations, 2 characters = 3844 permutations, etc.

41, 2377, 147299, 9132313, 566201239, 35104476161, 2176477521929

41 = next highest prime from golden mean of 62.

2377 = next highest prime from golden mean of 3844.

Uniqueness

I chose to use primes to ensure hash uniqueness. Any prime greater than one half 62^n will do, but if you use a prime near 62^n or 62^n/2 or 2*62^n/3 etc, you will detect a linearity in the sequence at certain points in the ring.

Appearance of Randomness

I chose primes near the golden ratio to maximize the appearance of randomness. Given a small set of hashes (even with the associated id) it would be difficult for anyone to guess the next hash.

โš ๏ธ Minimum-Security Technique

This is a thin rotation and base re-encoding obfuscation algorithm, not an encryption algorithm. Don't use this to encrypt sensitive information - use it to obfuscate integer IDs or create short reference keys (as an example, for short URLs).

Special Thanks

Analytics

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.