公链实战系列1 ——Base58编码算法理论与实战

理论介绍

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

设计Base58的主要目的有以下几点:

  • 避免混淆,在某些字体下,数字0和字母大写O 以及字母大写I和字母小写l会非常相似,难以辨认。
  • 不使用+和/的原因是非字母或数字的字符串作为账号较难被接受。
  • 没有标点符号,通常不会被从中间分行
  • 大部分软件支持双击选择整个字符串。

Base58编码

Value Char Value Char Value Char Value Char
0 1 16 H 32 Z 48 q
1 2 17 J 33 a 49 r
2 3 18 K 34 b 50 s
3 4 19 L 35 c 51 t
4 5 20 M 36 d 52 u
5 6 21 N 37 e 53 v
6 7 22 P 38 f 54 w
7 8 23 R 39 g 55 x
8 9 24 S 40 h 56 y
9 A 25 T 41 i 57 z
10 B 26 U 42 j
11 C 27 V 43 k
12 D 28 W 44 m
13 E 29 X 45 n
14 F 30 Y 46 o
15 G 31 Z 47 p

Base58=[1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", 
"F", "G", "H", "G", "K", "L", "M", "N", "P", "Q", "R", "S", 
"T", "U", "V", "W", "X", "Y", "Z",  "a", "b", "c", "d", "e", "f", "g",
 "h", "i", "g", "k","m", "n", "o", "p", "q", "r", "s", "t", "u", "v", 
 "w", "x", "y", "z" ]

以上是Base58加密算法的理论,接下来,我们分别用pythongo语言来实现Base58加密算法。

代码实现

Python实现

```python

!/usr/bin/env python

-- coding:utf-8 --

"""
This Document is Created by At 2018/7/6
"""

Base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def base58decode(data):
"""
base58 解码
:param data:
:return:
"""
result = 0

for d in data:
    charIndex = Base58Alphabet.find(d)
    result = result * len(Base58Alphabet)
    result = result + charIndex

decoded = hex(result)

# if data[0] == Base58Alphabet[0]:
#     decoded = str(0x0) + decoded

return decoded

def base58encode(data):

result = []
# 首先将字符串转换成十六进制数
x = int(data, 16)
base = 58

zero = 0

while x != zero:
    x, mod = divmod(x, base)
    result.append(Base58Alphabet[mod])

# if data[0] == str(0x0):
#     result.append(Base58Alphabet[0])

# 利用自己实现的reverse算法,当然实际工作中直接调用python标准库中的函数
return "".join(reverse(result))

def reverse(res):
"""
反转列表
:param res:
:return:
"""

if len(res) <= 1:
    return res

length_half = int(len(res) / 2)
length = len(res) - 1

for i in range(length_half):
    tmp = res[i]
    res[i] = res[length-i]
    res[length-i] = tmp

return res
top Created with Sketch.