You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.6 KiB
88 lines
1.6 KiB
<?php |
|
|
|
|
|
|
|
class Crypto { |
|
|
|
public static function bc2bin($num) { |
|
|
|
return self::dec2base($num, 256); |
|
} |
|
|
|
public static function dec2base($dec, $base, $digits = false) { |
|
|
|
if ($base < 2 || $base > 256) { |
|
die('Invalid Base: ' . $base); |
|
} |
|
|
|
bcscale(0); |
|
|
|
$value = ''; |
|
|
|
if (!$digits) { |
|
$digits = self::digits($base); |
|
} |
|
|
|
while ($dec > $base - 1) { |
|
|
|
$rest = bcmod($dec, $base); |
|
$dec = bcdiv($dec, $base); |
|
$value = $digits[$rest] . $value; |
|
} |
|
|
|
$value = $digits[intval($dec)] . $value; |
|
|
|
return (string) $value; |
|
} |
|
|
|
public static function base2dec($value, $base, $digits = false) { |
|
|
|
if ($base < 2 || $base > 256) { |
|
die('Invalid Base: ' . $base); |
|
} |
|
|
|
bcscale(0); |
|
|
|
if ($base < 37) { |
|
$value = strtolower($value); |
|
} |
|
if (!$digits) { |
|
$digits = self::digits($base); |
|
} |
|
|
|
$size = strlen($value); |
|
$dec = '0'; |
|
|
|
for ($loop = 0; $loop < $size; $loop++) { |
|
$element = strpos($digits, $value[$loop]); |
|
$power = bcpow($base, $size - $loop - 1); |
|
$dec = bcadd($dec, bcmul($element, $power)); |
|
} |
|
|
|
return (string) $dec; |
|
} |
|
|
|
public static function digits($base) { |
|
|
|
if ($base > 64) { |
|
|
|
$digits = ''; |
|
|
|
for ($loop = 0; $loop < 256; $loop++) { |
|
$digits .= chr($loop); |
|
} |
|
} else { |
|
$digits = '0123456789abcdefghijklmnopqrstuvwxyz'; |
|
$digits .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-_'; |
|
} |
|
|
|
$digits = substr($digits, 0, $base); |
|
|
|
return (string)$digits; |
|
} |
|
|
|
public static function bin2bc($num) { |
|
|
|
return self::base2dec($num, 256); |
|
} |
|
}
|
|
|