class_hashingcontext.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. Context to compute cryptographic hashes over multiple iterations.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. The HashingContext class provides an interface for computing cryptographic hashes over multiple iterations. This is useful for example when 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 = 102
  19. func hash_file(path):
  20. var ctx = HashingContext.new()
  21. var file = File.new()
  22. # Start a SHA-256 context.
  23. ctx.start(HashingContext.HASH_SHA256)
  24. # Check that file exists.
  25. if not file.file_exists(path):
  26. return
  27. # Open the file to hash.
  28. file.open(path, File.READ)
  29. # Update the context after reading each chunk.
  30. while not file.eof_reached():
  31. ctx.update(file.get_buffer(CHUNK_SIZE))
  32. # Get the computed hash.
  33. var res = ctx.finish()
  34. # Print the result as hex string and array.
  35. printt(res.hex_encode(), Array(res))
  36. .. code-tab:: csharp
  37. public const int ChunkSize = 1024;
  38. public void HashFile(string path)
  39. {
  40. var ctx = new HashingContext();
  41. var file = new File();
  42. // Start a SHA-256 context.
  43. ctx.Start(HashingContext.HashType.Sha256);
  44. // Check that file exists.
  45. if (!file.FileExists(path))
  46. {
  47. return;
  48. }
  49. // Open the file to hash.
  50. file.Open(path, File.ModeFlags.Read);
  51. // Update the context after reading each chunk.
  52. while (!file.EofReached())
  53. {
  54. ctx.Update(file.GetBuffer(ChunkSize));
  55. }
  56. // Get the computed hash.
  57. byte[] res = ctx.Finish();
  58. // Print the result as hex string and array.
  59. GD.PrintT(res.HexEncode(), res);
  60. }
  61. .. rst-class:: classref-reftable-group
  62. Methods
  63. -------
  64. .. table::
  65. :widths: auto
  66. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`finish<class_HashingContext_method_finish>` **(** **)** |
  68. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_HashingContext_method_start>` **(** :ref:`HashType<enum_HashingContext_HashType>` type **)** |
  70. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`update<class_HashingContext_method_update>` **(** :ref:`PackedByteArray<class_PackedByteArray>` chunk **)** |
  72. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
  73. .. rst-class:: classref-section-separator
  74. ----
  75. .. rst-class:: classref-descriptions-group
  76. Enumerations
  77. ------------
  78. .. _enum_HashingContext_HashType:
  79. .. rst-class:: classref-enumeration
  80. enum **HashType**:
  81. .. _class_HashingContext_constant_HASH_MD5:
  82. .. rst-class:: classref-enumeration-constant
  83. :ref:`HashType<enum_HashingContext_HashType>` **HASH_MD5** = ``0``
  84. Hashing algorithm: MD5.
  85. .. _class_HashingContext_constant_HASH_SHA1:
  86. .. rst-class:: classref-enumeration-constant
  87. :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA1** = ``1``
  88. Hashing algorithm: SHA-1.
  89. .. _class_HashingContext_constant_HASH_SHA256:
  90. .. rst-class:: classref-enumeration-constant
  91. :ref:`HashType<enum_HashingContext_HashType>` **HASH_SHA256** = ``2``
  92. Hashing algorithm: SHA-256.
  93. .. rst-class:: classref-section-separator
  94. ----
  95. .. rst-class:: classref-descriptions-group
  96. Method Descriptions
  97. -------------------
  98. .. _class_HashingContext_method_finish:
  99. .. rst-class:: classref-method
  100. :ref:`PackedByteArray<class_PackedByteArray>` **finish** **(** **)**
  101. Closes the current context, and return the computed hash.
  102. .. rst-class:: classref-item-separator
  103. ----
  104. .. _class_HashingContext_method_start:
  105. .. rst-class:: classref-method
  106. :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`HashType<enum_HashingContext_HashType>` type **)**
  107. 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).
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_HashingContext_method_update:
  111. .. rst-class:: classref-method
  112. :ref:`Error<enum_@GlobalScope_Error>` **update** **(** :ref:`PackedByteArray<class_PackedByteArray>` chunk **)**
  113. Updates the computation with the given ``chunk`` of data.
  114. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  115. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  116. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  117. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  118. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  119. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`