use of it.unica.tcs.lib.Hash in project balzac by balzac-lang.
the class BitcoinUtils method sha1.
public static Hash sha1(byte[] bytes) {
SHA1Digest digest = new SHA1Digest();
digest.update(bytes, 0, bytes.length);
byte[] sha1Hash = new byte[20];
digest.doFinal(sha1Hash, 0);
return new Hash(sha1Hash);
}
use of it.unica.tcs.lib.Hash in project balzac by balzac-lang.
the class BitcoinUtils method hash.
public static Hash hash(Object input, HashAlgorithm alg) {
checkArgument(input instanceof Number || input instanceof Hash || input instanceof Boolean || input instanceof String || input instanceof byte[]);
String methodName = null;
switch(alg) {
case HASH160:
methodName = "hash160";
break;
case HASH256:
methodName = "hash256";
break;
case RIPEMD160:
methodName = "ripemd160";
break;
case SHA256:
methodName = "sha256";
break;
case SHA1:
methodName = "sha1";
break;
default:
throw new IllegalArgumentException("unexpected class " + alg);
}
try {
Method method = MethodUtils.getMatchingMethod(BitcoinUtils.class, methodName, input.getClass());
return Hash.class.cast(method.invoke(null, input));
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
use of it.unica.tcs.lib.Hash in project balzac by balzac-lang.
the class ASTUtils method objectToExpression.
public Expression objectToExpression(Object value) {
if (value instanceof Long) {
NumberLiteral res = BitcoinTMFactory.eINSTANCE.createNumberLiteral();
res.setValue((Long) value);
return res;
} else if (value instanceof String) {
StringLiteral res = BitcoinTMFactory.eINSTANCE.createStringLiteral();
res.setValue((String) value);
return res;
} else if (value instanceof Boolean) {
BooleanLiteral res = BitcoinTMFactory.eINSTANCE.createBooleanLiteral();
res.setTrue((Boolean) value);
return res;
} else if (value instanceof Hash) {
HashLiteral res = BitcoinTMFactory.eINSTANCE.createHashLiteral();
res.setValue(((Hash) value).getBytes());
return res;
} else if (value instanceof DumpedPrivateKey) {
KeyLiteral res = BitcoinTMFactory.eINSTANCE.createKeyLiteral();
res.setValue(((DumpedPrivateKey) value).toBase58());
return res;
} else if (value instanceof TransactionSignature) {
SignatureLiteral res = BitcoinTMFactory.eINSTANCE.createSignatureLiteral();
res.setValue(BitcoinUtils.encode(((TransactionSignature) value).encodeToBitcoin()));
return res;
} else {
throw new IllegalStateException("Unexpected type " + value.getClass());
}
}
use of it.unica.tcs.lib.Hash in project balzac by balzac-lang.
the class BitcoinUtils method ripemd160.
public static Hash ripemd160(byte[] bytes) {
RIPEMD160Digest digest = new RIPEMD160Digest();
digest.update(bytes, 0, bytes.length);
byte[] ripmemdHash = new byte[20];
digest.doFinal(ripmemdHash, 0);
return new Hash(ripmemdHash);
}
use of it.unica.tcs.lib.Hash in project balzac by balzac-lang.
the class BitcoinUtilsTest method test_hash_refl.
@Test
public void test_hash_refl() {
HashAlgorithm[] hashClasses = new HashAlgorithm[] { HashAlgorithm.HASH160, HashAlgorithm.HASH256, HashAlgorithm.RIPEMD160, HashAlgorithm.SHA256, HashAlgorithm.SHA1 };
Object[] values = new Object[] { 1, 1L, "", true, new byte[42], new Hash(new byte[20]), new Hash(new byte[20]), new Hash(new byte[32]), new Hash(new byte[32]) };
for (HashAlgorithm hashCls : hashClasses) {
for (Object v : values) {
assertArrayEquals(executeScript(v, hashCls), BitcoinUtils.hash(v, hashCls).getBytes());
}
}
}
Aggregations