Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

HashingContext

Inherits: RefCounted < Object

Provides functionality for computing cryptographic hashes chunk by chunk.

描述

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 a 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 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))

Methods

PackedByteArray

finish ( )

Error

start ( HashType type )

Error

update ( PackedByteArray chunk )


Enumerations

enum HashType:

HashType HASH_MD5 = 0

Hashing algorithm: MD5.

HashType HASH_SHA1 = 1

Hashing algorithm: SHA-1.

HashType HASH_SHA256 = 2

Hashing algorithm: SHA-256.


Method Descriptions

PackedByteArray finish ( )

Closes the current context, and return the computed hash.


Error start ( HashType type )

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


Error update ( PackedByteArray chunk )

Updates the computation with the given chunk of data.