Class Base58Util
java.lang.Object
com.sure.tool.codec.Base58Util
Base58 编解码工具,使用 Bitcoin 字母表
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz。
与 Base64 相比,Base58 去除了易混淆的 0/O/I/l
以及 +//,适合短链接、钱包地址、邀请码等需要人工抄写的场景。
编码保留原始字节的前导零字节(输出为等数量的 1),解码时原样恢复。
示例:
String encoded = Base58Util.encode("hello".getBytes(StandardCharsets.UTF_8)); // "Cn8eVZg"
String plain = Base58Util.decodeToString("Cn8eVZg"); // "hello"
boolean valid = Base58Util.isBase58("Cn8eVZg"); // true
null 语义:入参为 null 时返回 null(空安全);
解码遇到非 Base58 字母表字符抛 IllegalArgumentException。
- Since:
- 1.19.1
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]解码 Base58 字符串为字节数组。static StringdecodeToString(String base58) 解码 Base58 字符串为 UTF-8 字符串。static StringdecodeToString(String base58, Charset charset) 解码 Base58 字符串为字符串。static Stringencode(byte[] data) 编码字节数组为 Base58 字符串。static String编码字符串为 Base58 字符串(默认 UTF-8)。static booleanisBase58(CharSequence text) 判断字符串是否为合法的 Base58 文本。
-
Method Details
-
encode
编码字节数组为 Base58 字符串。示例:
Base58Util.encode("hello".getBytes(UTF_8)) -> "Cn8eVZg"- Parameters:
data- 待编码字节,可为null- Returns:
- Base58 字符串;
null返回null
-
encode
-
decode
解码 Base58 字符串为字节数组。示例:
Base58Util.decode("Cn8eVZg") -> "hello" 的 UTF-8 字节- Parameters:
base58- Base58 字符串,可为null- Returns:
- 解码字节数组;
null返回null,空串返回空数组 - Throws:
IllegalArgumentException- 含非 Base58 字符
-
decodeToString
-
decodeToString
-
isBase58
判断字符串是否为合法的 Base58 文本。- Parameters:
text- 待校验文本,可为null- Returns:
- 非空且所有字符均在 Bitcoin Base58 字母表内返回
true
-