SHA-256 Merkle tree: Difference between revisions

m
(Initial Haskell version.)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 7:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program merkleRoot64.s */
Line 695:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
<pre>
Start hash number : 22
Line 702:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program merkleRoot.s */
Line 1,313:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Start hash number : 22
Line 1,320:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
Line 1,391:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,400:
=={{header|C++}}==
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 1,499:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,510:
{{libheader| DCPsha256}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program SHA256_Merkle_tree;
 
Line 1,606:
writeln(Merkle_tree('title.png'));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>A4F902CF9D51FE51EDA156A6792E1445DFF65EDF3A217A1F3334CC9CF1495C2C</pre>
Line 1,612:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: checksums checksums.sha fry grouping io
io.encodings.binary io.files kernel make math math.parser
namespaces sequences ;
Line 1,638:
bytes>hex-string ;
 
"title.png" 1024 merkle-hash print</langsyntaxhighlight>
{{out}}
<pre>
Line 1,645:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,696:
fmt.Printf("%x", hashes[0])
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,704:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE BangPatterns, LambdaCase #-}
 
import Control.Monad (mfilter)
Line 1,768:
Nothing -> putStrLn "No input to hash"
Just h -> printByteStringHex h
_ -> putStrLn "Argument usage: chunk-size"</langsyntaxhighlight>
 
{{out}}
Line 1,776:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.*;
import java.security.*;
import java.util.*;
Line 1,833:
return digests.get(0);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,841:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using SHA
 
function merkletree(filename="title.png", blocksize=1024)
Line 1,856:
 
println(merkletree())
</langsyntaxhighlight>{{out}}
<pre>a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">data=Import["https://rosettacode.org/mw/title.png","Byte"];
parts=Hash[ByteArray[#],"SHA256","ByteArray"]&/@Partition[data,UpTo[1024]];
parts=NestWhile[If[Length[#]==2,Hash[Join@@#,"SHA256","ByteArray"],First[#]]&/@Partition[#,UpTo[2]]&,parts,Length[#]>1&];
StringJoin[IntegerString[Normal[First[parts]],16]]</langsyntaxhighlight>
{{out}}
<pre>a4f92cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c</pre>
Line 1,870:
{{libheader|nimcrypto}}
To compute the digests of file blocks, we use the procedure “digest” which accepts the address of a byte array and a byte count. To compute the digests of pairs of digests, we use instead a SHA256 context and the procedures “update” and “finish”, which avoids a copy in an intermediate buffer.
<syntaxhighlight lang="nim">
<lang Nim>
import nimcrypto
 
Line 1,899:
hashes= newHashes
 
echo hashes[0]</langsyntaxhighlight>
 
{{out}}
Line 1,907:
==={{header|Free Pascal}}===
minimal modified [http://rosettacode.org/wiki/SHA-256_Merkle_tree#Delphi Delphi] version
<langsyntaxhighlight lang="pascal">
program SHA256_Merkle_tree;
{$IFDEF WINDOWS}
Line 2,021:
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,028:
=={{header|Perl}}==
{{Trans|Raku}}
<langsyntaxhighlight lang="perl"># 20210222 Perl programming solution
 
use strict;
Line 2,048:
}
 
print unpack ( 'H*', $blocks[0] ) , "\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 2,056:
=={{header|Phix}}==
{{libheader|Phix/libcurl}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 2,101:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">asHex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">merkle</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"title.png"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"https://rosettacode.org/mw/title.png"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1024</span><span style="color: #0000FF;">)))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,110:
This version attempts to combine blocks as soon as possible to minimize the memory footprint.
 
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
# compute the root label for a SHA256 Merkle tree built on blocks of a given
# size (default 1MB) taken from the given file(s)
Line 2,152:
 
argh.dispatch_command(main)
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,160:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Digest::SHA256::Native;
 
unit sub MAIN(Int :b(:$block-size) = 1024 × 1024, *@args);
Line 2,172:
}
 
say @blocks[0]».fmt('%02x').join;</langsyntaxhighlight>
 
{{Out}}
Line 2,179:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">extern crate crypto;
 
use crypto::digest::Digest;
Line 2,241:
Err(error) => eprintln!("I/O error: {}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,253:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./crypto" for Sha256, Bytes
import "./seq" for Lst
import "./str" for Str
import "./fmt" for Conv
var bytes = File.read("title.png").bytes.toList
Line 2,287:
hashes = hashes2
}
System.print(Bytes.toHexString(hashes[0]))</langsyntaxhighlight>
 
{{out}}
9,482

edits