Base58和Base58Check加解密实现代码

2020-12-15  本文已影响0人  wenju

1.Base58

public class Base58 {
    public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
    private static final int[] INDEXES = new int[128];
    static {
        for (int i = 0; i < INDEXES.length; i++) {
            INDEXES[i] = -1;
        }
        for (int i = 0; i < ALPHABET.length; i++) {
            INDEXES[ALPHABET[i]] = i;
        }
    }

    /**
     * Encodes the given bytes in base58. No checksum is appended.
     */
    public static String encode(byte[] input) {
        if (input.length == 0) {
            return "";
        }
        input = copyOfRange(input, 0, input.length);
        // Count leading zeroes.
        int zeroCount = 0;
        while (zeroCount < input.length && input[zeroCount] == 0) {
            ++zeroCount;
        }
        // The actual encoding.
        byte[] temp = new byte[input.length * 2];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input.length) {
            byte mod = divmod58(input, startAt);
            if (input[startAt] == 0) {
                ++startAt;
            }
            temp[--j] = (byte) ALPHABET[mod];
        }

        // Strip extra ‘1‘ if there are some after decoding.
        while (j < temp.length && temp[j] == ALPHABET[0]) {
            ++j;
        }
        // Add as many leading ‘1‘ as there were leading zeros.
        while (--zeroCount >= 0) {
            temp[--j] = (byte) ALPHABET[0];
        }

        byte[] output = copyOfRange(temp, j, temp.length);
        try {
            return new String(output, "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);  // Cannot happen.
        }
    }

    public static byte[] decode(String input) throws IllegalArgumentException {
        if (input.length() == 0) {
            return new byte[0];
        }
        byte[] input58 = new byte[input.length()];
        // Transform the String to a base58 byte sequence
        for (int i = 0; i < input.length(); ++i) {
            char c = input.charAt(i);

            int digit58 = -1;
            if (c >= 0 && c < 128) {
                digit58 = INDEXES[c];
            }
            if (digit58 < 0) {
                throw new IllegalArgumentException("Illegal character " + c + " at " + i);
            }

            input58[i] = (byte) digit58;
        }
        // Count leading zeroes
        int zeroCount = 0;
        while (zeroCount < input58.length && input58[zeroCount] == 0) {
            ++zeroCount;
        }
        // The encoding
        byte[] temp = new byte[input.length()];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input58.length) {
            byte mod = divmod256(input58, startAt);
            if (input58[startAt] == 0) {
                ++startAt;
            }

            temp[--j] = mod;
        }
        // Do no add extra leading zeroes, move j to first non null byte.
        while (j < temp.length && temp[j] == 0) {
            ++j;
        }

        return copyOfRange(temp, j - zeroCount, temp.length);
    }

    public static BigInteger decodeToBigInteger(String input) throws IllegalArgumentException {
        return new BigInteger(1, decode(input));
    }

    //
    // number -> number / 58, returns number % 58
    //
    private static byte divmod58(byte[] number, int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number.length; i++) {
            int digit256 = (int) number[i] & 0xFF;
            int temp = remainder * 256 + digit256;

            number[i] = (byte) (temp / 58);

            remainder = temp % 58;
        }

        return (byte) remainder;
    }

    //
    // number -> number / 256, returns number % 256
    //
    private static byte divmod256(byte[] number58, int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number58.length; i++) {
            int digit58 = (int) number58[i] & 0xFF;
            int temp = remainder * 58 + digit58;

            number58[i] = (byte) (temp / 256);

            remainder = temp % 256;
        }

        return (byte) remainder;
    }

    private static byte[] copyOfRange(byte[] source, int from, int to) {
        byte[] range = new byte[to - from];
        System.arraycopy(source, from, range, 0, range.length);

        return range;
    }
}

2.Base58Check

public class Base58Check {
   private static String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
   private static char[] ALPHABET_ARRAY = ALPHABET.toCharArray();
   private static BigInteger BASE_SIZE = BigInteger.valueOf(ALPHABET_ARRAY.length);
   private static int CHECKSUM_SIZE = 4;

   public static String encode(byte[] data) throws NoSuchAlgorithmException {
       return encodePlain(addChecksum(data));
   }

   public static String encodePlain(byte[] data) {
       BigInteger intData;

       try {
           intData = new BigInteger(1, data);
       } catch (NumberFormatException e) {
           return "";
       }

       String result = "";

       while(intData.compareTo(BigInteger.ZERO) == 1) {
           BigInteger[] quotientAndRemainder = intData.divideAndRemainder(BASE_SIZE);

           BigInteger quotient = quotientAndRemainder[0];
           BigInteger remainder = quotientAndRemainder[1];

           intData = quotient;

           result = ALPHABET_ARRAY[remainder.intValue()] + result;
       }

       for (int i = 0; i < data.length && data[i] == 0; i++)
       {
           result = '1' + result;
       }

       return result;
   }


   public static byte[] decode(String encoded) throws NoSuchAlgorithmException {
       byte[] valueWithChecksum = decodePlain(encoded);

       byte[] value = verifyAndRemoveChecksum(valueWithChecksum);

       if (value == null) {
           throw new IllegalArgumentException("Base58 checksum is invalid");
       }

       return value;
   }

   public static byte[] decodePlain(String encoded) {
       if (encoded.length() == 0) {
           return new byte[0];
       }

       BigInteger intData = BigInteger.ZERO;
       int leadingZeros = 0;

       for (int i = 0; i < encoded.length(); i++) {
           char current = encoded.charAt(i);

           int digit = ALPHABET.indexOf(current);

           if (digit == -1)
           {
               throw new IllegalArgumentException(String.format("Invalid Base58 character `%c` at position %d", current, i));
           }

           intData = (intData.multiply(BASE_SIZE)).add(BigInteger.valueOf(digit));
       }

       for (int i = 0; i < encoded.length(); i++) {
           char current = encoded.charAt(i);

           if (current == '1') {
               leadingZeros++;
           } else {
               break;
           }
       }

       byte[] bytesData;
       if (intData.equals(BigInteger.ZERO)) {
           bytesData = new byte[0];
       } else {
           bytesData = intData.toByteArray();
       }

       //Should we cut the sign byte ? - https://bitcoinj.googlecode.com/git-history/216deb2d35d1a128a7f617b91f2ca35438aae546/lib/src/com/google/bitcoin/core/Base58.java
       boolean stripSignByte = bytesData.length > 1 && bytesData[0] == 0 && bytesData[1] < 0;

       byte[] decoded = new byte[bytesData.length - (stripSignByte ? 1 : 0) + leadingZeros];

       System.arraycopy(bytesData, stripSignByte ? 1 : 0, decoded, leadingZeros, decoded.length - leadingZeros);

       return decoded;
   }

   private static byte[] verifyAndRemoveChecksum(byte[] data) throws NoSuchAlgorithmException {
       byte[] value = Arrays.copyOfRange(data, 0, data.length - CHECKSUM_SIZE);
       byte[] checksum = Arrays.copyOfRange(data, data.length - CHECKSUM_SIZE, data.length);
       byte[] expectedChecksum = getChecksum(value);

       return Arrays.equals(checksum, expectedChecksum) ? value : null;
   }

   private static byte[] addChecksum(byte[] data) throws NoSuchAlgorithmException {
       byte[] checksum = getChecksum(data);

       byte[] result = new byte[data.length + checksum.length];

       System.arraycopy(data, 0, result, 0, data.length);
       System.arraycopy(checksum, 0, result, data.length, checksum.length);

       return result;
   }

   private static byte[] getChecksum(byte[] data) throws NoSuchAlgorithmException {
       byte[] hash = hash256(data);
       hash = hash256(hash);

       return Arrays.copyOfRange(hash, 0, CHECKSUM_SIZE);
   }

   public static byte[] hash256(byte[] data) throws NoSuchAlgorithmException {
       MessageDigest md = MessageDigest.getInstance("SHA-256");

       md.update(data);

       return md.digest();
   }
}
上一篇 下一篇

猜你喜欢

热点阅读