class_hashingcontext.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 an 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 file.get_position() < file.get_length():
  30. var remaining = file.get_length() - file.get_position()
  31. ctx.update(file.get_buffer(min(remaining, 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. // Check that file exists.
  41. if (!FileAccess.FileExists(path))
  42. {
  43. return;
  44. }
  45. // Start an SHA-256 context.
  46. var ctx = new HashingContext();
  47. ctx.Start(HashingContext.HashType.Sha256);
  48. // Open the file to hash.
  49. using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  50. // Update the context after reading each chunk.
  51. while (file.GetPosition() < file.GetLength())
  52. {
  53. int remaining = (int)(file.GetLength() - file.GetPosition());
  54. ctx.Update(file.GetBuffer(Mathf.Min(remaining, 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(), (Variant)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>`\ (\ type\: :ref:`HashType<enum_HashingContext_HashType>`\ ) |
  70. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`update<class_HashingContext_method_update>`\ (\ chunk\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  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**: :ref:`🔗<enum_HashingContext_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**\ (\ ) :ref:`🔗<class_HashingContext_method_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**\ (\ type\: :ref:`HashType<enum_HashingContext_HashType>`\ ) :ref:`🔗<class_HashingContext_method_start>`
  107. 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).
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_HashingContext_method_update:
  111. .. rst-class:: classref-method
  112. :ref:`Error<enum_@GlobalScope_Error>` **update**\ (\ chunk\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_HashingContext_method_update>`
  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.)`
  120. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  121. .. |void| replace:: :abbr:`void (No return value.)`