class_hashingcontext.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the HashingContext.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_HashingContext:
  6. HashingContext
  7. ==============
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. Context to compute cryptographic hashes over multiple iterations.
  10. Description
  11. -----------
  12. 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).
  13. The :ref:`HashType<enum_HashingContext_HashType>` enum shows the supported hashing algorithms.
  14. ::
  15. const CHUNK_SIZE = 1024
  16. func hash_file(path):
  17. var ctx = HashingContext.new()
  18. var file = File.new()
  19. # Start a SHA-256 context.
  20. ctx.start(HashingContext.HASH_SHA256)
  21. # Check that file exists.
  22. if not file.file_exists(path):
  23. return
  24. # Open the file to hash.
  25. file.open(path, File.READ)
  26. # Update the context after reading each chunk.
  27. while not file.eof_reached():
  28. ctx.update(file.get_buffer(CHUNK_SIZE))
  29. # Get the computed hash.
  30. var res = ctx.finish()
  31. # Print the result as hex string and array.
  32. printt(res.hex_encode(), Array(res))
  33. **Note:** Not available in HTML5 exports.
  34. Methods
  35. -------
  36. +-------------------------------------------+----------------------------------------------------------------------------------------------------------------+
  37. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`finish<class_HashingContext_method_finish>` **(** **)** |
  38. +-------------------------------------------+----------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_HashingContext_method_start>` **(** :ref:`HashType<enum_HashingContext_HashType>` type **)** |
  40. +-------------------------------------------+----------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`update<class_HashingContext_method_update>` **(** :ref:`PoolByteArray<class_PoolByteArray>` chunk **)** |
  42. +-------------------------------------------+----------------------------------------------------------------------------------------------------------------+
  43. Enumerations
  44. ------------
  45. .. _enum_HashingContext_HashType:
  46. .. _class_HashingContext_constant_HASH_MD5:
  47. .. _class_HashingContext_constant_HASH_SHA1:
  48. .. _class_HashingContext_constant_HASH_SHA256:
  49. enum **HashType**:
  50. - **HASH_MD5** = **0** --- Hashing algorithm: MD5.
  51. - **HASH_SHA1** = **1** --- Hashing algorithm: SHA-1.
  52. - **HASH_SHA256** = **2** --- Hashing algorithm: SHA-256.
  53. Method Descriptions
  54. -------------------
  55. .. _class_HashingContext_method_finish:
  56. - :ref:`PoolByteArray<class_PoolByteArray>` **finish** **(** **)**
  57. Closes the current context, and return the computed hash.
  58. ----
  59. .. _class_HashingContext_method_start:
  60. - :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`HashType<enum_HashingContext_HashType>` type **)**
  61. 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).
  62. ----
  63. .. _class_HashingContext_method_update:
  64. - :ref:`Error<enum_@GlobalScope_Error>` **update** **(** :ref:`PoolByteArray<class_PoolByteArray>` chunk **)**
  65. Updates the computation with the given ``chunk`` of data.
  66. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  67. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  68. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`