Base58

Base58是用于比特幣(Bitcoin)中使用的一种独特的编码方式,主要用于产生Bitcoin的钱包地址。相比Base64,Base58不使用数字"0",字母大写"O",字母大写"I",和字母小写"l",以及"+"和"/"符号。

设计Base58主要的目的是:

避免混淆。在某些字体下,数字0和字母大写O,以及字母大写I和字母小写l会非常相似。

不使用"+"和"/"的原因是非字母或数字的字符串作为帐号较难被接受。

没有标点符号,通常不会被从中间分行。

大部分的软件支持双击选择整个字符串。

以下引用自其作者中本聰(Satoshi Nakamoto)在base58.h中的注释:

//
// Why base-58 instead of standard base-64 encoding?
// - Don't want 0OIl characters that look the same in some fonts and
// could be used to create visually identical looking account numbers.
// - A string with non-alphanumeric characters is not as easily accepted as an account number.
// - E-mail usually won't line-break if there's no punctuation to break at.
// - Doubleclicking selects the whole number as one word if it's all alphanumeric.
//

编码
一個Base58"字元"可以表示的位元數為Log258\approx5.858 bits。經過Base58編碼的數據為原始的數據長度的\tfrac{8}{5.858}\approx1.37倍,稍微多於Base64的1.33倍。

編碼符號表:

由於256不能被58所整除,Base58無法像Base64那樣子轉換為8位元的二進位後依次取出6位元,就可以快速完成轉換;因此,Base58編碼演算法需要除法運算實現。如果被編碼的數據較長,則要用特殊的class來處理大數,在Bitcoin使用了OpenSSL中的BIGNUM:

code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
x = convert_bytes_to_big_integer(hash_result);

output_string = "";

while(x > 0)
{
(x, remainder) = divide(x, 58);
output_string.append(code_string[remainder]);
}

repeat(number_of_leading_zero_bytes_in_hash)
{
output_string.append(code_string[0]);
}

output_string.reverse();

外部链接

参见

  • Base64

评论 (0)

  • 还没有评论,来抢沙发吧。