Sha256 Hashes in Julia

Here’s the translation of the SHA256 Hashes example from Go to Julia, formatted in Markdown suitable for Hugo:

Julia implements several hash functions in various cryptography packages. Here’s how to compute SHA256 hashes in Julia.

using SHA
using Printf

function main()
    s = "sha256 this string"

    # Here we compute the hash directly
    hash = sha256(s)

    println(s)
    @printf("%x\n", hash)
end

main()

In Julia, we use the SHA package to compute cryptographic hashes. Unlike Go, we don’t need to create a new hash object and write to it; we can directly compute the hash of a string or byte array using the sha256() function.

Running the program computes the hash and prints it in a human-readable hex format.

$ julia sha256-hashes.jl
sha256 this string
1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a...

You can compute other hashes using a similar pattern to the one shown above. For example, to compute SHA512 hashes, you can use the sha512() function from the same SHA package.

Note that if you need cryptographically secure hashes, you should carefully research hash strength!

In Julia, the SHA package provides a convenient way to compute various cryptographic hashes. The package includes functions for SHA-1, SHA-2 (224, 256, 384, 512), and SHA-3, among others. These functions can be used directly on strings or byte arrays, making the process of computing hashes straightforward and efficient.