Checksumming with External Utilities

With the checksums.process vocabulary any console utility can be used to checksum data, provided it supports a certain interface: it should accept input data on STDIN and output result to STDOUT. The output should consist of the hexadecimal checksum string, terminated with a blank? character. For instance, all the checksums from the GNU CoreUtils package support this mode of operation as the default.

The checksum-process tuple holds a launch descriptor (see Launch descriptors) of the utility, e.g. "sha1sum". When the initialize-checksum-state method is called on it, a new instance of the checksum utility is started in the background. In Factor it is represented by the process-state tuple. You can then use add-checksum-bytes to stream data to it. When done, call get-checksum to read the resulting checksum and dispose of the tuple in one step. If you want to cancel the work without calling get-checksum, you must dispose of the tuple so that the underlying process is terminated.

The checksum-bytes and the checksum-stream methods encapsulate the above protocol, including instantiation and disposal of the process-state tuple.

USING: byte-arrays checksums checksums.process ; "test" >byte-array "sha1sum" <checksum-process> checksum-bytes .
B{ 169 74 143 229 204 177 155 166 28 76 8 115 211 145 233 135 152 47 187 211 }

USING: checksums checksums.common checksums.process io io.encodings.binary namespaces ; "LICENSE.txt" binary [ input-stream get "sha1sum" <checksum-process> checksum-stream ] with-file-reader .
B{ 125 80 102 9 175 178 81 111 33 59 33 149 187 70 193 32 81 188 89 156 }