Search in sources :

Example 1 with Hash

use of com.swirlds.common.crypto.Hash in project hedera-services by hashgraph.

the class RecordStreamFileParsingTest method parseSigFileV5.

@Test
void parseSigFileV5() throws Exception {
    final var streamFilePath = "src/test/resources/recordStreamTest/record0.0.3/2022-02-01T20_08_44.147325000Z.rcd";
    final File streamFile = new File(streamFilePath);
    final File sigFile = new File(streamFilePath + "_sig");
    Hash expectedEntireHash = LinkedObjectStreamUtilities.computeEntireHash(streamFile);
    Hash expectedMetaHash = LinkedObjectStreamUtilities.computeMetaHash(streamFile, Release023xStreamType.RELEASE_023x_STREAM_TYPE);
    Pair<Pair<Hash, Signature>, Pair<Hash, Signature>> parsedResult = LinkedObjectStreamUtilities.parseSigFile(sigFile, Release023xStreamType.RELEASE_023x_STREAM_TYPE);
    Hash entireHashInSig = parsedResult.getLeft().getLeft();
    Hash metaHashInSig = parsedResult.getRight().getLeft();
    assertEquals(expectedEntireHash, entireHashInSig);
    assertEquals(expectedMetaHash, metaHashInSig);
}
Also used : ImmutableHash(com.swirlds.common.crypto.ImmutableHash) Hash(com.swirlds.common.crypto.Hash) File(java.io.File) Pair(org.apache.commons.lang3.tuple.Pair) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 2 with Hash

use of com.swirlds.common.crypto.Hash in project hedera-services by hashgraph.

the class RecordStreamFileParsingTest method parseV5.

void parseV5(final String dir, final Hash expectedStartHash) throws Exception {
    final File out = new File(dir + "/out.log");
    // these files are generated with initial Hash be an empty Hash
    final File recordsDir = new File(dir);
    Iterator<SelfSerializable> iterator = LinkedObjectStreamUtilities.parseStreamDirOrFile(recordsDir, Release023xStreamType.RELEASE_023x_STREAM_TYPE);
    Hash startHash = null;
    int recordsCount = 0;
    Hash endHash = null;
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(out))) {
        while (iterator.hasNext()) {
            SelfSerializable object = iterator.next();
            if (startHash == null) {
                startHash = (Hash) object;
                writer.write(startHash.toString());
                writer.write("\n");
            } else if (!iterator.hasNext()) {
                endHash = (Hash) object;
                writer.write(endHash.toString());
                writer.write("\n");
                break;
            } else {
                assertTrue(object instanceof RecordStreamObject);
                RecordStreamObject recordStreamObject = (RecordStreamObject) object;
                writer.write(recordStreamObject.toShortString());
                writer.write("\n");
                assertNotNull(recordStreamObject.getTimestamp());
                recordsCount++;
            }
        }
    }
    // the record streams are generated with an empty startHash
    assertEquals(expectedStartHash, startHash);
    assertNotEquals(0, recordsCount);
    assertNotEquals(startHash, endHash);
}
Also used : SelfSerializable(com.swirlds.common.io.SelfSerializable) FileWriter(java.io.FileWriter) ImmutableHash(com.swirlds.common.crypto.ImmutableHash) Hash(com.swirlds.common.crypto.Hash) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 3 with Hash

use of com.swirlds.common.crypto.Hash in project hedera-services by hashgraph.

the class RecordsRunningHashLeafTest method updateRunningHashInvalidateHashTest.

@Test
void updateRunningHashInvalidateHashTest() {
    final var runningHash = new RunningHash();
    // sets Hash for the RunningHash
    runningHash.setHash(new Hash(RandomUtils.nextBytes(DigestType.SHA_384.digestLength())));
    // initializes a leaf with a RunningHash
    final var leafForTestingRunningHash = new RecordsRunningHashLeaf(runningHash);
    // digest this leaf
    CryptoFactory.getInstance().digestSync(leafForTestingRunningHash, DigestType.SHA_384);
    assertNotNull(leafForTestingRunningHash.getHash());
    // update runningHash object
    final var newRunningHash = new RunningHash();
    newRunningHash.setHash(new Hash(RandomUtils.nextBytes(DigestType.SHA_384.digestLength())));
    leafForTestingRunningHash.setRunningHash(newRunningHash);
    // the Leaf's Hash should be set to be null after updating the runningHash object
    assertNull(leafForTestingRunningHash.getHash());
}
Also used : RunningHash(com.swirlds.common.crypto.RunningHash) Hash(com.swirlds.common.crypto.Hash) RunningHash(com.swirlds.common.crypto.RunningHash) Test(org.junit.jupiter.api.Test)

Example 4 with Hash

use of com.swirlds.common.crypto.Hash in project hedera-services by hashgraph.

the class HashBench method setup.

@Setup(Level.Trial)
public void setup() {
    hashes = new Hash[256];
    Random random = new Random();
    for (int i = 0; i < hashes.length; i++) {
        byte[] hashData = new byte[48];
        random.nextBytes(hashData);
        hashes[i] = new Hash(hashData);
    }
}
Also used : Random(java.util.Random) Hash(com.swirlds.common.crypto.Hash) Setup(org.openjdk.jmh.annotations.Setup)

Example 5 with Hash

use of com.swirlds.common.crypto.Hash in project hedera-services by hashgraph.

the class VFCDataSourceLmdb method getHash.

private Hash getHash(ByteBuffer hashBytes) {
    hashBytes.rewind();
    Hash hash = new Hash(DigestType.SHA_384);
    hashBytes.get(hash.getValue());
    return hash;
}
Also used : Hash(com.swirlds.common.crypto.Hash)

Aggregations

Hash (com.swirlds.common.crypto.Hash)11 IOException (java.io.IOException)4 VirtualLeafRecord (com.swirlds.virtualmap.datasource.VirtualLeafRecord)3 Random (java.util.Random)3 ContractKey (com.hedera.services.state.virtual.ContractKey)2 ContractKeySerializer (com.hedera.services.state.virtual.ContractKeySerializer)2 ContractKeySupplier (com.hedera.services.state.virtual.ContractKeySupplier)2 ContractValue (com.hedera.services.state.virtual.ContractValue)2 ContractValueSupplier (com.hedera.services.state.virtual.ContractValueSupplier)2 ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)2 DigestType (com.swirlds.common.crypto.DigestType)2 ImmutableHash (com.swirlds.common.crypto.ImmutableHash)2 JasperDbBuilder (com.swirlds.jasperdb.JasperDbBuilder)2 VirtualDataSourceJasperDB (com.swirlds.jasperdb.VirtualDataSourceJasperDB)2 VirtualInternalRecordSerializer (com.swirlds.jasperdb.VirtualInternalRecordSerializer)2 VirtualLeafRecordSerializer (com.swirlds.jasperdb.VirtualLeafRecordSerializer)2 DataFileCommon (com.swirlds.jasperdb.files.DataFileCommon)2 VirtualDataSource (com.swirlds.virtualmap.datasource.VirtualDataSource)2 VirtualInternalRecord (com.swirlds.virtualmap.datasource.VirtualInternalRecord)2 File (java.io.File)2