class_aescontext.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Description
  12. -----------
  13. This class provides access to AES encryption/decryption of raw data. Both AES-ECB and AES-CBC mode are supported.
  14. .. tabs::
  15. .. code-tab:: gdscript
  16. extends Node
  17. var aes = AESContext.new()
  18. func _ready():
  19. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  20. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  21. # Encrypt ECB
  22. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8())
  23. var encrypted = aes.update(data.to_utf8())
  24. aes.finish()
  25. # Decrypt ECB
  26. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
  27. var decrypted = aes.update(encrypted)
  28. aes.finish()
  29. # Check ECB
  30. assert(decrypted == data.to_utf8())
  31. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  32. # Encrypt CBC
  33. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8(), iv.to_utf8())
  34. encrypted = aes.update(data.to_utf8())
  35. aes.finish()
  36. # Decrypt CBC
  37. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
  38. decrypted = aes.update(encrypted)
  39. aes.finish()
  40. # Check CBC
  41. assert(decrypted == data.to_utf8())
  42. .. code-tab:: csharp
  43. using Godot;
  44. using System;
  45. using System.Diagnostics;
  46. public class Example : Node
  47. {
  48. public 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.ToUTF8());
  55. byte[] encrypted = Aes.Update(data.ToUTF8());
  56. Aes.Finish();
  57. // Decrypt ECB
  58. Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8());
  59. byte[] decrypted = Aes.Update(encrypted);
  60. Aes.Finish();
  61. // Check ECB
  62. Debug.Assert(decrypted == data.ToUTF8());
  63. string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
  64. // Encrypt CBC
  65. Aes.Start(AESContext.Mode.EcbEncrypt, key.ToUTF8(), iv.ToUTF8());
  66. encrypted = Aes.Update(data.ToUTF8());
  67. Aes.Finish();
  68. // Decrypt CBC
  69. Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8(), iv.ToUTF8());
  70. decrypted = Aes.Update(encrypted);
  71. Aes.Finish();
  72. // Check CBC
  73. Debug.Assert(decrypted == data.ToUTF8());
  74. }
  75. }
  76. Methods
  77. -------
  78. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | void | :ref:`finish<class_AESContext_method_finish>` **(** **)** |
  80. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_iv_state<class_AESContext_method_get_iv_state>` **(** **)** |
  82. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :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() **)** |
  84. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`update<class_AESContext_method_update>` **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)** |
  86. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. Enumerations
  88. ------------
  89. .. _enum_AESContext_Mode:
  90. .. _class_AESContext_constant_MODE_ECB_ENCRYPT:
  91. .. _class_AESContext_constant_MODE_ECB_DECRYPT:
  92. .. _class_AESContext_constant_MODE_CBC_ENCRYPT:
  93. .. _class_AESContext_constant_MODE_CBC_DECRYPT:
  94. .. _class_AESContext_constant_MODE_MAX:
  95. enum **Mode**:
  96. - **MODE_ECB_ENCRYPT** = **0** --- AES electronic codebook encryption mode.
  97. - **MODE_ECB_DECRYPT** = **1** --- AES electronic codebook decryption mode.
  98. - **MODE_CBC_ENCRYPT** = **2** --- AES cipher blocker chaining encryption mode.
  99. - **MODE_CBC_DECRYPT** = **3** --- AES cipher blocker chaining decryption mode.
  100. - **MODE_MAX** = **4** --- Maximum value for the mode enum.
  101. Method Descriptions
  102. -------------------
  103. .. _class_AESContext_method_finish:
  104. - void **finish** **(** **)**
  105. Close this AES context so it can be started again. See :ref:`start<class_AESContext_method_start>`.
  106. ----
  107. .. _class_AESContext_method_get_iv_state:
  108. - :ref:`PackedByteArray<class_PackedByteArray>` **get_iv_state** **(** **)**
  109. 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.
  110. \ **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>`.
  111. ----
  112. .. _class_AESContext_method_start:
  113. - :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` iv=PackedByteArray() **)**
  114. 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>`.
  115. ----
  116. .. _class_AESContext_method_update:
  117. - :ref:`PackedByteArray<class_PackedByteArray>` **update** **(** :ref:`PackedByteArray<class_PackedByteArray>` src **)**
  118. 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.
  119. \ **Note:** The size of ``src`` must be a multiple of 16. Apply some padding if needed.
  120. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  121. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  122. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  123. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  124. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  125. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`