5
5
* https://www.nayuki.io/page/bitcoin-cryptography-library
6
6
* https://github.com/nayuki/Bitcoin-Cryptography-Library
7
7
*/
8
-
9
8
package one .wangwei .blockchain .util ;
10
9
11
10
import java .io .ByteArrayOutputStream ;
12
11
import java .io .IOException ;
13
12
import java .math .BigInteger ;
14
13
import java .util .Arrays ;
15
14
16
-
17
15
/**
18
- * Converts between an array of bytes and a Base58Check string. Not instantiable.
16
+ * Base58 转化工具
19
17
*/
20
18
public final class Base58Check {
21
19
22
- /*---- Static functions ----*/
23
-
24
- // Adds the checksum and converts to Base58Check. Note that the caller needs to prepend the version byte(s).
20
+ /**
21
+ * 添加校验码并转化为 Base58 字符串
22
+ *
23
+ * @param data
24
+ * @return
25
+ */
25
26
public static String bytesToBase58 (byte [] data ) {
26
27
return rawBytesToBase58 (addCheckHash (data ));
27
28
}
28
29
29
- // Directly converts to Base58Check without adding a checksum.
30
+ /**
31
+ * 转化为 Base58 字符串
32
+ *
33
+ * @param data
34
+ * @return
35
+ */
30
36
public static String rawBytesToBase58 (byte [] data ) {
31
37
// Convert to base-58 string
32
38
StringBuilder sb = new StringBuilder ();
@@ -38,16 +44,22 @@ public static String rawBytesToBase58(byte[] data) {
38
44
}
39
45
40
46
// Add '1' characters for leading 0-value bytes
41
- for (int i = 0 ; i < data .length && data [i ] == 0 ; i ++)
47
+ for (int i = 0 ; i < data .length && data [i ] == 0 ; i ++) {
42
48
sb .append (ALPHABET .charAt (0 ));
49
+ }
43
50
return sb .reverse ().toString ();
44
51
}
45
52
46
53
47
- // Returns a new byte array by concatenating the given array with its checksum.
54
+ /**
55
+ * 添加校验码并返回待有校验码的原生数据
56
+ *
57
+ * @param data
58
+ * @return
59
+ */
48
60
static byte [] addCheckHash (byte [] data ) {
49
61
try {
50
- byte [] hash = Arrays .copyOf (Sha256 . getDoubleHash (data ). toBytes ( ), 4 );
62
+ byte [] hash = Arrays .copyOf (BtcAddressUtils . doubleHash (data ), 4 );
51
63
ByteArrayOutputStream buf = new ByteArrayOutputStream ();
52
64
buf .write (data );
53
65
buf .write (hash );
@@ -58,41 +70,53 @@ static byte[] addCheckHash(byte[] data) {
58
70
}
59
71
60
72
61
- // Converts the given Base58Check string to a byte array, verifies the checksum, and removes the checksum to return the payload.
62
- // The caller is responsible for handling the version byte(s).
73
+ /**
74
+ * 将 Base58Check 字符串转化为 byte 数组,并校验其校验码
75
+ * 返回的byte数组带有版本号,但不带有校验码
76
+ *
77
+ * @param s
78
+ * @return
79
+ */
63
80
public static byte [] base58ToBytes (String s ) {
64
81
byte [] concat = base58ToRawBytes (s );
65
82
byte [] data = Arrays .copyOf (concat , concat .length - 4 );
66
83
byte [] hash = Arrays .copyOfRange (concat , concat .length - 4 , concat .length );
67
- byte [] rehash = Arrays .copyOf (Sha256 . getDoubleHash (data ). toBytes ( ), 4 );
68
- if (!Arrays .equals (rehash , hash ))
84
+ byte [] rehash = Arrays .copyOf (BtcAddressUtils . doubleHash (data ), 4 );
85
+ if (!Arrays .equals (rehash , hash )) {
69
86
throw new IllegalArgumentException ("Checksum mismatch" );
87
+ }
70
88
return data ;
71
89
}
72
90
73
91
74
- // Converts the given Base58Check string to a byte array, without checking or removing the trailing 4-byte checksum.
92
+ /**
93
+ * 将 Base58Check 字符串反转为 byte 数组
94
+ *
95
+ * @param s
96
+ * @return
97
+ */
75
98
static byte [] base58ToRawBytes (String s ) {
76
99
// Parse base-58 string
77
100
BigInteger num = BigInteger .ZERO ;
78
101
for (int i = 0 ; i < s .length (); i ++) {
79
102
num = num .multiply (ALPHABET_SIZE );
80
103
int digit = ALPHABET .indexOf (s .charAt (i ));
81
- if (digit == -1 )
104
+ if (digit == -1 ) {
82
105
throw new IllegalArgumentException ("Invalid character for Base58Check" );
106
+ }
83
107
num = num .add (BigInteger .valueOf (digit ));
84
108
}
85
-
86
109
// Strip possible leading zero due to mandatory sign bit
87
110
byte [] b = num .toByteArray ();
88
- if (b [0 ] == 0 )
111
+ if (b [0 ] == 0 ) {
89
112
b = Arrays .copyOfRange (b , 1 , b .length );
90
-
113
+ }
91
114
try {
92
115
// Convert leading '1' characters to leading 0-value bytes
93
116
ByteArrayOutputStream buf = new ByteArrayOutputStream ();
94
- for (int i = 0 ; i < s .length () && s .charAt (i ) == ALPHABET .charAt (0 ); i ++)
117
+ for (int i = 0 ; i < s .length () && s .charAt (i ) == ALPHABET .charAt (0 ); i ++) {
95
118
buf .write (0 );
119
+ }
96
120
buf .write (b );
97
121
return buf .toByteArray ();
98
122
} catch (IOException e ) {
0 commit comments