Base58 把字节序列视为一个大整数,并用 Bitcoin 字母表 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz 表示。开头的零字节会变成开头的字符 1,从而在往返转换中保留。
Base58 适合需要人工复制或输入的紧凑标识符,因为它排除了 0、O、I 和 l。本工具使用原始 Bitcoin 字母表,并非 Base58Check:不会添加版本或校验和,其他 Base58 字母表也不兼容。
// npm install bs58
import bs58 from 'bs58';
const encoded = bs58.encode(Buffer.from('Hello'));
console.log(encoded);
// Output: 9Ajdvzr
const decoded = Buffer.from(bs58.decode('9Ajdvzr')).toString('utf8');
console.log(decoded);
// Output: Hello
package main
import (
"fmt"
"github.com/btcsuite/btcutil/base58"
)
func main() {
encoded := base58.Encode([]byte("Hello"))
fmt.Println(encoded)
// Output: 9Ajdvzr
decoded := base58.Decode("9Ajdvzr")
fmt.Println(string(decoded))
// Output: Hello
}
<?php
// composer require tuupola/base58
require 'vendor/autoload.php';
use Tuupola\Base58;
$base58 = new Base58(["characters" => Base58::BITCOIN]);
$encoded = $base58->encode("Hello");
echo $encoded . "\n";
// Output: 9Ajdvzr
$decoded = $base58->decode("9Ajdvzr");
echo $decoded . "\n";
// Output: Hello