45 lines
874 B
Nim
45 lines
874 B
Nim
import md5
|
|
import os
|
|
|
|
proc fileToMD5*(filename: string) : string =
|
|
|
|
const blockSize: int = 8192
|
|
var
|
|
c: MD5Context
|
|
d: MD5Digest
|
|
f: File
|
|
bytesRead: int = 0
|
|
buffer: array[blockSize, char]
|
|
byteTotal: int = 0
|
|
|
|
#read chunk of file, calling update until all bytes have been read
|
|
try:
|
|
f = open(filename)
|
|
|
|
md5Init(c)
|
|
bytesRead = f.readBuffer(buffer.addr, blockSize)
|
|
|
|
while bytesRead > 0:
|
|
byteTotal += bytesRead
|
|
md5Update(c, buffer, bytesRead)
|
|
bytesRead = f.readBuffer(buffer.addr, blockSize)
|
|
|
|
md5Final(c, d)
|
|
|
|
except IOError:
|
|
echo("File not found.")
|
|
finally:
|
|
if f != nil:
|
|
close(f)
|
|
|
|
result = $d
|
|
|
|
when isMainModule:
|
|
|
|
if paramCount() > 0:
|
|
let arguments = commandLineParams()
|
|
echo("MD5: ", fileToMD5(arguments[0]))
|
|
else:
|
|
echo("Must pass filename.")
|
|
quit(-1)
|