Table of Contents

Class HashingContext

Provides functionality for computing cryptographic hashes chunk by chunk.

Inheritance
HashingContext

Remarks

The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. Useful for 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.

const CHUNK_SIZE = 1024

func hash_file(path):
    # Check that file exists.
    if not FileAccess.file_exists(path):
        return
    # Start an SHA-256 context.
    var ctx = HashingContext.new()
    ctx.start(HashingContext.HASH_SHA256)
    # Open the file to hash.
    var file = FileAccess.open(path, FileAccess.READ)
    # Update the context after reading each chunk.
    while file.get_position() < file.get_length():
        var remaining = file.get_length() - file.get_position()
        ctx.update(file.get_buffer(min(remaining, CHUNK_SIZE)))
    # Get the computed hash.
    var res = ctx.finish()
    # Print the result as hex string and array.
    printt(res.hex_encode(), Array(res))

Methods

finish

Closes the current context, and return the computed hash.

PackedByteArray finish

start(int)

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

int start(int type)

Parameters

type int

update(PackedByteArray)

Updates the computation with the given chunk of data.

int update(PackedByteArray chunk)

Parameters

chunk PackedByteArray