HashingContext

package
v0.0.0-...-660914d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package HashingContext provides methods for working with HashingContext object instances.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsHashingContext() Instance
}

type HashType

type HashType = gdclass.HashingContextHashType //gd:HashingContext.HashType
const (
	/*Hashing algorithm: MD5.*/
	HashMd5 HashType = 0
	/*Hashing algorithm: SHA-1.*/
	HashSha1 HashType = 1
	/*Hashing algorithm: SHA-256.*/
	HashSha256 HashType = 2
)

type Instance

type Instance [1]gdclass.HashingContext

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 [enum HashType] enum shows the supported hashing algorithms. [codeblocks] [gdscript] 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))

[/gdscript] [csharp] 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);
}

[/csharp] [/codeblocks]

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsHashingContext

func (self Instance) AsHashingContext() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) Finish

func (self Instance) Finish() []byte

Closes the current context, and return the computed hash.

func (Instance) Start

func (self Instance) Start(atype gdclass.HashingContextHashType) error

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

func (*Instance) UnsafePointer

func (self *Instance) UnsafePointer() unsafe.Pointer

func (Instance) Update

func (self Instance) Update(chunk []byte) error

Updates the computation with the given [param chunk] of data.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL