This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
287 additions
and
110 deletions.
There are no files selected for viewing
324 changes: 220 additions & 104 deletions
324
syncany-util/src/main/java/org/syncany/util/Base58.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,233 @@ | ||
/* | ||
* Syncany, www.syncany.org | ||
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com> | ||
/** | ||
* Copyright 2011 Google Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.syncany.util; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* base58 encoding | ||
* @author Vincent Wiencek <vwiencek@gmail.com> | ||
* <p>Base58 is a way to encode Bitcoin addresses as numbers and letters. Note that this is not the same base58 as used by | ||
* Flickr, which you may see reference to around the internet.</p> | ||
* | ||
* <p>You may instead wish to work with {@link VersionedChecksummedBytes}, which adds support for testing the prefix | ||
* and suffix bytes commonly found in addresses.</p> | ||
* | ||
* <p>Satoshi says: why base-58 instead of standard base-64 encoding?<p> | ||
* | ||
* <ul> | ||
* <li>Don't want 0OIl characters that look the same in some fonts and | ||
* could be used to create visually identical looking account numbers.</li> | ||
* <li>A string with non-alphanumeric characters is not as easily accepted as an account number.</li> | ||
* <li>E-mail usually won't line-break if there's no punctuation to break at.</li> | ||
* <li>Doubleclicking selects the whole number as one word if it's all alphanumeric.</li> | ||
* </ul> | ||
*/ | ||
public class Base58 { | ||
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray(); | ||
private static final int[] INDEXES = new int[128]; | ||
private static final MessageDigest digest; | ||
static { | ||
try { | ||
digest = MessageDigest.getInstance("SHA-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); // Can't happen. | ||
} | ||
} | ||
|
||
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) { | ||
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 RuntimeException("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) { | ||
return new BigInteger(1, decode(input)); | ||
} | ||
|
||
/** | ||
* Uses the checksum in the last 4 bytes of the decoded data to verify the rest are correct. The checksum is | ||
* removed from the returned data. | ||
* | ||
*/ | ||
public static byte[] decodeChecked(String input) { | ||
byte tmp [] = decode(input); | ||
if (tmp.length < 4) | ||
throw new RuntimeException("Input too short"); | ||
byte[] bytes = copyOfRange(tmp, 0, tmp.length - 4); | ||
byte[] checksum = copyOfRange(tmp, tmp.length - 4, tmp.length); | ||
|
||
tmp = doubleDigest(bytes); | ||
byte[] hash = copyOfRange(tmp, 0, 4); | ||
if (!Arrays.equals(checksum, hash)) | ||
throw new RuntimeException("Checksum does not validate"); | ||
|
||
return bytes; | ||
} | ||
|
||
/** | ||
* See {@link Utils#doubleDigest(byte[], int, int)}. | ||
*/ | ||
public static byte[] doubleDigest(byte[] input) { | ||
return doubleDigest(input, 0, input.length); | ||
} | ||
|
||
/** | ||
* Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again. This is | ||
* standard procedure in Bitcoin. The resulting hash is in big endian form. | ||
*/ | ||
public static byte[] doubleDigest(byte[] input, int offset, int length) { | ||
synchronized (digest) { | ||
digest.reset(); | ||
digest.update(input, offset, length); | ||
byte[] first = digest.digest(); | ||
return digest.digest(first); | ||
} | ||
} | ||
|
||
// | ||
// 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); | ||
|
||
private static final String Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
private static final BigInteger Base58 = new BigInteger("58", 10); | ||
|
||
public static String encode(byte[] indata) { | ||
byte[] data = new byte[indata.length]; | ||
System.arraycopy(indata, 0, data, 0, data.length); | ||
// convert big endian data to little endian | ||
int len = data.length; | ||
int mid = len / 2; | ||
int i; | ||
for (i = 0; i < mid; i++) { | ||
byte tmp = data[i]; | ||
data[i] = data[len - 1 - i]; | ||
data[len - 1 - i] = tmp; | ||
} | ||
BigInteger value = new BigInteger(1, data); | ||
StringBuilder result = new StringBuilder(); | ||
|
||
// divide until zero | ||
while (value.compareTo(BigInteger.ZERO) > 0) { | ||
BigInteger[] qr = value.divideAndRemainder(Base58); | ||
value = qr[0]; | ||
BigInteger rem = qr[1]; | ||
result.append(Alphabet.charAt(rem.intValue())); | ||
} | ||
// append leading zeros | ||
for (i = 0; i < len; i++) { | ||
if (data[i] == 0) { | ||
result.append(Alphabet.charAt(0)); | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
// reverse to big endian | ||
len = result.length(); | ||
mid = len / 2; | ||
for (i = 0; i < mid; i++) { | ||
char ch1 = result.charAt(i); | ||
char ch2 = result.charAt(len - 1 - i); | ||
result.setCharAt(len - 1 - i, ch1); | ||
result.setCharAt(i, ch2); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
public static byte[] decode(String data) { | ||
BigInteger value = BigInteger.ZERO; | ||
BigInteger character; | ||
// eat spaces | ||
data = data.trim(); | ||
// encode to big number | ||
int i; | ||
int len = data.length(); | ||
for (i = 0; i < len; i++) { | ||
int pos = Alphabet.indexOf(data.charAt(i)); | ||
if (pos < 0) { // not a valid char | ||
return null; | ||
} | ||
character = new BigInteger(String.valueOf(pos), 10); | ||
value = value.multiply(Base58); | ||
value = value.add(character); | ||
} | ||
|
||
byte[] dec = value.toByteArray(); | ||
int declen = dec.length; | ||
int decoff = 0; | ||
// strip sign byte | ||
if (dec.length > 2 && dec[0] == 0 && (dec[1] & 0x80) == 0x80) { | ||
declen--; | ||
decoff++; | ||
} | ||
// count leading zeros | ||
int n = 0; | ||
for (i = 0; i < len; i++) { | ||
if (data.charAt(i) == Alphabet.charAt(0)) { | ||
n++; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
// reverse result at the right place | ||
byte[] result = new byte[declen + n]; | ||
for (i = 0; i < declen; i++) { | ||
result[i] = dec[(declen - 1 - i) + decoff]; | ||
} | ||
return result; | ||
} | ||
return range; | ||
} | ||
} |
Oops, something went wrong.