SHA-256 Merkle tree: Difference between revisions

Line 60:
}</lang>
 
{{out}}
<pre>
a4f902cf9d51fe51eda156a6792e1445dff65edf3a217a1f3334cc9cf1495c2c
</pre>
 
=={{header|Phix}}==
<lang Phix>include builtins\libcurl.e
include builtins\sha256.e
 
constant ONE_MB = 1024 * 1024
 
function merkle(string filename, url, integer block_size=ONE_MB)
if not file_exists(filename) then
printf(1,"Downloading %s...\n",{filename})
CURLcode res = curl_easy_get_file(url,"",filename) -- (no proxy)
if res!=CURLE_OK then
string error = sprintf("%d",res)
if res=CURLE_COULDNT_RESOLVE_HOST then
error &= " [CURLE_COULDNT_RESOLVE_HOST]"
end if
crash("Error %s downloading file\n", {error})
end if
end if
string data = get_text(filename)
sequence blocks = {}
for i=1 to length(data) by block_size do
blocks = append(blocks,sha256(data[i..min(i+block_size-1,length(data))]))
end for
while length(blocks)>1 do
integer l = 0
for i=1 to length(blocks) by 2 do
l += 1
blocks[l] = iff(i<length(blocks)?sha256(blocks[i]&blocks[i+1])
:blocks[i])
end for
blocks = blocks[1..l]
end while
return blocks[1]
end function
 
function asHex(string s)
string res = ""
for i=1 to length(s) do
res &= sprintf("%02X",s[i])
end for
return res
end function
 
printf(1,"%s\n",asHex(merkle("title.png", "https://rosettacode.org/mw/title.png", 1024)))</lang>
{{out}}
<pre>
7,806

edits