Skip to content

Commit

Permalink
sup ada
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpengliu committed Mar 2, 2020
1 parent 37f289a commit 7cc6d71
Show file tree
Hide file tree
Showing 34 changed files with 7,945 additions and 0 deletions.
110 changes: 110 additions & 0 deletions core/src/main/java/com/raugfer/crypto/base32.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.raugfer.crypto;

import java.math.BigInteger;

public class base32 {

public static final String digits = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";

public static String encode(byte[] b) {
StringBuilder sb = new StringBuilder();
BigInteger n = binint.b2n(b);
BigInteger base = BigInteger.valueOf(digits.length());
while (n.compareTo(BigInteger.ZERO) > 0) {
BigInteger r = n.mod(base);
n = n.divide(base);
sb.append(digits.charAt(r.intValue()));
}
int l = (8 * b.length) / 5 + ((8 * b.length) % 5 > 0 ? 1 : 0);
assert l >= sb.length();
while (sb.length() < l) sb.append(digits.charAt(0));
return sb.reverse().toString();
}

public static byte[] decode(String w) {
BigInteger v = BigInteger.ZERO;
BigInteger base = BigInteger.valueOf(digits.length());
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
int index = digits.indexOf(c);
if (index < 0) throw new IllegalArgumentException("Invalid input");
v = v.multiply(base).add(BigInteger.valueOf(index));
}
byte[] b = binint.n2b(v);
int l = (5 * w.length()) / 8 + ((5 * w.length()) % 8 > 0 ? 1 : 0);
assert l >= b.length;
int zeros = l - b.length;
if (zeros > 0) {
byte[] t = new byte[zeros + b.length];
System.arraycopy(b, 0, t, zeros, b.length);
b = t;
}
return b;
}

public static String check_encode(byte[] b) {
return check_encode(b, new byte[]{ }, new byte[]{ });
}

public static String check_encode(byte[] b, byte[] prefix, byte[] suffix) {
return check_encode(b, prefix, suffix, null);
}

public static String check_encode(byte[] b, byte[] prefix, byte[] suffix, hashing.hashfun f) {
if (f == null) f = base32::_rev_blake2b_5;
b = bytes.concat(prefix, b, suffix);
byte[] h = f.hash(b);
b = bytes.concat(b, h);
return encode(b);
}

public static triple<byte[], byte[], byte[]> check_decode(String w) {
return check_decode(w, 0, 0);
}

public static triple<byte[], byte[], byte[]> check_decode(String w, int prefix_len, int suffix_len) {
return check_decode(w, prefix_len, suffix_len, 5, null);
}

public static triple<byte[], byte[], byte[]> check_decode(String w, int prefix_len, int suffix_len, int hash_len, hashing.hashfun f) {
if (f == null) f = base32::_rev_blake2b_5;
byte[] b = decode(w);
if (b.length < prefix_len + suffix_len + hash_len) throw new IllegalArgumentException("Invalid length");
if (hash_len == 0) hash_len = -b.length;
byte[] h = bytes.sub(b, -hash_len);
b = bytes.sub(b, 0, -hash_len);
if (!bytes.equ(h, f.hash(b))) {
byte[] t = bytes.sub(b, 0 ,1);
b = bytes.sub(b, 1);
if (!bytes.equ(t, new byte[]{ 0x00 }) || !bytes.equ(h, f.hash(b))) throw new IllegalArgumentException("Invalid hash");
}
byte[] prefix = bytes.sub(b, 0, prefix_len);
b = bytes.sub(b, prefix_len);
if (suffix_len == 0) suffix_len = -b.length;
byte[] suffix = bytes.sub(b, -suffix_len);
b = bytes.sub(b, 0, -suffix_len);
return new triple<>(b, prefix, suffix);
}

public static String translate(String w, String from_digits, String to_digits) {
if (from_digits == null) from_digits = digits;
if (to_digits == null) to_digits = digits;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
int index = from_digits.indexOf(c);
if (index < 0) throw new IllegalArgumentException("Invalid input");
sb.append(to_digits.charAt(index));
}
return sb.toString();
}

static byte[] _rev_blake2b_5(byte[] b) {
return bytes.rev(hashing.blake2b(b, 5));
}

static byte[] _rev_crc16(byte[] b) {
return bytes.rev(crc16.crc16xmodem(b));
}

}
121 changes: 121 additions & 0 deletions core/src/main/java/com/raugfer/crypto/base58.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.raugfer.crypto;

import java.math.BigInteger;

public class base58 {

public static final String digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

public static String encode(byte[] b) {
StringBuilder sb = new StringBuilder();
BigInteger n = binint.b2n(b);
BigInteger base = BigInteger.valueOf(digits.length());
while (n.compareTo(BigInteger.ZERO) > 0) {
BigInteger r = n.mod(base);
n = n.divide(base);
sb.append(digits.charAt(r.intValue()));
}
for (byte c : b) {
if (c != 0) break;
sb.append(digits.charAt(0));
}
return sb.reverse().toString();
}

public static byte[] decode(String w) {
BigInteger v = BigInteger.ZERO;
BigInteger base = BigInteger.valueOf(digits.length());
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
int index = digits.indexOf(c);
if (index < 0) throw new IllegalArgumentException("Invalid input");
v = v.multiply(base).add(BigInteger.valueOf(index));
}
byte[] b = binint.n2b(v);
int zeros = 0;
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
if (c != digits.charAt(0)) break;
zeros++;
}
if (zeros > 0) {
byte[] t = new byte[zeros + b.length];
System.arraycopy(b, 0, t, zeros, b.length);
b = t;
}
return b;
}

public static String check_encode(byte[] b) {
return check_encode(b, new byte[]{ }, new byte[]{ });
}

public static String check_encode(byte[] b, byte[] prefix, byte[] suffix) {
return check_encode(b, prefix, suffix, null);
}

public static String check_encode(byte[] b, byte[] prefix, byte[] suffix, hashing.hashfun f) {
if (f == null) f = base58::_sub_hash256_0_4;
b = bytes.concat(prefix, b, suffix);
byte[] h = f.hash(b);
b = bytes.concat(b, h);
return encode(b);
}

public static triple<byte[], byte[], byte[]> check_decode(String w) {
return check_decode(w, 0, 0);
}

public static triple<byte[], byte[], byte[]> check_decode(String w, int prefix_len, int suffix_len) {
return check_decode(w, prefix_len, suffix_len, 4, null);
}

public static triple<byte[], byte[], byte[]> check_decode(String w, int prefix_len, int suffix_len, int hash_len, hashing.hashfun f) {
if (f == null) f = base58::_sub_hash256_0_4;
byte[] b = decode(w);
if (b.length < prefix_len + hash_len) throw new IllegalArgumentException("Invalid length");
if (hash_len == 0) hash_len = -b.length;
byte[] h = bytes.sub(b, -hash_len);
b = bytes.sub(b, 0, -hash_len);
if (!bytes.equ(h, f.hash(b))) throw new IllegalArgumentException("Invalid hash");
byte[] prefix = bytes.sub(b, 0, prefix_len);
b = bytes.sub(b, prefix_len);
if (suffix_len == 0) suffix_len = -b.length;
byte[] suffix = bytes.sub(b, -suffix_len);
b = bytes.sub(b, 0, -suffix_len);
return new triple<>(b, prefix, suffix);
}

public static String translate(String w, String from_digits, String to_digits) {
if (from_digits == null) from_digits = digits;
if (to_digits == null) to_digits = digits;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < w.length(); i++) {
char c = w.charAt(i);
int index = from_digits.indexOf(c);
if (index < 0) throw new IllegalArgumentException("Invalid input");
sb.append(to_digits.charAt(index));
}
return sb.toString();
}

static byte[] _sub_hash256_0_4(byte[] b) {
return bytes.sub(hashing.hash256(b), 0, 4);
}
static byte[] _sub_ripemd160_0_4(byte[] b) {
return bytes.sub(hashing.ripemd160(b), 0, 4);
}
static byte[] _sub_blake1s_0_4(byte[] b) {
return bytes.sub(hashing.blake1s(b), 0, 4);
}
static byte[] _sub_blake256_0_4(byte[] b) {
return bytes.sub(hashing.blake256(b), 0, 4);
}
static byte[] _sub_securehash_0_4(byte[] b) {
return bytes.sub(hashing.securehash(b), 0, 4);
}
static byte[] _concat_crc32_5(byte[] b) {
return bytes.concat(new byte[]{ (byte)0x1a }, crc32.crc32xmodem(bytes.sub(b, 5)));
}

}
75 changes: 75 additions & 0 deletions core/src/main/java/com/raugfer/crypto/binint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.raugfer.crypto;

import java.math.BigInteger;

public class binint {

private final static String digits = "0123456789abcdef";

public static byte[] h2b(String s) {
int length = s.length();
if (length % 2 != 0) {
s = "0" + s;
length++;
}
byte[] b = new byte[length / 2];
for (int i = 0; i < b.length; i++) {
int p1 = digits.indexOf(Character.toLowerCase(s.charAt(2*i+0)));
if (p1 < 0) throw new IllegalArgumentException("Invalid input");
int p2 = digits.indexOf(Character.toLowerCase(s.charAt(2*i+1)));
if (p2 < 0) throw new IllegalArgumentException("Invalid input");
b[i] = (byte)((p1 << 4) | (p2 << 0));
}
return b;
}

public static String b2h(byte[] b) {
if (b.length == 0) return "0";
char[] c = new char[2*b.length];
for (int i = 0; i < b.length; i++) {
c[2*i+0] = digits.charAt((b[i] >> 4) & 0x0f);
c[2*i+1] = digits.charAt((b[i] >> 0) & 0x0f);
}
return new String(c);
}

public static BigInteger b2n(byte[] b) {
if (b.length == 0 || b[0] < 0) {
byte[] t = new byte[b.length + 1];
System.arraycopy(b, 0, t, 1, b.length);
b = t;
}
return new BigInteger(b);
}

public static byte[] n2b(BigInteger n) {
return n2b(n, 0);
}

public static byte[] n2b(BigInteger n, int length) {
if (n.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("Negative number");
byte[] b = n.toByteArray();
if (b[0] == 0) {
byte[] t = new byte[b.length - 1];
System.arraycopy(b, 1, t, 0, t.length);
b = t;
}
if (length > b.length) {
byte[] t = new byte[length];
System.arraycopy(b, 0, t, length - b.length, b.length);
b = t;
}
return b;
}

public static BigInteger h2n(String s) { return b2n(h2b(s)); }

public static String n2h(BigInteger n) {
return n2h(n, 0);
}

public static String n2h(BigInteger n, int length) {
return b2h(n2b(n, length));
}

}
Loading

0 comments on commit 7cc6d71

Please sign in to comment.