Java tutorial
//package com.java2s; /* * The MIT License (MIT) * <p/> * Copyright (c) 2013-2014 Valentin Konovalov * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * <p/> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { private static final char[] BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray(); private static final int BASE58_CHUNK_DIGITS = 10; private static final BigInteger BASE58_CHUNK_MOD = BigInteger.valueOf(0x5fa8624c7fba400L); private static final byte[] BASE58_VALUES = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; public static boolean verifyBitcoinAddress(String address) { byte[] decodedAddress = decodeBase58(address); return !(decodedAddress == null || decodedAddress.length < 6 || decodedAddress[0] != 0 || !verifyChecksum(decodedAddress)); } public static byte[] decodeBase58(String input) { if (input == null) { return null; } input = input.trim(); if (input.length() == 0) { return new byte[0]; } BigInteger resultNum = BigInteger.ZERO; int nLeadingZeros = 0; while (nLeadingZeros < input.length() && input.charAt(nLeadingZeros) == BASE58[0]) { nLeadingZeros++; } long acc = 0; int nDigits = 0; int p = nLeadingZeros; while (p < input.length()) { int v = BASE58_VALUES[input.charAt(p) & 0xff]; if (v >= 0) { acc *= 58; acc += v; nDigits++; if (nDigits == BASE58_CHUNK_DIGITS) { resultNum = resultNum.multiply(BASE58_CHUNK_MOD).add(BigInteger.valueOf(acc)); acc = 0; nDigits = 0; } p++; } else { break; } } if (nDigits > 0) { long mul = 58; while (--nDigits > 0) { mul *= 58; } resultNum = resultNum.multiply(BigInteger.valueOf(mul)).add(BigInteger.valueOf(acc)); } final int BASE58_SPACE = -2; while (p < input.length() && BASE58_VALUES[input.charAt(p) & 0xff] == BASE58_SPACE) { p++; } if (p < input.length()) { return null; } byte[] plainNumber = resultNum.toByteArray(); int plainNumbersOffs = plainNumber[0] == 0 ? 1 : 0; byte[] result = new byte[nLeadingZeros + plainNumber.length - plainNumbersOffs]; System.arraycopy(plainNumber, plainNumbersOffs, result, nLeadingZeros, plainNumber.length - plainNumbersOffs); return result; } public static boolean verifyChecksum(byte[] bytesWithChecksumm) { try { if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) { return false; } MessageDigest digestSha = MessageDigest.getInstance("SHA-256"); digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4); byte[] first = digestSha.digest(); byte[] calculatedDigest = digestSha.digest(first); boolean checksumValid = true; for (int i = 0; i < 4; i++) { if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) { checksumValid = false; } } return checksumValid; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }