class_aescontext.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/AESContext.xml.
  6. .. _class_AESContext:
  7. AESContext
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Interface to low level AES encryption features.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class provides access to AES encryption/decryption of raw data. Both AES-ECB and AES-CBC mode 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())
  24. var encrypted = aes.update(data.to_utf8())
  25. aes.finish()
  26. # Decrypt ECB
  27. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
  28. var decrypted = aes.update(encrypted)
  29. aes.finish()
  30. # Check ECB
  31. assert(decrypted == data.to_utf8())
  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(), iv.to_utf8())
  35. encrypted = aes.update(data.to_utf8())
  36. aes.finish()
  37. # Decrypt CBC
  38. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
  39. decrypted = aes.update(encrypted)
  40. aes.finish()
  41. # Check CBC
  42. assert(decrypted == data.to_utf8())
  43. .. code-tab:: csharp
  44. using Godot;
  45. using System;
  46. using System.Diagnostics;
  47. public class Example : Node
  48. {
  49. public AESContext Aes = new AESContext();
  50. public override void _Ready()
  51. {
  52. string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
  53. string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
  54. // Encrypt ECB
  55. Aes.Start(AESContext.Mode.EcbEncrypt, key.ToUTF8());
  56. byte[] encrypted = Aes.Update(data.ToUTF8());
  57. Aes.Finish();
  58. // Decrypt ECB
  59. Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8());
  60. byte[] decrypted = Aes.Update(encrypted);
  61. Aes.Finish();
  62. // Check ECB
  63. Debug.Assert(decrypted == data.ToUTF8());
  64. string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
  65. // Encrypt CBC
  66. Aes.Start(AESContext.Mode.EcbEncrypt, key.ToUTF8(), iv.ToUTF8());
  67. encrypted = Aes.Update(data.ToUTF8());
  68. Aes.Finish();
  69. // Decrypt CBC
  70. Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8(), iv.ToUTF8());
  71. decrypted = Aes.Update(encrypted);
  72. Aes.Finish();
  73. // Check CBC
  74. Debug.Assert(decrypted == data.ToUTF8());
  75. }
  76. }
  77. .. rst-class:: classref-reftable-group
  78. Methods
  79. -------
  80. .. table::
  81. :widths: auto
  82. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | void | :ref:`finish<class_AESContext_method_finish>` **(** **)** |
  84. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_iv_state<class_AESContext_method_get_iv_state>` **(** **)** |
  86. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :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() **)** |
  88. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`update<class_AESContext_method_update>` **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)** |
  90. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. .. rst-class:: classref-section-separator
  92. ----
  93. .. rst-class:: classref-descriptions-group
  94. Enumerations
  95. ------------
  96. .. _enum_AESContext_Mode:
  97. .. rst-class:: classref-enumeration
  98. enum **Mode**:
  99. .. _class_AESContext_constant_MODE_ECB_ENCRYPT:
  100. .. rst-class:: classref-enumeration-constant
  101. :ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_ENCRYPT** = ``0``
  102. AES electronic codebook encryption mode.
  103. .. _class_AESContext_constant_MODE_ECB_DECRYPT:
  104. .. rst-class:: classref-enumeration-constant
  105. :ref:`Mode<enum_AESContext_Mode>` **MODE_ECB_DECRYPT** = ``1``
  106. AES electronic codebook decryption mode.
  107. .. _class_AESContext_constant_MODE_CBC_ENCRYPT:
  108. .. rst-class:: classref-enumeration-constant
  109. :ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_ENCRYPT** = ``2``
  110. AES cipher blocker chaining encryption mode.
  111. .. _class_AESContext_constant_MODE_CBC_DECRYPT:
  112. .. rst-class:: classref-enumeration-constant
  113. :ref:`Mode<enum_AESContext_Mode>` **MODE_CBC_DECRYPT** = ``3``
  114. AES cipher blocker chaining decryption mode.
  115. .. _class_AESContext_constant_MODE_MAX:
  116. .. rst-class:: classref-enumeration-constant
  117. :ref:`Mode<enum_AESContext_Mode>` **MODE_MAX** = ``4``
  118. Maximum value for the mode enum.
  119. .. rst-class:: classref-section-separator
  120. ----
  121. .. rst-class:: classref-descriptions-group
  122. Method Descriptions
  123. -------------------
  124. .. _class_AESContext_method_finish:
  125. .. rst-class:: classref-method
  126. void **finish** **(** **)**
  127. Close this AES context so it can be started again. See :ref:`start<class_AESContext_method_start>`.
  128. .. rst-class:: classref-item-separator
  129. ----
  130. .. _class_AESContext_method_get_iv_state:
  131. .. rst-class:: classref-method
  132. :ref:`PackedByteArray<class_PackedByteArray>` **get_iv_state** **(** **)**
  133. 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.
  134. \ **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>`.
  135. .. rst-class:: classref-item-separator
  136. ----
  137. .. _class_AESContext_method_start:
  138. .. rst-class:: classref-method
  139. :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` iv=PackedByteArray() **)**
  140. 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>`.
  141. .. rst-class:: classref-item-separator
  142. ----
  143. .. _class_AESContext_method_update:
  144. .. rst-class:: classref-method
  145. :ref:`PackedByteArray<class_PackedByteArray>` **update** **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)**
  146. 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.
  147. \ **Note:** The size of ``src`` must be a multiple of 16. Apply some padding if needed.
  148. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  149. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  150. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  151. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  152. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  153. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`