Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alixaxel/23ce91a9f33838d135e2 to your computer and use it in GitHub Desktop.
Save alixaxel/23ce91a9f33838d135e2 to your computer and use it in GitHub Desktop.
Damm Algorithm
<?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;
}
@alixaxel
Copy link
Author

alixaxel commented Aug 6, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment