sidebar_label | sidebar_position |
---|---|
'Encoding Algorithm' | 0 |
Encoding Algorithm
Base58
Base58 is a group of encoding/decoding schemes used to switch data between binary format (hexdecimal) and alphanumeric text format (ASCII). Base58 enables data compressing, is easy to identify, and is suitable for constructing encoding mechanism of transmission system that is anti-auto-monitoring. However, lack of verification makes it not able to detect error during transmission. Thus for Base58Check, an improved scheme is required.
The Base58's alphabet includes numbers (From 1 to 9), and English letters except O (uppercase o) / I (uppercase i) / l (lowercase L). These letters are omitted to avoid confusion.
Neo's Base58 alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
Interface definition:
-
Encoding method: Encoding byte[] format data into Base58 string format
-
Decoding Method: Decoding Base58 string format data into byte[] format
Encoding Steps :
-
Add 0x00 before byte[] data to generate a new byte array, and then reverse its order (little endian)
-
Convert array data to a BigInteger object
-
Convert the BigInteger format number to 58-based number according to Base58 alphabet
-
Count the number of 0x00 in original byte array format data. At the head of the Base58 format data generated in step 3, for each 0x00, add a letter '1', which is the first character in Base58 alphabet
Decoding Steps :
-
Convert the input string into BigInteger format according to Base58 alphabet.
-
Convert from BigInteger format to byte[] format and then reverse the order to big endian.
-
If byte[] format data's length is more than 1 & byte[0] = 0 & byte[1] >= 0x80, start from byte[1], otherwise start from byte[0] to get the decoded result.
-
Count the number of the first letter of Base58 alphabet in original input data as count and remove leading zeros from the decoded data.
Example:
String Content | byte[] |
---|---|
NTRAJ9EEjHFHhHZvMKEKfkceg5V9ppx5ZP | [0x35, 0x52, 0x4e, 0x37, 0xb7, 0x01, 0x39, 0xc8, 0x96, 0xeb, 0xd5, 0x4a, 0x86, 0x48, 0xd3, 0xfa, 0x78, 0x6b, 0x26, 0x48, 0x76, 0xea, 0xc5, 0x26, 0xce] |
Scenarios:
Serves for Base58Check encoding / decoding method.
Base58Check
Base58Check is an improved encoding / decoding method base on Base58. Base58Check solved the lack of checking mechanism in Base58, by adding hash value to original data as salt.
Interface definition:
-
Encoding method: encode byte array data into checkable Base58 string format
-
Decoding method:decode checkable Base58 string data into byte array format
Encoding Steps :
-
Encode input byte array twice with Sha256 to get a hashed byte array. Take the first 4 bytes of the hash as checksum and append it to the end of original byte array.
-
Use Base58 to encode the byte array including checksum to get corresponding encoded result.
Decoding Steps :
-
Use Base58 to decode input string to get byte array format decoded result.
-
Take the content of byte array except the last 4 bytes as data.
-
Encode data twice with SHA256 to get a hashed byte array. Check whether the first 4 bytes of the hashed value are identical with the last 4 bytes in byte array of step 1. If so, the decoded data is correct. Otherwise the data is corrupted.
Example:
String content | byte[] |
---|---|
NTRAJ9EEjHFHhHZvMKEKfkceg5V9ppx5ZP | [0x35, 0x52, 0x4e, 0x37, 0xb7, 0x01, 0x39, 0xc8, 0x96, 0xeb, 0xd5, 0x4a, 0x86, 0x48, 0xd3, 0xfa, 0x78, 0x6b, 0x26, 0x48, 0x76] |
Scenarios:
-
Import / export WIF format secret key
-
Switch between contract script hash and address
-
Import / export NEP2 format secret key