Last active
August 29, 2015 14:04
-
-
Save alixaxel/23ce91a9f33838d135e2 to your computer and use it in GitHub Desktop.
Damm Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function Damm($string, $encode = true) | |
{ | |
if ($encode > 0) | |
{ | |
$encode += 1; | |
$string = str_split($string, 1); | |
$matrix = array_chunk(str_split('0317598642709215486342068713591750983426612304597836742095815869720134894536201794386172052581436790', 1), 10); | |
while (--$encode > 0) | |
{ | |
$interim = 0; | |
foreach ($string as $value) | |
{ | |
$interim = $matrix[$interim][intval($value)]; | |
} | |
$string[] = $interim; | |
} | |
return implode('', $string); | |
} | |
else if (strcmp($string, Damm(substr($string, 0, max(1, abs($encode)) * -1), max(1, abs($encode)))) === 0) | |
{ | |
return substr($string, 0, max(1, abs($encode)) * -1); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://en.wikipedia.org/wiki/Damm_algorithm