The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Digest::SHA256 - a module that implements the NIST SHA-256/384/512 hash.


 use Digest::SHA256;

 $context = new Digest::SHA256::new($size);

 $digest = $context->digest();
 $string = $context->hexdigest();

 $digest = $context->hash($string);
 $string = $context->hexhash($string);


The sha256 module allows you to use the NIST SHA 256/384/512 hash algorithm.

A new sha256 context object is created with the new operation followed by a digest size which may be either 256, 384, or 512 bits. Multiple simultaneous digest context can be maintained if desired. The context is updated with the add operation which adds the strings contained in the LIST parameter.

The final message digest value is returned by the digest operation as a 32-, 48-, or 64-byte binary string. This operation delivers the result of operations since the last new or reset operation. Once the operation has been performed, the context must be reset before being used to calculate another digest value.

Several convenience methods are also provided. The addfile operation takes an open file-handle and reads it until end-of file in 8192-byte blocks adding the contents to the context. The hexdigest operation calls digest and returns the result as a printable string of hexadecimal digits in eight-digit groups. The hash operation performs the complete series of steps: reset, add, digest on the supplied scalar value, and returns the result as a 32-, 48-, or 64-byte binary string. The hexhash operation does the same thing, but returns the result in the format of the hexdigest operation.


SHA-256/384/512 C code and Perl interface by Rafael R. Sevilla, based on specification paper provided by NIST.

For more information on this algorithm see: