0.2.0-dev Download ZIP
130 lines ·5.0 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="AESContext" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Provides access to AES encryption/decryption of raw data. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | 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. |
| 8 | [codeblocks] |
| 9 | [gdscript] |
| 10 | extends Node |
| 11 | |
| 12 | var aes = AESContext.new() |
| 13 | |
| 14 | func _ready(): |
| 15 | var key = "My secret key!!!" # Key must be either 16 or 32 bytes. |
| 16 | var data = "My secret text!!" # Data size must be multiple of 16 bytes, apply padding if needed. |
| 17 | # Encrypt ECB |
| 18 | aes.start(AESContext.MODE_ECB_ENCRYPT, key.to_utf8_buffer()) |
| 19 | var encrypted = aes.update(data.to_utf8_buffer()) |
| 20 | aes.finish() |
| 21 | # Decrypt ECB |
| 22 | aes.start(AESContext.MODE_ECB_DECRYPT, key.to_utf8_buffer()) |
| 23 | var decrypted = aes.update(encrypted) |
| 24 | aes.finish() |
| 25 | # Check ECB |
| 26 | assert(decrypted == data.to_utf8_buffer()) |
| 27 | |
| 28 | var iv = "My secret iv!!!!" # IV must be of exactly 16 bytes. |
| 29 | # Encrypt CBC |
| 30 | aes.start(AESContext.MODE_CBC_ENCRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer()) |
| 31 | encrypted = aes.update(data.to_utf8_buffer()) |
| 32 | aes.finish() |
| 33 | # Decrypt CBC |
| 34 | aes.start(AESContext.MODE_CBC_DECRYPT, key.to_utf8_buffer(), iv.to_utf8_buffer()) |
| 35 | decrypted = aes.update(encrypted) |
| 36 | aes.finish() |
| 37 | # Check CBC |
| 38 | assert(decrypted == data.to_utf8_buffer()) |
| 39 | [/gdscript] |
| 40 | [csharp] |
| 41 | using Godot; |
| 42 | using System.Diagnostics; |
| 43 | |
| 44 | public partial class MyNode : Node |
| 45 | { |
| 46 | private AesContext _aes = new AesContext(); |
| 47 | |
| 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.ToUtf8Buffer()); |
| 54 | byte[] encrypted = _aes.Update(data.ToUtf8Buffer()); |
| 55 | _aes.Finish(); |
| 56 | // Decrypt ECB |
| 57 | _aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8Buffer()); |
| 58 | byte[] decrypted = _aes.Update(encrypted); |
| 59 | _aes.Finish(); |
| 60 | // Check ECB |
| 61 | Debug.Assert(decrypted == data.ToUtf8Buffer()); |
| 62 | |
| 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 | [/csharp] |
| 77 | [/codeblocks] |
| 78 | </description> |
| 79 | <tutorials> |
| 80 | </tutorials> |
| 81 | <methods> |
| 82 | <method name="finish"> |
| 83 | <return type="void" /> |
| 84 | <description> |
| 85 | Close this AES context so it can be started again. See [method start]. |
| 86 | </description> |
| 87 | </method> |
| 88 | <method name="get_iv_state" deprecated="Extracting the IV state is no longer supported"> |
| 89 | <return type="PackedByteArray" /> |
| 90 | <description> |
| 91 | Returns an empty [PackedByteArray]. |
| 92 | [b]Note:[/b] This method used to return the active IV state as modified by one or more [method update] calls. This is no longer supported. |
| 93 | </description> |
| 94 | </method> |
| 95 | <method name="start"> |
| 96 | <return type="int" enum="Error" /> |
| 97 | <param index="0" name="mode" type="int" enum="AESContext.Mode" /> |
| 98 | <param index="1" name="key" type="PackedByteArray" /> |
| 99 | <param index="2" name="iv" type="PackedByteArray" default="PackedByteArray()" /> |
| 100 | <description> |
| 101 | Start the AES context in the given [param mode]. A [param key] of either 16 or 32 bytes must always be provided, while an [param iv] (initialization vector) of exactly 16 bytes, is only needed when [param mode] is either [constant MODE_CBC_ENCRYPT] or [constant MODE_CBC_DECRYPT]. |
| 102 | </description> |
| 103 | </method> |
| 104 | <method name="update"> |
| 105 | <return type="PackedByteArray" /> |
| 106 | <param index="0" name="src" type="PackedByteArray" /> |
| 107 | <description> |
| 108 | Run the desired operation for this AES context. Will return a [PackedByteArray] containing the result of encrypting (or decrypting) the given [param src]. See [method start] for mode of operation. |
| 109 | [b]Note:[/b] The size of [param src] must be a multiple of 16. Apply some padding if needed. |
| 110 | </description> |
| 111 | </method> |
| 112 | </methods> |
| 113 | <constants> |
| 114 | <constant name="MODE_ECB_ENCRYPT" value="0" enum="Mode"> |
| 115 | AES electronic codebook encryption mode. |
| 116 | </constant> |
| 117 | <constant name="MODE_ECB_DECRYPT" value="1" enum="Mode"> |
| 118 | AES electronic codebook decryption mode. |
| 119 | </constant> |
| 120 | <constant name="MODE_CBC_ENCRYPT" value="2" enum="Mode"> |
| 121 | AES cipher block chaining encryption mode. |
| 122 | </constant> |
| 123 | <constant name="MODE_CBC_DECRYPT" value="3" enum="Mode"> |
| 124 | AES cipher block chaining decryption mode. |
| 125 | </constant> |
| 126 | <constant name="MODE_MAX" value="4" enum="Mode"> |
| 127 | Maximum value for the mode enum. |
| 128 | </constant> |
| 129 | </constants> |
| 130 | </class> |