class_aescontext.rst 8.5 KB

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