class_aescontext.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/4.2/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.2/doc/classes/AESContext.xml.
  6. .. _class_AESContext:
  7. AESContext
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Provides access to AES encryption/decryption of raw data.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class holds the context information required for encryption and decryption operations with AES (Advanced Encryption Standard). Both AES-ECB and AES-CBC modes are supported.
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. extends Node
  18. var aes = AESContext.new()
  19. func _ready():
  20. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  21. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  22. # Encrypt ECB
  23. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer())
  24. var encrypted = aes.update(data.to_utf8_buffer())
  25. aes.finish()
  26. # Decrypt ECB
  27. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer())
  28. var decrypted = aes.update(encrypted)
  29. aes.finish()
  30. # Check ECB
  31. assert(decrypted == data.to_utf8_buffer())
  32. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  33. # Encrypt CBC
  34. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  35. encrypted = aes.update(data.to_utf8_buffer())
  36. aes.finish()
  37. # Decrypt CBC
  38. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer())
  39. decrypted = aes.update(encrypted)
  40. aes.finish()
  41. # Check CBC
  42. assert(decrypted == data.to_utf8_buffer())
  43. .. code-tab:: csharp
  44. using Godot;
  45. using System.Diagnostics;
  46. public partial class MyNode : Node
  47. {
  48. private AesContext _aes = new AesContext();
  49. public override void _Ready()
  50. {
  51. string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
  52. string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
  53. // Encrypt ECB
  54. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer());
  55. byte[] encrypted = _aes.Update(data.ToUtf8Buffer());
  56. _aes.Finish();
  57. // Decrypt ECB
  58. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer());
  59. byte[] decrypted = _aes.Update(encrypted);
  60. _aes.Finish();
  61. // Check ECB
  62. Debug.Assert(decrypted == data.ToUtf8Buffer());
  63. string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
  64. // Encrypt CBC
  65. _aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  66. encrypted = _aes.Update(data.ToUtf8Buffer());
  67. _aes.Finish();
  68. // Decrypt CBC
  69. _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer(), iv.ToUtf8Buffer());
  70. decrypted = _aes.Update(encrypted);
  71. _aes.Finish();
  72. // Check CBC
  73. Debug.Assert(decrypted == data.ToUtf8Buffer());
  74. }
  75. }
  76. .. rst-class:: classref-reftable-group
  77. Methods
  78. -------
  79. .. table::
  80. :widths: auto
  81. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | void | :ref:`finish<class_AESContext_method_finish>` **(** **)** |
  83. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_iv_state<class_AESContext_method_get_iv_state>` **(** **)** |
  85. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_AESContext_method_start>` **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` iv=PackedByteArray() **)** |
  87. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`update<class_AESContext_method_update>` **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)** |
  89. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. .. rst-class:: classref-section-separator
  91. ----
  92. .. rst-class:: classref-descriptions-group
  93. Enumerations
  94. ------------
  95. .. _enum_AESContext_Mode:
  96. .. rst-class:: classref-enumeration
  97. enum **Mode**:
  98. .. _class_AESContext_constant_MODE_ECB_ENCRYPT:
  99. .. rst-class:: classref-enumeration-constant
  100. :ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_ENCRYPT** = ``0``
  101. AES electronic codebook encryption mode.
  102. .. _class_AESContext_constant_MODE_ECB_DECRYPT:
  103. .. rst-class:: classref-enumeration-constant
  104. :ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_DECRYPT** = ``1``
  105. AES electronic codebook decryption mode.
  106. .. _class_AESContext_constant_MODE_CBC_ENCRYPT:
  107. .. rst-class:: classref-enumeration-constant
  108. :ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_ENCRYPT** = ``2``
  109. AES cipher blocker chaining encryption mode.
  110. .. _class_AESContext_constant_MODE_CBC_DECRYPT:
  111. .. rst-class:: classref-enumeration-constant
  112. :ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_DECRYPT** = ``3``
  113. AES cipher blocker chaining decryption mode.
  114. .. _class_AESContext_constant_MODE_MAX:
  115. .. rst-class:: classref-enumeration-constant
  116. :ref:`Mode<enum_AESContext_Mode>` **MODE_MAX** = ``4``
  117. Maximum value for the mode enum.
  118. .. rst-class:: classref-section-separator
  119. ----
  120. .. rst-class:: classref-descriptions-group
  121. Method Descriptions
  122. -------------------
  123. .. _class_AESContext_method_finish:
  124. .. rst-class:: classref-method
  125. void **finish** **(** **)**
  126. Close this AES context so it can be started again. See :ref:`start<class_AESContext_method_start>`.
  127. .. rst-class:: classref-item-separator
  128. ----
  129. .. _class_AESContext_method_get_iv_state:
  130. .. rst-class:: classref-method
  131. :ref:`PackedByteArray<class_PackedByteArray>` **get_iv_state** **(** **)**
  132. Get the current IV state for this context (IV gets updated when calling :ref:`update<class_AESContext_method_update>`). You normally don't need this function.
  133. \ **Note:** This function only makes sense when the context is started with :ref:`MODE_CBC_ENCRYPT<class_AESContext_constant_MODE_CBC_ENCRYPT>` or :ref:`MODE_CBC_DECRYPT<class_AESContext_constant_MODE_CBC_DECRYPT>`.
  134. .. rst-class:: classref-item-separator
  135. ----
  136. .. _class_AESContext_method_start:
  137. .. rst-class:: classref-method
  138. :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` iv=PackedByteArray() **)**
  139. Start the AES context in the given ``mode``. A ``key`` of either 16 or 32 bytes must always be provided, while an ``iv`` (initialization vector) of exactly 16 bytes, is only needed when ``mode`` is either :ref:`MODE_CBC_ENCRYPT<class_AESContext_constant_MODE_CBC_ENCRYPT>` or :ref:`MODE_CBC_DECRYPT<class_AESContext_constant_MODE_CBC_DECRYPT>`.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. _class_AESContext_method_update:
  143. .. rst-class:: classref-method
  144. :ref:`PackedByteArray<class_PackedByteArray>` **update** **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)**
  145. Run the desired operation for this AES context. Will return a :ref:`PackedByteArray<class_PackedByteArray>` containing the result of encrypting (or decrypting) the given ``src``. See :ref:`start<class_AESContext_method_start>` for mode of operation.
  146. \ **Note:** The size of ``src`` must be a multiple of 16. Apply some padding if needed.
  147. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  148. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  149. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  150. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  151. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  152. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  153. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`