Search in sources :

Example 1 with Sponge

use of com.iota.iri.hash.Sponge in project iri by iotaledger.

the class NodeIntegrationTests method setBundleHash.

public void setBundleHash(List<int[]> transactions, Curl customCurl) {
    int[] hash = new int[Curl.HASH_LENGTH];
    Sponge curl = customCurl == null ? SpongeFactory.create(SpongeFactory.Mode.CURLP81) : customCurl;
    curl.reset();
    for (int i = 0; i < transactions.size(); i++) {
        int[] t = Arrays.copyOfRange(transactions.get(i), ADDRESS_TRINARY_OFFSET, ADDRESS_TRINARY_OFFSET + ADDRESS_TRINARY_SIZE);
        int[] valueTrits = Arrays.copyOfRange(transactions.get(i), VALUE_TRINARY_OFFSET, VALUE_TRINARY_OFFSET + VALUE_TRINARY_SIZE);
        t = ArrayUtils.addAll(t, valueTrits);
        int[] tagTrits = Arrays.copyOfRange(transactions.get(i), OBSOLETE_TAG_TRINARY_OFFSET, OBSOLETE_TAG_TRINARY_OFFSET + OBSOLETE_TAG_TRINARY_SIZE);
        t = ArrayUtils.addAll(t, tagTrits);
        int[] timestampTrits = Arrays.copyOfRange(transactions.get(i), TIMESTAMP_TRINARY_OFFSET, TIMESTAMP_TRINARY_OFFSET + TIMESTAMP_TRINARY_SIZE);
        t = ArrayUtils.addAll(t, timestampTrits);
        Converter.copyTrits(i, transactions.get(i), CURRENT_INDEX_TRINARY_OFFSET, CURRENT_INDEX_TRINARY_SIZE);
        int[] currentIndexTrits = Arrays.copyOfRange(transactions.get(i), CURRENT_INDEX_TRINARY_OFFSET, CURRENT_INDEX_TRINARY_OFFSET + CURRENT_INDEX_TRINARY_SIZE);
        t = ArrayUtils.addAll(t, currentIndexTrits);
        Converter.copyTrits(transactions.size(), transactions.get(i), LAST_INDEX_TRINARY_OFFSET, LAST_INDEX_TRINARY_SIZE);
        int[] lastIndexTrits = Arrays.copyOfRange(transactions.get(i), LAST_INDEX_TRINARY_OFFSET, LAST_INDEX_TRINARY_OFFSET + LAST_INDEX_TRINARY_SIZE);
        t = ArrayUtils.addAll(t, lastIndexTrits);
        curl.absorb(t, 0, t.length);
    }
    curl.squeeze(hash, 0, hash.length);
    for (int i = 0; i < transactions.size(); i++) {
        System.arraycopy(hash, 0, transactions.get(i), BUNDLE_TRINARY_OFFSET, BUNDLE_TRINARY_SIZE);
    }
}
Also used : Sponge(com.iota.iri.hash.Sponge)

Example 2 with Sponge

use of com.iota.iri.hash.Sponge in project iri by iotaledger.

the class SignedFiles method isFileSignatureValid.

public static boolean isFileSignatureValid(String filename, String signatureFilename, String publicKey, int depth, int index) {
    int[] trits = new int[Curl.HASH_LENGTH * 3];
    Sponge curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
    try {
        // digest file
        InputStream in = SignedFiles.class.getResourceAsStream(filename);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            Converter.trits(Converter.asciiToTrytes(line), trits, 0);
            curl.absorb(trits, 0, trits.length);
            Arrays.fill(trits, 0);
        }
        // validate signature
        {
            trits = new int[Curl.HASH_LENGTH];
            curl.squeeze(trits, 0, Curl.HASH_LENGTH);
            SpongeFactory.Mode mode = SpongeFactory.Mode.CURLP81;
            int[] digests = new int[0];
            int[] bundle = ISS.normalizedBundle(trits);
            int[] root;
            int i;
            in = SignedFiles.class.getResourceAsStream(signatureFilename);
            reader = new BufferedReader(new InputStreamReader(in));
            for (i = 0; i < 3 && (line = reader.readLine()) != null; i++) {
                int[] lineTrits = Converter.allocateTritsForTrytes(line.length());
                Converter.trits(line, lineTrits, 0);
                digests = ArrayUtils.addAll(digests, ISS.digest(mode, Arrays.copyOfRange(bundle, i * ISS.NORMALIZED_FRAGMENT_LENGTH, (i + 1) * ISS.NORMALIZED_FRAGMENT_LENGTH), lineTrits));
            }
            if ((line = reader.readLine()) != null) {
                int[] lineTrits = Converter.allocateTritsForTrytes(line.length());
                Converter.trits(line, lineTrits, 0);
                root = ISS.getMerkleRoot(mode, ISS.address(mode, digests), lineTrits, 0, index, depth);
            } else {
                root = ISS.address(mode, digests);
            }
            int[] pubkeyTrits = Converter.allocateTritsForTrytes(publicKey.length());
            Converter.trits(publicKey, pubkeyTrits, 0);
            if (Arrays.equals(pubkeyTrits, root)) {
                // valid
                return true;
            }
        }
    } catch (IOException e) {
        return false;
    }
    return false;
}
Also used : Sponge(com.iota.iri.hash.Sponge)

Example 3 with Sponge

use of com.iota.iri.hash.Sponge in project iri by iotaledger.

the class TangleTest method save.

@Test
public void save() throws Exception {
    Transaction transaction = new Transaction();
    Random r = new Random();
    int[] hash = new int[Curl.HASH_LENGTH], trits = Arrays.stream(new int[TransactionViewModel.TRINARY_SIZE]).map(i -> r.nextInt(3) - 1).toArray();
    Sponge curl = SpongeFactory.create(SpongeFactory.Mode.CURLP81);
    curl.absorb(trits, 0, trits.length);
    curl.squeeze(hash, 0, Curl.HASH_LENGTH);
    transaction.bytes = Converter.allocateBytesForTrits(trits.length);
    Converter.bytes(trits, transaction.bytes);
// assertTrue("Should be a new, unique transaction", !Tangle.instance().save(transaction).get());
}
Also used : Arrays(java.util.Arrays) RocksDBPersistenceProvider(com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider) Sponge(com.iota.iri.hash.Sponge) Set(java.util.Set) Test(org.junit.Test) Random(java.util.Random) Hash(com.iota.iri.model.Hash) SpongeFactory(com.iota.iri.hash.SpongeFactory) Converter(com.iota.iri.utils.Converter) TransactionViewModelTest.getRandomTransactionTrits(com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionTrits) Curl(com.iota.iri.hash.Curl) After(org.junit.After) Transaction(com.iota.iri.model.Transaction) TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) Transaction(com.iota.iri.model.Transaction) Random(java.util.Random) Sponge(com.iota.iri.hash.Sponge) Test(org.junit.Test)

Example 4 with Sponge

use of com.iota.iri.hash.Sponge in project iri by iotaledger.

the class MapIdentityManager method verifyCredential.

private boolean verifyCredential(Account account, Credential credential) {
    if (credential instanceof PasswordCredential) {
        char[] givenPassword = ((PasswordCredential) credential).getPassword();
        String trytes = Converter.asciiToTrytes(new String(givenPassword));
        int[] in_trits = Converter.allocateTritsForTrytes(trytes.length());
        Converter.trits(trytes, in_trits, 0);
        int[] hash_trits = new int[Curl.HASH_LENGTH];
        Sponge curl;
        curl = SpongeFactory.create(SpongeFactory.Mode.CURLP81);
        curl.absorb(in_trits, 0, in_trits.length);
        curl.squeeze(hash_trits, 0, Curl.HASH_LENGTH);
        String out_trytes = Converter.trytes(hash_trits);
        char[] char_out_trytes = out_trytes.toCharArray();
        char[] expectedPassword = users.get(account.getPrincipal().getName());
        boolean verified = Arrays.equals(givenPassword, expectedPassword);
        // Password can either be clear text or the hash of the password
        if (!verified) {
            verified = Arrays.equals(char_out_trytes, expectedPassword);
        }
        return verified;
    }
    return false;
}
Also used : PasswordCredential(io.undertow.security.idm.PasswordCredential) Sponge(com.iota.iri.hash.Sponge)

Aggregations

Sponge (com.iota.iri.hash.Sponge)4 TransactionViewModel (com.iota.iri.controllers.TransactionViewModel)1 TransactionViewModelTest.getRandomTransactionTrits (com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionTrits)1 Curl (com.iota.iri.hash.Curl)1 SpongeFactory (com.iota.iri.hash.SpongeFactory)1 Hash (com.iota.iri.model.Hash)1 Transaction (com.iota.iri.model.Transaction)1 RocksDBPersistenceProvider (com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider)1 Converter (com.iota.iri.utils.Converter)1 PasswordCredential (io.undertow.security.idm.PasswordCredential)1 Arrays (java.util.Arrays)1 Random (java.util.Random)1 Set (java.util.Set)1 After (org.junit.After)1 Before (org.junit.Before)1 Test (org.junit.Test)1 TemporaryFolder (org.junit.rules.TemporaryFolder)1