The following code read a file into bytes and computes the md5sum of the bytes array. It works but I would like to find a solution in V that need less RAM. Thanks for your comments !
import os
import crypto.md5
b := os.read_bytes("file.txt") or {panic(err)}
s := md5.sum(b).hex()
println(s)
I also tried without success :
import os
import crypto.md5
import io
mut f := os.open_file("file.txt", "r")?
mut h := md5.new()
io.cp(mut f, mut h)?
s := h.sum().hex()
println(s) // does not return the correct md5sum
Alrighty. This is what you're looking for. It produces the same result as
md5sum
and is only slightly slower.block_size
is inversely related to the amount of memory used and speed at which the checksum is computed. Decreasingblock_size
will lower the memory footprint, but takes longer to compute. Increasingblock_size
has the opposite effect. I tested on a 2GB manjaro disc image and can confirm the memory usage is very low.Note: It seems this does perform noticeably slower without the
-prod
flag. The V compiler makes special optimizations in order to run faster for the production build.