class_hashingcontext.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/HashingContext.xml.
  6. .. _class_HashingContext:
  7. HashingContext
  8. ==============
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Provides functionality for computing cryptographic hashes chunk by chunk.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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).
  15. The :ref:`HashType<enum_HashingContext_HashType>` enum shows the supported hashing algorithms.
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. const CHUNK_SIZE = 1024
  19. func hash_file(path):
  20. # Check that file exists.
  21. if not FileAccess.file_exists(path):
  22. return
  23. # Start a SHA-256 context.
  24. var ctx = HashingContext.new()
  25. ctx.start(HashingContext.HASH_SHA256)
  26. # Open the file to hash.
  27. var file = FileAccess.open(path, FileAccess.READ)
  28. # Update the context after reading each chunk.
  29. while not file.eof_reached():
  30. ctx.update(file.get_buffer(CHUNK_SIZE))
  31. # Get the computed hash.
  32. var res = ctx.finish()
  33. # Print the result as hex string and array.
  34. printt(res.hex_encode(), Array(res))
  35. .. code-tab:: csharp
  36. public const int ChunkSize = 1024;
  37. public void HashFile(string path)
  38. {
  39. // Check that file exists.
  40. if (!FileAccess.FileExists(path))
  41. {
  42. return;
  43. }
  44. // Start a SHA-256 context.
  45. var ctx = new HashingContext();
  46. ctx.Start(HashingContext.HashType.Sha256);
  47. // Open the file to hash.
  48. using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  49. // Update the context after reading each chunk.
  50. while (!file.EofReached())
  51. {
  52. ctx.Update(file.GetBuffer(ChunkSize));
  53. }
  54. // Get the computed hash.
  55. byte[] res = ctx.Finish();
  56. // Print the result as hex string and array.
  57. GD.PrintT(res.HexEncode(), (Variant)res);
  58. }
  59. .. rst-class:: classref-reftable-group
  60. Methods
  61. -------
  62. .. table::
  63. :widths: auto
  64. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`finish<class_HashingContext_method_finish>` **(** **)** |
  66. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_HashingContext_method_start>` **(** :ref:`HashType<enum_HashingContext_HashType>` type **)** |
  68. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`update<class_HashingContext_method_update>` **(** :ref:`PackedByteArray<class_PackedByteArray>` chunk **)** |
  70. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  71. .. rst-class:: classref-section-separator
  72. ----
  73. .. rst-class:: classref-descriptions-group
  74. Enumerations
  75. ------------
  76. .. _enum_HashingContext_HashType:
  77. .. rst-class:: classref-enumeration
  78. enum **HashType**:
  79. .. _class_HashingContext_constant_HASH_MD5:
  80. .. rst-class:: classref-enumeration-constant
  81. :ref:`HashType<enum_HashingContext_HashType>` **HASH_MD5** = ``0``
  82. Hashing algorithm: MD5.
  83. .. _class_HashingContext_constant_HASH_SHA1:
  84. .. rst-class:: classref-enumeration-constant
  85. :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA1** = ``1``
  86. Hashing algorithm: SHA-1.
  87. .. _class_HashingContext_constant_HASH_SHA256:
  88. .. rst-class:: classref-enumeration-constant
  89. :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA256** = ``2``
  90. Hashing algorithm: SHA-256.
  91. .. rst-class:: classref-section-separator
  92. ----
  93. .. rst-class:: classref-descriptions-group
  94. Method Descriptions
  95. -------------------
  96. .. _class_HashingContext_method_finish:
  97. .. rst-class:: classref-method
  98. :ref:`PackedByteArray<class_PackedByteArray>` **finish** **(** **)**
  99. Closes the current context, and return the computed hash.
  100. .. rst-class:: classref-item-separator
  101. ----
  102. .. _class_HashingContext_method_start:
  103. .. rst-class:: classref-method
  104. :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`HashType<enum_HashingContext_HashType>` type **)**
  105. Starts a new hash computation of the given ``type`` (e.g. :ref:`HASH_SHA256<class_HashingContext_constant_HASH_SHA256>` to start computation of a SHA-256).
  106. .. rst-class:: classref-item-separator
  107. ----
  108. .. _class_HashingContext_method_update:
  109. .. rst-class:: classref-method
  110. :ref:`Error<enum_@GlobalScope_Error>` **update** **(** :ref:`PackedByteArray<class_PackedByteArray>` chunk **)**
  111. Updates the computation with the given ``chunk`` of data.
  112. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  113. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  114. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  115. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  116. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  117. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  118. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`