- D Cookbook
- Adam D. Ruppe
- 468字
- 2021-07-16 11:50:47
Writing a digest utility
Phobos provides a package called std.digest
that offers checksum and message digest algorithms through a unified API. Here, we'll create a small MD5 utility that calculates and prints the resulting digest of a file.
How to do it…
Let's print an MD5 digest by executing the following steps:
- Create a digest object with the algorithm you need. Here, we'll use MD5 from
std.digest.md
. - Call the
start
method. - Use the
put
method, or theput
function fromstd.range
, to feed data to the digest object. - Call the
finish
method. - Convert hexadecimal data to string if you want.
The code is as follows:
void main(string[] args) { import std.digest.md; import std.stdio; import std.range; File file; if(args.length > 1) file = File(args[1], "rb"); else file = stdin; MD5 digest; digest.start(); put(digest, file.byChunk(1024)); writeln(toHexString(digest.finish())); }
How it works…
The std.digest
package in Phobos defines modules that all follow a common API. The digest structures are output ranges, which means you feed data to them by calling their put
method. Each digest algorithm is in a separate module, but they all follow the same pattern: you create the object, call start()
, put your data into it, and then call finish()
to get the result. If you want it as a printable string, call toHexString
on the result.
The put
method on the structure itself offers only one form, accepting one array of data at a time. However, the std.range
module includes additional helper functions to expand the capabilities of any output range. The std.range.put
function can also accept input ranges. The end result is we build something very similar to a Unix pipeline; put(digest, file.byChunk(1024))
will read a file, one kilobyte chunk at a time, and feed that data into the digest algorithm. The std.range.put
method looks like the following:
foreach(chunk; input) output.put(chunk);
It is a generic function that works to connect any kind of output range to any kind of input range with a matching element type.
There's more…
Phobos' digest API also provides two other ways to get digests: a convenience function if all the data is available at once, md5Of
, and the OOP API (an interface and classes) if you need to swap out implementations at runtime.
writeln("The MD5 hash of 'test' is ", toHexString(md5Of("test")));
There are also other algorithms available in std.digest
with the same API, including SHA1 and CRC.
See also
- Chapter 3, Ranges, will show you how to create your own input and output ranges that can be connected to the digest structures
- http://dlang.org/phobos/std_digest.html is the documentation of the package
- 圖解Java數據結構與算法(微課視頻版)
- Twilio Best Practices
- 編寫高質量代碼:改善C程序代碼的125個建議
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Java Web程序設計任務教程
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- INSTANT Sinatra Starter
- HTML5 APP開發從入門到精通(微課精編版)
- 代碼閱讀
- Extending Unity with Editor Scripting
- Everyday Data Structures
- Getting Started with Polymer
- FFmpeg開發實戰:從零基礎到短視頻上線
- Android 游戲開發大全(第二版)
- R語言:邁向大數據之路