forked from TrueCloudLab/frostfs-sdk-java
parent
7518893388
commit
ab8a574d0d
14 changed files with 139 additions and 25 deletions
|
@ -10,8 +10,12 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class Base58 {
|
||||
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
|
||||
public static final int BASE58_SYMBOL_COUNT = 58;
|
||||
public static final int BASE256_SYMBOL_COUNT = 256;
|
||||
private static final int BYTE_DIVISION = 0xFF;
|
||||
private static final char ENCODED_ZERO = ALPHABET[0];
|
||||
private static final int[] INDEXES = new int[128];
|
||||
private static final char BASE58_ASCII_MAX_VALUE = 128;
|
||||
private static final int[] INDEXES = new int[BASE58_ASCII_MAX_VALUE];
|
||||
|
||||
static {
|
||||
Arrays.fill(INDEXES, -1);
|
||||
|
@ -69,7 +73,7 @@ public class Base58 {
|
|||
char[] encoded = new char[input.length * 2]; // upper bound
|
||||
int outputStart = encoded.length;
|
||||
for (int inputStart = zeros; inputStart < input.length; ) {
|
||||
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, 256, 58)];
|
||||
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, BASE256_SYMBOL_COUNT, BASE58_SYMBOL_COUNT)];
|
||||
if (input[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
|
@ -93,7 +97,7 @@ public class Base58 {
|
|||
byte[] input58 = new byte[input.length()];
|
||||
for (int i = 0; i < input.length(); ++i) {
|
||||
char c = input.charAt(i);
|
||||
int digit = c < 128 ? INDEXES[c] : -1;
|
||||
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
|
||||
if (digit < 0) {
|
||||
throw new IllegalArgumentException(String.format("Invalid character in Base58: 0x%04x", (int) c));
|
||||
}
|
||||
|
@ -108,7 +112,7 @@ public class Base58 {
|
|||
byte[] decoded = new byte[input.length()];
|
||||
int outputStart = decoded.length;
|
||||
for (int inputStart = zeros; inputStart < input58.length; ) {
|
||||
decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
|
||||
decoded[--outputStart] = divmod(input58, inputStart, BASE58_SYMBOL_COUNT, BASE256_SYMBOL_COUNT);
|
||||
if (input58[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
|
@ -125,7 +129,7 @@ public class Base58 {
|
|||
// this is just long division which accounts for the base of the input digits
|
||||
int remainder = 0;
|
||||
for (int i = firstDigit; i < number.length; i++) {
|
||||
int digit = (int) number[i] & 0xFF;
|
||||
int digit = (int) number[i] & BYTE_DIVISION;
|
||||
int temp = remainder * base + digit;
|
||||
number[i] = (byte) (temp / divisor);
|
||||
remainder = temp % divisor;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue