From cc449f93045fbde174bc3573b2e0c2395ac81108 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 2 Jul 2021 08:54:05 +0100 Subject: [PATCH] Added DataFile.chunkHashes() method which appends all hashes into a single byte array (32 bytes / 256 bits each). --- .../java/org/qortal/storage/DataFile.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/org/qortal/storage/DataFile.java b/src/main/java/org/qortal/storage/DataFile.java index 1e2eb4e5..6f82799f 100644 --- a/src/main/java/org/qortal/storage/DataFile.java +++ b/src/main/java/org/qortal/storage/DataFile.java @@ -369,6 +369,31 @@ public class DataFile { return null; } + public byte[] chunkHashes() { + if (this.chunks != null && this.chunks.size() > 0) { + // Return null if we only have one chunk, with the same hash as the parent + if (this.digest().equals(this.chunks.get(0).digest())) { + return null; + } + + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + for (DataFileChunk chunk : this.chunks) { + byte[] chunkHash = chunk.digest(); + if (chunkHash.length != 32) { + LOGGER.info("Invalid chunk hash length: {}", chunkHash.length); + throw new IllegalStateException("Invalid chunk hash length"); + } + outputStream.write(chunk.digest()); + } + return outputStream.toByteArray(); + } catch (IOException e) { + return null; + } + } + return null; + } + public String base58Digest() { if (this.digest() != null) { return Base58.encode(this.digest());