1. Common codes

Base64: Contains 26 lowercase letters, 26 uppercase letters, 10 digits, /, and +

Base58 (blockchain) : Remove 6 confusing ones, remove 0, uppercase O, uppercase I, lowercase L, /, +/, + influence double click selection

2. Base64 encoding and decoding with GO language

2.1 Base64 Encoding Principles

2.2 Base64 encoding and decoding

base64

package main

import (
	"encoding/base64"
	"fmt"
)

func Base64Encoding(str string) string {  / / Base64 encoding
	src := []byte(str)
	res := base64.StdEncoding.EncodeToString(src)  // Turn the encoding into a string
	return res
}

func Base64Decoding(str string) string {  / / Base64 decoding
	res,_:=base64.StdEncoding.DecodeString(str)
	return string(res)
}

func main(a) {
	src := "FanOne"
	res := Base64Encoding(src) / / code
	fmt.Println("FanOne code becomes:",res)
	ret := Base64Decoding(res)  / / decoding
	fmt.Println(res,"Decode into:",ret)
}
/* Result: FanOne encoding becomes: RmFuT25l RmFuT25l decoding is called: FanOne */

Copy the code

3. Base58 encoding and decoding with GO language

Base58 coding table

3.1 Encoding process of Base58

  1. Converting each byte of a string to ASCII, the string is actually a combination of numbers in base 256
  2. Convert a number from base 256 to base 10
  3. Convert a decimal number to a number in base 58 (divide by 58 and each time you get the remainder in the corresponding base 58, with 0 represented by the code 1)
  4. Find the corresponding character of the base 58 number against the 58 encoding table

Example:

  1. Replace Fan with ASCII: 77, 97, 110
  2. Convert 256 from 77, 97, 110 to the decimal value: 77256256 + 97*256 + 110 = 4612462
  3. Divide 4612462 by 58, and each time the remainder is the corresponding base 58, with 0 represented by code 1:
  4. Look up the table with the remainder value

3.2 Implementation of Base58 codec

package main

import (
	"bytes"
	"fmt"
	"math/big"
)


var base58= []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

func Base58Encoding(str string) string { 		/ / Base58 encoding
	//1. Convert the value to the corresponding ASCII code
	strByte := []byte(str)
	// FMT.Println(strByte) // Result [70 97 110]
	//2. Convert to decimal
	strTen := big.NewInt(0).SetBytes(strByte)
	// FMT.Println(strTen) // Result 4612462
	//3. Remove the remainder
	var modSlice []byte
	for strTen.Cmp(big.NewInt(0)) > 0 {
		mod:=big.NewInt(0)  			/ / remainder
		strTen58:=big.NewInt(58)
		strTen.DivMod(strTen,strTen58,mod)  // Take mod
		modSlice = append(modSlice, base58[mod.Int64()])    // Store the remainder and put the corresponding value into it
 	}
	// Use byte '1' instead of 0 when 0 is 1
	for _,elem := range strByte{
		ifelem! =0{
			break
		}else if elem == 0{
			modSlice = append(modSlice,byte('1'))}}// fmt.println (modSlice) // The result [12 7 37 23] is reversed, because the remainder is the opposite.
	// fmt.println (string(modSlice)) // Result D8eQ
	ReverseModSlice:=ReverseByteArr(modSlice)
	(ReverseModSlice) // reverse [81 101 56 68]
	Println(String (ReverseModSlice))
	return string(ReverseModSlice)
}

func ReverseByteArr(bytes []byte) []byte{  	// Invert the array of bytes
	for i:=0; i<len(bytes)/2; i++{ bytes[i],bytes[len(bytes)- 1-i] = bytes[len(bytes)- 1-i],bytes[i]  // Switch back and forth
	}
	return bytes
}

// is the inverse of coding
func Base58Decoding(str string) string { / / Base58 decoding
	strByte := []byte(str)
	//fmt.Println(strByte) //[81 101 56 68]
	ret := big.NewInt(0)
	for _,byteElem := range strByte{
		index := bytes.IndexByte(base58,byteElem) // Get the subscript of the array corresponding to base58
		ret.Mul(ret,big.NewInt(58))  			// Multiply back
		ret.Add(ret,big.NewInt(int64(index)))  / / add
	}
	Println(ret) // get the decimal 4612462
	//fmt.Println(ret.Bytes()) //[70 97 110]
	//fmt.Println(string(ret.Bytes()))
	return string(ret.Bytes())
}

func main(a) {
	src := "Fan"
	res := Base58Encoding(src)
	fmt.Println(res)  //Qe8D
	resD:=Base58Decoding(res)
	fmt.Println(resD)  //Fan
}
Copy the code

4. The last

Xiao Sheng Fan Yi, looking forward to your attention.