SHA-256 Merkle tree: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 1: Line 1:
{{task|Checksums}}
{{task|Checksums}}
As described in [https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html its documentation], [https://aws.amazon.com/glacier/ Amazon S3 Glacier] requires that all uploaded files come with a checksum computed as a [[wp:Merkle_tree|Merkle Tree]] using [[SHA-256]].
As described in [https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html its documentation], [https://aws.amazon.com/glacier/ Amazon S3 Glacier] requires that all uploaded files come with a checksum computed as a [[wp:Merkle_tree|Merkle Tree]] using [[wp:SHA-2|SHA-256]].


Specifically, the SHA-256 hash is computed for each 1MiB block of the file. And then, starting from the beginning of the file, the raw hashes of consecutive blocks are paired up and concatenated together, and a new hash is computed from each concatenation. Then these are paired up and concatenated and hashed, and the process continues until there is only one hash left, which is the final checksum. The hexadecimal representation of this checksum is the value that must be included with the AWS API call to upload the object (or complete a multipart upload).
Specifically, the SHA-256 hash is computed for each 1MiB block of the file. And then, starting from the beginning of the file, the raw hashes of consecutive blocks are paired up and concatenated together, and a new hash is computed from each concatenation. Then these are paired up and concatenated and hashed, and the process continues until there is only one hash left, which is the final checksum. The hexadecimal representation of this checksum is the value that must be included with the AWS API call to upload the object (or complete a multipart upload).


Implement this algorithm in your language; you can use the code from the SHA-256 task for the actual hash computations. For better manageability and portability, build it to use a smaller block size of only 1024 bytes, and execute it on [https://{{SERVERNAME}}/mw/title.png the RosettaCode title image] with that block size. The final result should be the hexadecimal digest value <tt style="font-weight:bold">a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c</tt>.
Implement this algorithm in your language; you can use the code from the [[SHA-256]] task for the actual hash computations. For better manageability and portability, build the tree using a smaller block size of only 1024 bytes, and demonstrate it on [https://{{SERVERNAME}}/mw/title.png the RosettaCode title image] with that block size. The final result should be the hexadecimal digest value <tt style="font-weight:bold">a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c</tt>.


=={{header|Raku}}==
=={{header|Raku}}==

Revision as of 04:23, 7 November 2020

Task
SHA-256 Merkle tree
You are encouraged to solve this task according to the task description, using any language you may know.

As described in its documentation, Amazon S3 Glacier requires that all uploaded files come with a checksum computed as a Merkle Tree using SHA-256.

Specifically, the SHA-256 hash is computed for each 1MiB block of the file. And then, starting from the beginning of the file, the raw hashes of consecutive blocks are paired up and concatenated together, and a new hash is computed from each concatenation. Then these are paired up and concatenated and hashed, and the process continues until there is only one hash left, which is the final checksum. The hexadecimal representation of this checksum is the value that must be included with the AWS API call to upload the object (or complete a multipart upload).

Implement this algorithm in your language; you can use the code from the SHA-256 task for the actual hash computations. For better manageability and portability, build the tree using a smaller block size of only 1024 bytes, and demonstrate it on the RosettaCode title image with that block size. The final result should be the hexadecimal digest value a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c.

Raku

<lang Raku>#!/usr/bin/env raku

  1. compute the root label for a SHA256 Merkle tree built on blocks of a given
  2. size (default 1MB) taken from the given file(s)

use Digest::SHA256::Native;

sub MAIN(Int :b(:$block-size) = 1024 * 1024, *@args) {

 my ($block, @blocks);
 my $in = @args ?? IO::CatHandle.new(@args) !! $*IN;
 @blocks.push(sha256($block)) while $block = $in.read($block-size);
 while (@blocks > 1) {
   my @out;
   for @blocks.batch(2) -> @b {
     @out.push( @b > 1 ?? sha256(@b[0] ~ @b[1]) !! @b[0] );
   }
   @blocks = @out;
 }
 say @blocks[0]».fmt('%02x').join;

}</lang>

Output:
$ sha256tree --block-size=1024 title.png
a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c