Hi,

I'm trying to make sha256 hashes on files. For testing purposes I generate a 
file with 
    
    
    dd if=/dev/zero of=file.bin bs=1K count=20
    

This is the code
    
    
    import system, nimSHA2, os, strutils
    
    proc main(): string =
       const blockSize = 8192
       var bytesRead: int = 0
       var buffer: array[blockSize, char]
       
       var f: File = open("./file.bin")
       var sha: SHA256
       sha.initSHA()
       
       bytesRead = f.readBuffer(buffer.addr, blockSize)
       
       while bytesRead > 0:
         echo bytesRead
         sha.update($buffer)
         bytesRead = f.readBuffer(buffer.addr, blockSize)
       
       let digest = sha.final()
       
       result = digest.hex()
    
    
    when isMainModule:
      echo main()
    

Hex results: 
    
    
    my code: E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
    sha256sum -b file.bin: 
cc61635da46b2c9974335ea37e0b5fd660a5c8a42a89b271fa7ec2ac4b8b26f6
    

I think the error may be at the array[blockSize, char] and $buffer conversion.

PS: I am sorry for my noob question.

Reply via email to