Sha256 Hashes in D Programming Language
Here’s the translation of the SHA256 hashes example from Go to D Programming Language, formatted in Markdown suitable for Hugo:
SHA256 hashes are frequently used to compute short identities for binary or text blobs. For example, TLS/SSL certificates use SHA256 to compute a certificate’s signature. Here’s how to compute SHA256 hashes in D.
D implements several hash functions in the std.digest
module.
Running the program computes the hash and prints it in a human-readable hex format.
You can compute other hashes using a similar pattern to the one shown above. For example, to compute SHA512 hashes, you can use SHA512Digest
from the same std.digest.sha
module.
Note that if you need cryptographically secure hashes, you should carefully research hash strength!
In this D version:
- We import
std.digest.sha
which provides SHA hash functions. - We create a
SHA256Digest
object instead of using aNew()
function. - We use the
put
method to add data to the hash, which can accept aubyte[]
(unsigned byte array). We cast our string to this type. - We use the
finish()
method to get the final hash value as aubyte[]
. - We use
writefln
with a special format string"%(%02x%)"
to print each byte of the result as a two-digit hexadecimal number.
The overall structure and functionality remain the same as the original example, adapted to D’s syntax and standard library.