Skip to content

Instantly share code, notes, and snippets.

@indrora
Created September 16, 2012 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indrora/3730933 to your computer and use it in GitHub Desktop.
Save indrora/3730933 to your computer and use it in GitHub Desktop.
ASDFHash: Ascii hashing function
<?php
/*
* @name: asdfhash.php
* @author: morgan ``indrora'' gangwere <indrora@earfolds.com>
*
* @license 2clause BSD
*
* Copyright (c) 2012, morgan ``indrora'' gangwere
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @description
* asdfhash: a simple (if not overly-simple) hashing algo for stuff.
* This isn't anything secure. Don't trust it to your bank data 'n'
* stuff like that.
*
* Since some people like longer names, you can adjust the constant
* ASDF_MAXLEN to suit your needs.
*
* If you feel SHA512 isn't adequate, try one of its cousins, like
* Whirlpool or crc32 (just kidding, don't use crc32 for anything)
*
* This is as one-way as I can make it. If you can get source
* content from the hash, you're probably working in infosec. I
* would totally love to see how you did it (other than bruteforcing
* the entire ASCII keyspace -- that's cheating!).
*
* Fair warning: this will, eventually, produce such fine works
* as "penis", "sex", "fuck", etc. If you want, drop the following
* from the $chars array:
*
* a,s,f,w,p,i,c,k,u
*
* This drastically reduces your keyspace, but it keeps it.. ehh...
* "cleaner"? I've seen worse come out of imgur's algorithm.
*/
define("ASDF_MAXLEN", 8);
/**
* @name asdfhash
* @param $content [mixed] content which will become part of the hash
* @description A light hashing function which replicates values such
* as those from Imgur and other popular sites.
*/
function asdfhash($content)
{
$hash = array();
// Our allowed characters (URL safe, please!)
$chars = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz01234567890");
// Content digenst (pick a hash, any hash);
$content_md = str_split(hash("sha512", $content, TRUE));
// This essentially guarantees that we will have a string
// which is ASDF_MAXLEN chars long.
for($x =0;$x<ASDF_MAXLEN;$x++)
{
foreach($content_md as $b)
{
$ch = $chars[($x*ord($b)+$x) % sizeof($chars)];
$hash[($x+ord($ch))%ASDF_MAXLEN] = $ch;
}
}
return implode('',$hash);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment