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);
}
}
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;
}
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());
}
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;
}
Aggregations