class_aescontext.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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:`Reference<class_Reference>` **<** :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. ::
  14. extends Node
  15. var aes = AESContext.new()
  16. func _ready():
  17. var key = "My secret key!!!" # Key must be either 16 or 32 bytes.
  18. var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed.
  19. # Encrypt ECB
  20. aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8())
  21. var encrypted = aes.update(data.to_utf8())
  22. aes.finish()
  23. # Decrypt ECB
  24. aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8())
  25. var decrypted = aes.update(encrypted)
  26. aes.finish()
  27. # Check ECB
  28. assert(decrypted == data.to_utf8())
  29. var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes.
  30. # Encrypt CBC
  31. aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8(), iv.to_utf8())
  32. encrypted = aes.update(data.to_utf8())
  33. aes.finish()
  34. # Decrypt CBC
  35. aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8(), iv.to_utf8())
  36. decrypted = aes.update(encrypted)
  37. aes.finish()
  38. # Check CBC
  39. assert(decrypted == data.to_utf8())
  40. Methods
  41. -------
  42. +-------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | void | :ref:`finish<class_AESContext_method_finish>` **(** **)** |
  44. +-------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`get_iv_state<class_AESContext_method_get_iv_state>` **(** **)** |
  46. +-------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_AESContext_method_start>` **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PoolByteArray<class_PoolByteArray>` key, :ref:`PoolByteArray<class_PoolByteArray>` iv=PoolByteArray( ) **)** |
  48. +-------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`update<class_AESContext_method_update>` **(** :ref:`PoolByteArray<class_PoolByteArray>` src **)** |
  50. +-------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. Enumerations
  52. ------------
  53. .. _enum_AESContext_Mode:
  54. .. _class_AESContext_constant_MODE_ECB_ENCRYPT:
  55. .. _class_AESContext_constant_MODE_ECB_DECRYPT:
  56. .. _class_AESContext_constant_MODE_CBC_ENCRYPT:
  57. .. _class_AESContext_constant_MODE_CBC_DECRYPT:
  58. .. _class_AESContext_constant_MODE_MAX:
  59. enum **Mode**:
  60. - **MODE_ECB_ENCRYPT** = **0** --- AES electronic codebook encryption mode.
  61. - **MODE_ECB_DECRYPT** = **1** --- AES electronic codebook decryption mode.
  62. - **MODE_CBC_ENCRYPT** = **2** --- AES cipher blocker chaining encryption mode.
  63. - **MODE_CBC_DECRYPT** = **3** --- AES cipher blocker chaining decryption mode.
  64. - **MODE_MAX** = **4** --- Maximum value for the mode enum.
  65. Method Descriptions
  66. -------------------
  67. .. _class_AESContext_method_finish:
  68. - void **finish** **(** **)**
  69. Close this AES context so it can be started again. See :ref:`start<class_AESContext_method_start>`.
  70. ----
  71. .. _class_AESContext_method_get_iv_state:
  72. - :ref:`PoolByteArray<class_PoolByteArray>` **get_iv_state** **(** **)**
  73. 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.
  74. **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>`.
  75. ----
  76. .. _class_AESContext_method_start:
  77. - :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`Mode<enum_AESContext_Mode>` mode, :ref:`PoolByteArray<class_PoolByteArray>` key, :ref:`PoolByteArray<class_PoolByteArray>` iv=PoolByteArray( ) **)**
  78. 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>`.
  79. ----
  80. .. _class_AESContext_method_update:
  81. - :ref:`PoolByteArray<class_PoolByteArray>` **update** **(** :ref:`PoolByteArray<class_PoolByteArray>` src **)**
  82. Run the desired operation for this AES context. Will return a :ref:`PoolByteArray<class_PoolByteArray>` containing the result of encrypting (or decrypting) the given ``src``. See :ref:`start<class_AESContext_method_start>` for mode of operation.
  83. **Note:** The size of ``src`` must be a multiple of 16. Apply some padding if needed.
  84. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  85. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  86. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`