HashingContext

继承: 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 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))

    public const int ChunkSize = 1024;
    
    public void HashFile(string path)
    {
        // Check that file exists.
        if (!FileAccess.FileExists(path))
        {
            return;
        }
        // Start an SHA-256 context.
        var ctx = new HashingContext();
        ctx.Start(HashingContext.HashType.Sha256);
        // Open the file to hash.
        using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
        // Update the context after reading each chunk.
        while (file.GetPosition() < file.GetLength())
        {
            int remaining = (int)(file.GetLength() - file.GetPosition());
            ctx.Update(file.GetBuffer(Mathf.Min(remaining, ChunkSize)));
        }
        // Get the computed hash.
        byte[] res = ctx.Finish();
        // Print the result as hex string and array.
        GD.PrintT(res.HexEncode(), (Variant)res);
    }

方法


枚举

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.


方法说明

PackedByteArray finish ( )

Closes the current context, and return the computed hash.


Error start ( type: HashType )

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


Error update ( chunk: PackedByteArray )

Updates the computation with the given chunk of data.

1

本方法通常需要用户覆盖才能生效。

2

本方法无副作用,不会修改该实例的任何成员变量。

3

本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。

4

本方法用于构造某个类型。

5

调用本方法无需实例,可直接使用类名进行调用。

6

本方法描述的是使用本类型作为左操作数的有效运算符。

7

这个值是由下列位标志构成位掩码的整数。

8

无返回值。