Skip to content

Instantly share code, notes, and snippets.

/decode.php Secret

Created July 15, 2016 12:47
Show Gist options
  • Save anonymous/1939c6e76d4bf400eb9b525a76dd2288 to your computer and use it in GitHub Desktop.
Save anonymous/1939c6e76d4bf400eb9b525a76dd2288 to your computer and use it in GitHub Desktop.
AlphaID
<?php
function decode( $input, $index ) {
$base = strlen( $index );
$output = 0;
$length = strlen( $input ) - 1;
for( $i = 0; $i <= $length; $i++ ) {
$bcpow = bcpow( $base, $length - $i );
$output += strpos( $index, substr( $input, $i, 1 ) ) * $bcpow;
}
$output -= pow( $base, 4 );
$output = sprintf( '%F', $output );
$output = substr( $output, 0, strpos( $output, '.' ) );
return (int) $output;
}
<?php
function encode( $input, $index ) {
$base = strlen( $index );
$input += pow( $base, 4 );
$output = '';
for( $i = floor( log( $input, $base ) ); $i >= 0; $i-- ) {
$bcp = bcpow( $base, $i );
$start = floor( $input / $bcp ) % $base;
$output .= substr( $index, $start, 1 );
$input = $input - ( $start * $bcp );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment