forked from TrueCloudLab/frostfs-sdk-java
[#1] provide naming conventions
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
a7fab6f314
commit
bf2f19f08d
103 changed files with 416 additions and 417 deletions
12
cryptography/src/main/java/info/frostfs/sdk/ArrayHelper.java
Normal file
12
cryptography/src/main/java/info/frostfs/sdk/ArrayHelper.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
public class ArrayHelper {
|
||||
|
||||
public static byte[] concat(byte[] startArray, byte[] endArray) {
|
||||
byte[] result = new byte[startArray.length + endArray.length];
|
||||
|
||||
System.arraycopy(startArray, 0, result, 0, startArray.length);
|
||||
System.arraycopy(endArray, 0, result, startArray.length, endArray.length);
|
||||
return result;
|
||||
}
|
||||
}
|
127
cryptography/src/main/java/info/frostfs/sdk/Base58.java
Normal file
127
cryptography/src/main/java/info/frostfs/sdk/Base58.java
Normal file
|
@ -0,0 +1,127 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static info.frostfs.sdk.ArrayHelper.concat;
|
||||
import static info.frostfs.sdk.Helper.getSha256;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Base58 {
|
||||
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
|
||||
private static final char ENCODED_ZERO = ALPHABET[0];
|
||||
private static final int[] INDEXES = new int[128];
|
||||
|
||||
static {
|
||||
Arrays.fill(INDEXES, -1);
|
||||
for (int i = 0; i < ALPHABET.length; i++) {
|
||||
INDEXES[ALPHABET[i]] = i;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] base58CheckDecode(String input) {
|
||||
if (isNull(input) || input.isEmpty()) {
|
||||
throw new IllegalArgumentException("Input value is missing");
|
||||
}
|
||||
|
||||
byte[] buffer = decode(input);
|
||||
if (buffer.length < 4) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
byte[] decode = Arrays.copyOfRange(buffer, 0, buffer.length - 4);
|
||||
byte[] checksum = getSha256(getSha256(decode));
|
||||
if (!Arrays.equals(
|
||||
Arrays.copyOfRange(buffer, buffer.length - 4, buffer.length),
|
||||
Arrays.copyOfRange(checksum, 0, 4)
|
||||
)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
return decode;
|
||||
}
|
||||
|
||||
public static String base58CheckEncode(byte[] data) {
|
||||
byte[] checksum = getSha256(getSha256(data));
|
||||
var buffer = concat(data, Arrays.copyOfRange(checksum, 0, 4));
|
||||
var ret = encode(buffer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String encode(byte[] input) {
|
||||
if (input.length == 0) {
|
||||
return "";
|
||||
}
|
||||
// Count leading zeros.
|
||||
int zeros = 0;
|
||||
while (zeros < input.length && input[zeros] == 0) {
|
||||
++zeros;
|
||||
}
|
||||
// Convert base-256 digits to base-58 digits (plus conversion to ASCII characters)
|
||||
input = Arrays.copyOf(input, input.length); // since we modify it in-place
|
||||
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)];
|
||||
if (input[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
}
|
||||
// Preserve exactly as many leading encoded zeros in output as there were leading zeros in input.
|
||||
while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
|
||||
++outputStart;
|
||||
}
|
||||
while (--zeros >= 0) {
|
||||
encoded[--outputStart] = ENCODED_ZERO;
|
||||
}
|
||||
// Return encoded string (including encoded leading zeros).
|
||||
return new String(encoded, outputStart, encoded.length - outputStart);
|
||||
}
|
||||
|
||||
public static byte[] decode(String input) {
|
||||
if (input.isEmpty()) {
|
||||
return new byte[0];
|
||||
}
|
||||
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
|
||||
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;
|
||||
if (digit < 0) {
|
||||
throw new IllegalArgumentException(String.format("Invalid character in Base58: 0x%04x", (int) c));
|
||||
}
|
||||
input58[i] = (byte) digit;
|
||||
}
|
||||
// Count leading zeros.
|
||||
int zeros = 0;
|
||||
while (zeros < input58.length && input58[zeros] == 0) {
|
||||
++zeros;
|
||||
}
|
||||
// Convert base-58 digits to base-256 digits.
|
||||
byte[] decoded = new byte[input.length()];
|
||||
int outputStart = decoded.length;
|
||||
for (int inputStart = zeros; inputStart < input58.length; ) {
|
||||
decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
|
||||
if (input58[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
}
|
||||
// Ignore extra leading zeroes that were added during the calculation.
|
||||
while (outputStart < decoded.length && decoded[outputStart] == 0) {
|
||||
++outputStart;
|
||||
}
|
||||
// Return decoded data (including original number of leading zeros).
|
||||
return Arrays.copyOfRange(decoded, outputStart - zeros, decoded.length);
|
||||
}
|
||||
|
||||
private static byte divmod(byte[] number, int firstDigit, int base, int divisor) {
|
||||
// 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 temp = remainder * base + digit;
|
||||
number[i] = (byte) (temp / divisor);
|
||||
remainder = temp % divisor;
|
||||
}
|
||||
return (byte) remainder;
|
||||
}
|
||||
}
|
40
cryptography/src/main/java/info/frostfs/sdk/Helper.java
Normal file
40
cryptography/src/main/java/info/frostfs/sdk/Helper.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.Message;
|
||||
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class Helper {
|
||||
|
||||
public static byte[] getRIPEMD160(byte[] value) {
|
||||
var hash = new byte[20];
|
||||
var digest = new RIPEMD160Digest();
|
||||
digest.update(value, 0, value.length);
|
||||
digest.doFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static MessageDigest getSha256Instance() {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] getSha256(byte[] value) {
|
||||
return getSha256Instance().digest(value);
|
||||
}
|
||||
|
||||
public static ByteString getSha256(Message value) {
|
||||
return ByteString.copyFrom(getSha256(value.toByteArray()));
|
||||
}
|
||||
|
||||
public static String getHexString(byte[] array) {
|
||||
return String.format("%0" + (array.length << 1) + "x", new BigInteger(1, array));
|
||||
}
|
||||
}
|
167
cryptography/src/main/java/info/frostfs/sdk/KeyExtension.java
Normal file
167
cryptography/src/main/java/info/frostfs/sdk/KeyExtension.java
Normal file
|
@ -0,0 +1,167 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import org.bouncycastle.asn1.sec.SECNamedCurves;
|
||||
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
|
||||
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||||
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
||||
import org.bouncycastle.math.ec.ECPoint;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.ECParameterSpec;
|
||||
import java.security.spec.ECPrivateKeySpec;
|
||||
import java.security.spec.ECPublicKeySpec;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static info.frostfs.sdk.Helper.getRIPEMD160;
|
||||
import static info.frostfs.sdk.Helper.getSha256;
|
||||
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
|
||||
|
||||
public class KeyExtension {
|
||||
public static final byte NEO_ADDRESS_VERSION = 0x35;
|
||||
private static final int COMPRESSED_PUBLIC_KEY_LENGTH = 33;
|
||||
private static final int UNCOMPRESSED_PUBLIC_KEY_LENGTH = 65;
|
||||
|
||||
private static final int CHECK_SIG_DESCRIPTOR = ByteBuffer
|
||||
.wrap(getSha256("System.Crypto.CheckSig".getBytes(StandardCharsets.US_ASCII)))
|
||||
.order(ByteOrder.LITTLE_ENDIAN).getInt();
|
||||
|
||||
|
||||
public static byte[] compress(byte[] publicKey) {
|
||||
if (publicKey.length != UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Compress argument isn't uncompressed public key. Expected length=%s, actual=%s",
|
||||
UNCOMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length)
|
||||
);
|
||||
}
|
||||
|
||||
var secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
|
||||
var point = secp256R1.getCurve().decodePoint(publicKey);
|
||||
return point.getEncoded(true);
|
||||
}
|
||||
|
||||
public static byte[] getPrivateKeyFromWIF(String wif) {
|
||||
var data = Base58.base58CheckDecode(wif);
|
||||
return Arrays.copyOfRange(data, 1, data.length - 1);
|
||||
}
|
||||
|
||||
public static byte[] loadPublicKey(byte[] privateKey) {
|
||||
X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
|
||||
ECDomainParameters domain = new ECDomainParameters(
|
||||
params.getCurve(), params.getG(), params.getN(), params.getH()
|
||||
);
|
||||
ECPoint q = domain.getG().multiply(new BigInteger(1, privateKey));
|
||||
ECPublicKeyParameters publicParams = new ECPublicKeyParameters(q, domain);
|
||||
return publicParams.getQ().getEncoded(true);
|
||||
}
|
||||
|
||||
public static PrivateKey loadPrivateKey(byte[] privateKey) {
|
||||
X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
|
||||
ECDomainParameters domain = new ECDomainParameters(
|
||||
params.getCurve(), params.getG(), params.getN(), params.getH()
|
||||
);
|
||||
ECPrivateKeyParameters ecParams = new ECPrivateKeyParameters(fromUnsignedByteArray(privateKey), domain);
|
||||
|
||||
ECParameterSpec ecParameterSpec = new ECNamedCurveSpec(
|
||||
"secp256r1", params.getCurve(), params.getG(), params.getN(), params.getH()
|
||||
);
|
||||
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecParams.getD(), ecParameterSpec);
|
||||
try {
|
||||
KeyFactory kf = KeyFactory.getInstance("EC");
|
||||
return kf.generatePrivate(privateKeySpec);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static PublicKey getPublicKeyFromBytes(byte[] publicKey) {
|
||||
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Decompress argument isn't compressed public key. Expected length=%s, actual=%s",
|
||||
COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length)
|
||||
);
|
||||
}
|
||||
|
||||
X9ECParameters secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
|
||||
ECDomainParameters domain = new ECDomainParameters(
|
||||
secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(), secp256R1.getH()
|
||||
);
|
||||
var ecPoint = secp256R1.getCurve().decodePoint(publicKey);
|
||||
var publicParams = new ECPublicKeyParameters(ecPoint, domain);
|
||||
java.security.spec.ECPoint point = new java.security.spec.ECPoint(
|
||||
publicParams.getQ().getRawXCoord().toBigInteger(), publicParams.getQ().getRawYCoord().toBigInteger()
|
||||
);
|
||||
ECParameterSpec ecParameterSpec = new ECNamedCurveSpec(
|
||||
"secp256r1", secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(),
|
||||
secp256R1.getH(), secp256R1.getSeed()
|
||||
);
|
||||
ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(point, ecParameterSpec);
|
||||
|
||||
try {
|
||||
KeyFactory kf = KeyFactory.getInstance("EC");
|
||||
return kf.generatePublic(publicKeySpec);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] getScriptHash(byte[] publicKey) {
|
||||
var script = createSignatureRedeemScript(publicKey);
|
||||
|
||||
return getRIPEMD160(getSha256(script));
|
||||
}
|
||||
|
||||
public static String publicKeyToAddress(byte[] publicKey) {
|
||||
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("PublicKey isn't encoded compressed public key. Expected length=%s, actual=%s",
|
||||
COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length)
|
||||
);
|
||||
}
|
||||
|
||||
return toAddress(getScriptHash(publicKey), NEO_ADDRESS_VERSION);
|
||||
}
|
||||
|
||||
private static String toAddress(byte[] scriptHash, byte version) {
|
||||
byte[] data = new byte[21];
|
||||
data[0] = version;
|
||||
System.arraycopy(scriptHash, 0, data, 1, scriptHash.length);
|
||||
return Base58.base58CheckEncode(data);
|
||||
}
|
||||
|
||||
private static byte[] getBytes(int value) {
|
||||
byte[] buffer = new byte[4];
|
||||
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = (byte) (value >> i * 8);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private static byte[] createSignatureRedeemScript(byte[] publicKey) {
|
||||
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("PublicKey isn't encoded compressed public key. Expected length=%s, actual=%s",
|
||||
COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length)
|
||||
);
|
||||
}
|
||||
|
||||
var script = new byte[]{0x0c, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
|
||||
|
||||
script = ArrayHelper.concat(script, publicKey);
|
||||
script = ArrayHelper.concat(script, new byte[]{0x41}); //SYSCALL
|
||||
script = ArrayHelper.concat(script, getBytes(CHECK_SIG_DESCRIPTOR)); //Neo_Crypto_CheckSig
|
||||
return script;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue