123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- :github_url: hide
- .. DO NOT EDIT THIS FILE!!!
- .. Generated automatically from Godot engine sources.
- .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
- .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/HashingContext.xml.
- .. _class_HashingContext:
- HashingContext
- ==============
- **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
- Provides functionality for computing cryptographic hashes chunk by chunk.
- .. rst-class:: classref-introduction-group
- Description
- -----------
- 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 :ref:`HashType<enum_HashingContext_HashType>` enum shows the supported hashing algorithms.
- .. tabs::
- .. code-tab:: 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))
- .. code-tab:: 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);
- }
- .. rst-class:: classref-reftable-group
- Methods
- -------
- .. table::
- :widths: auto
- +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------+
- | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`finish<class_HashingContext_method_finish>`\ (\ ) |
- +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_HashingContext_method_start>`\ (\ type\: :ref:`HashType<enum_HashingContext_HashType>`\ ) |
- +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`update<class_HashingContext_method_update>`\ (\ chunk\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
- +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------+
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Enumerations
- ------------
- .. _enum_HashingContext_HashType:
- .. rst-class:: classref-enumeration
- enum **HashType**: :ref:`🔗<enum_HashingContext_HashType>`
- .. _class_HashingContext_constant_HASH_MD5:
- .. rst-class:: classref-enumeration-constant
- :ref:`HashType<enum_HashingContext_HashType>` **HASH_MD5** = ``0``
- Hashing algorithm: MD5.
- .. _class_HashingContext_constant_HASH_SHA1:
- .. rst-class:: classref-enumeration-constant
- :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA1** = ``1``
- Hashing algorithm: SHA-1.
- .. _class_HashingContext_constant_HASH_SHA256:
- .. rst-class:: classref-enumeration-constant
- :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA256** = ``2``
- Hashing algorithm: SHA-256.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Method Descriptions
- -------------------
- .. _class_HashingContext_method_finish:
- .. rst-class:: classref-method
- :ref:`PackedByteArray<class_PackedByteArray>` **finish**\ (\ ) :ref:`🔗<class_HashingContext_method_finish>`
- Closes the current context, and return the computed hash.
- .. rst-class:: classref-item-separator
- ----
- .. _class_HashingContext_method_start:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **start**\ (\ type\: :ref:`HashType<enum_HashingContext_HashType>`\ ) :ref:`🔗<class_HashingContext_method_start>`
- Starts a new hash computation of the given ``type`` (e.g. :ref:`HASH_SHA256<class_HashingContext_constant_HASH_SHA256>` to start computation of an SHA-256).
- .. rst-class:: classref-item-separator
- ----
- .. _class_HashingContext_method_update:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **update**\ (\ chunk\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_HashingContext_method_update>`
- Updates the computation with the given ``chunk`` of data.
- .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
- .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
- .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
- .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
- .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
- .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
- .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
- .. |void| replace:: :abbr:`void (No return value.)`
|