Context to compute cryptographic hashes over multiple iterations.
    public class HashingContext : Reference

The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. This is useful for example when computing hashes of big files (so you don’t have to load them all in memory), network streams, and data streams in general (so you don’t have to hold buffers).

The HashType enum shows the supported hashing algorithms.

[codeblock]

const CHUNK_SIZE = 1024

func hash_file(path):

var ctx = HashingContext.new()

var file = File.new()

Start a SHA-256 context.

ctx.start(HashingContext.HASH_SHA256)

Check that file exists.

if not file.file_exists(path):

return

Open the file to hash.

file.open(path, File.READ)

Update the context after reading each chunk.

while not file.eof_reached():

ctx.update(file.get_buffer(CHUNK_SIZE))

Get the computed hash.

var res = ctx.finish()

Print the result as hex string and array.

printt(res.hex_encode(), Array(res))

[/codeblock]

Inheritance Chain

Constructors

    public HashingContext()

Methods

    public byte[] Finish()

Closes the current context, and return the computed hash.

    public Error Start(HashType type)

Starts a new hash computation of the given type (e.g. [constant HASH_SHA256] to start computation of a SHA-256).

    public Error Update(byte[] chunk)

Updates the computation with the given chunk of data.

Inner Types

HashType

Name Value Description
Md5 0 Hashing algorithm: MD5.
Sha1 1 Hashing algorithm: SHA-1.
Sha256 2 Hashing algorithm: SHA-256.
Tags: