Search in sources :

Example 1 with Hash

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);
}
Also used : SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) Hash(it.unica.tcs.lib.Hash) Sha256Hash(org.bitcoinj.core.Sha256Hash)

Example 2 with Hash

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);
    }
}
Also used : Method(java.lang.reflect.Method) Hash(it.unica.tcs.lib.Hash) Sha256Hash(org.bitcoinj.core.Sha256Hash) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Hash

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());
    }
}
Also used : BooleanLiteral(it.unica.tcs.bitcoinTM.BooleanLiteral) TransactionSignature(org.bitcoinj.crypto.TransactionSignature) SignatureLiteral(it.unica.tcs.bitcoinTM.SignatureLiteral) Hash(it.unica.tcs.lib.Hash) SigHash(org.bitcoinj.core.Transaction.SigHash) StringLiteral(it.unica.tcs.bitcoinTM.StringLiteral) HashLiteral(it.unica.tcs.bitcoinTM.HashLiteral) KeyLiteral(it.unica.tcs.bitcoinTM.KeyLiteral) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) NumberLiteral(it.unica.tcs.bitcoinTM.NumberLiteral)

Example 4 with Hash

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);
}
Also used : RIPEMD160Digest(org.spongycastle.crypto.digests.RIPEMD160Digest) Hash(it.unica.tcs.lib.Hash) Sha256Hash(org.bitcoinj.core.Sha256Hash)

Example 5 with Hash

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());
        }
    }
}
Also used : Hash(it.unica.tcs.lib.Hash) HashAlgorithm(it.unica.tcs.lib.Hash.HashAlgorithm) Test(org.junit.Test)

Aggregations

Hash (it.unica.tcs.lib.Hash)5 Sha256Hash (org.bitcoinj.core.Sha256Hash)3 BooleanLiteral (it.unica.tcs.bitcoinTM.BooleanLiteral)1 HashLiteral (it.unica.tcs.bitcoinTM.HashLiteral)1 KeyLiteral (it.unica.tcs.bitcoinTM.KeyLiteral)1 NumberLiteral (it.unica.tcs.bitcoinTM.NumberLiteral)1 SignatureLiteral (it.unica.tcs.bitcoinTM.SignatureLiteral)1 StringLiteral (it.unica.tcs.bitcoinTM.StringLiteral)1 HashAlgorithm (it.unica.tcs.lib.Hash.HashAlgorithm)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)1 SigHash (org.bitcoinj.core.Transaction.SigHash)1 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)1 Test (org.junit.Test)1 RIPEMD160Digest (org.spongycastle.crypto.digests.RIPEMD160Digest)1 SHA1Digest (org.spongycastle.crypto.digests.SHA1Digest)1