Skip to content

Commit

Permalink
FEAT: included function file-checksum returning checksum of a given…
Browse files Browse the repository at this point in the history
… file's content
  • Loading branch information
Oldes committed Jun 16, 2023
1 parent b624280 commit b91f645
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/mezz/mezz-shell.reb
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,24 @@ su: set-user: func[
target: file/2
]
]

file-checksum: function [
"Computes a checksum of a given file's content"
file [file!] "Using 256kB chunks"
method [word!] "One of system/catalog/checksums"
][
;; it is ok to throw an error on invalid input args
port: open join checksum:// method
file: open/read/seek file
;; but catch an error when computing the sum,
;; so we could close the file later
try [
while [not empty? bin: read/part file 262144][ write port bin ]
]
;; not using try to get none as a result in case of errors
attempt [
close file
read port
]
]

15 changes: 15 additions & 0 deletions src/tests/units/checksum-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,19 @@ AF45D2E376484031617F78D2B58A6B1B9C7EF464F5A01B47E42EC3736322445E
]
===end-group===


===start-group=== "file-checksum"
--test-- "file-checksum with small file"
--assert (file-checksum %units/files/pdf-maker-doc.pdf 'md5) == #{6F782354D64B0B09CF103A9A129E1137}
--assert (file-checksum %units/files/pdf-maker-doc.pdf 'sha1) == #{A598B252C045ABF94EE5F034798B384056C57086}
--assert (file-checksum %units/files/pdf-maker-doc.pdf 'sha256) == #{FA24645FE45C06DEB31DEC0B4478718A3ABE3F8C923A3B720B5564DAA2C9FC0F}
--test-- "file-checksum with bigger file"
--assert binary? file-checksum system/options/boot 'md5 ;; not testing result, because the binary changes.
===end-group===






~~~end-file~~~

0 comments on commit b91f645

Please sign in to comment.