php编码函数 base58,1. Base58可逆加密
发布日期:2021-06-24 16:15:58 浏览次数:4 分类:技术文章

本文共 1531 字,大约阅读时间需要 5 分钟。

#

~~~

function base58_encode($string)

{

$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';

$base = strlen($alphabet);

if (is_string($string) === false) {

return false;

}

if (strlen($string) === 0) {

return '';

}

$bytes = array_values(unpack('C*', $string));

$decimal = $bytes[0];

for ($i = 1, $l = count($bytes); $i < $l; $i++) {

$decimal = bcmul($decimal, 256);

$decimal = bcadd($decimal, $bytes[$i]);

}

$output = '';

while ($decimal >= $base) {

$div = bcdiv($decimal, $base, 0);

$mod = bcmod($decimal, $base);

$output .= $alphabet[$mod];

$decimal = $div;

}

if ($decimal > 0) {

$output .= $alphabet[$decimal];

}

$output = strrev($output);

foreach ($bytes as $byte) {

if ($byte === 0) {

$output = $alphabet[0] . $output;

continue;

}

break;

}

return (string) $output;

}

function base58_decode($base58)

{

if (is_string($base58) === false) {

return false;

}

if (strlen($base58) === 0) {

return '';

}

$indexes = array_flip(str_split($this->alphabet));

$chars = str_split($base58);

foreach ($chars as $char) {

if (isset($indexes[$char]) === false) {

return false;

}

}

$decimal = $indexes[$chars[0]];

for ($i = 1, $l = count($chars); $i < $l; $i++) {

$decimal = bcmul($decimal, $this->base);

$decimal = bcadd($decimal, $indexes[$chars[$i]]);

}

$output = '';

while ($decimal > 0) {

$byte = bcmod($decimal, 256);

$output = pack('C', $byte) . $output;

$decimal = bcdiv($decimal, 256, 0);

}

foreach ($chars as $char) {

if ($indexes[$char] === 0) {

$output = "\x00" . $output;

continue;

}

break;

}

return $output;

}

~~~

转载地址:https://blog.csdn.net/weixin_33758343/article/details/116361144 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:oracle 在需要下列之一,Oracle存储过程中PLS-00103:出现符号“/”在需要下列之一时:(...
下一篇:matlab 变量不存在,matlab程序运行时提示变量未定义

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月17日 04时20分08秒