class_crypto.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/Crypto.xml.
  6. .. _class_Crypto:
  7. Crypto
  8. ======
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Access to advanced cryptographic functionalities.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. The Crypto class allows you to access some more advanced cryptographic functionalities in Godot.
  15. For now, this includes generating cryptographically secure random bytes, RSA keys and self-signed X509 certificates generation, asymmetric key encryption/decryption, and signing/verification.
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. extends Node
  19. var crypto = Crypto.new()
  20. var key = CryptoKey.new()
  21. var cert = X509Certificate.new()
  22. func _ready():
  23. # Generate new RSA key.
  24. key = crypto.generate_rsa(4096)
  25. # Generate new self-signed certificate with the given key.
  26. cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
  27. # Save key and certificate in the user folder.
  28. key.save("user://generated.key")
  29. cert.save("user://generated.crt")
  30. # Encryption
  31. var data = "Some data"
  32. var encrypted = crypto.encrypt(key, data.to_utf8())
  33. # Decryption
  34. var decrypted = crypto.decrypt(key, encrypted)
  35. # Signing
  36. var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
  37. # Verifying
  38. var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
  39. # Checks
  40. assert(verified)
  41. assert(data.to_utf8() == decrypted)
  42. .. code-tab:: csharp
  43. using Godot;
  44. using System;
  45. using System.Diagnostics;
  46. public class CryptoNode : Node
  47. {
  48. public Crypto Crypto = new Crypto();
  49. public CryptoKey Key = new CryptoKey();
  50. public X509Certificate Cert = new X509Certificate();
  51. public override void _Ready()
  52. {
  53. // Generate new RSA key.
  54. Key = Crypto.GenerateRsa(4096);
  55. // Generate new self-signed certificate with the given key.
  56. Cert = Crypto.GenerateSelfSignedCertificate(Key, "CN=mydomain.com,O=My Game Company,C=IT");
  57. // Save key and certificate in the user folder.
  58. Key.Save("user://generated.key");
  59. Cert.Save("user://generated.crt");
  60. // Encryption
  61. string data = "Some data";
  62. byte[] encrypted = Crypto.Encrypt(Key, data.ToUTF8());
  63. // Decryption
  64. byte[] decrypted = Crypto.Decrypt(Key, encrypted);
  65. // Signing
  66. byte[] signature = Crypto.Sign(HashingContext.HashType.Sha256, Data.SHA256Buffer(), Key);
  67. // Verifying
  68. bool verified = Crypto.Verify(HashingContext.HashType.Sha256, Data.SHA256Buffer(), signature, Key);
  69. // Checks
  70. Debug.Assert(verified);
  71. Debug.Assert(data.ToUTF8() == decrypted);
  72. }
  73. }
  74. .. rst-class:: classref-reftable-group
  75. Methods
  76. -------
  77. .. table::
  78. :widths: auto
  79. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`bool<class_bool>` | :ref:`constant_time_compare<class_Crypto_method_constant_time_compare>` **(** :ref:`PackedByteArray<class_PackedByteArray>` trusted, :ref:`PackedByteArray<class_PackedByteArray>` received **)** |
  81. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`decrypt<class_Crypto_method_decrypt>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PackedByteArray<class_PackedByteArray>` ciphertext **)** |
  83. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`encrypt<class_Crypto_method_encrypt>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PackedByteArray<class_PackedByteArray>` plaintext **)** |
  85. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`generate_random_bytes<class_Crypto_method_generate_random_bytes>` **(** :ref:`int<class_int>` size **)** |
  87. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`CryptoKey<class_CryptoKey>` | :ref:`generate_rsa<class_Crypto_method_generate_rsa>` **(** :ref:`int<class_int>` size **)** |
  89. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`generate_self_signed_certificate<class_Crypto_method_generate_self_signed_certificate>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`String<class_String>` issuer_name="CN=myserver,O=myorganisation,C=IT", :ref:`String<class_String>` not_before="20140101000000", :ref:`String<class_String>` not_after="20340101000000" **)** |
  91. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`hmac_digest<class_Crypto_method_hmac_digest>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` msg **)** |
  93. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`sign<class_Crypto_method_sign>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` hash, :ref:`CryptoKey<class_CryptoKey>` key **)** |
  95. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | :ref:`bool<class_bool>` | :ref:`verify<class_Crypto_method_verify>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` hash, :ref:`PackedByteArray<class_PackedByteArray>` signature, :ref:`CryptoKey<class_CryptoKey>` key **)** |
  97. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. .. rst-class:: classref-section-separator
  99. ----
  100. .. rst-class:: classref-descriptions-group
  101. Method Descriptions
  102. -------------------
  103. .. _class_Crypto_method_constant_time_compare:
  104. .. rst-class:: classref-method
  105. :ref:`bool<class_bool>` **constant_time_compare** **(** :ref:`PackedByteArray<class_PackedByteArray>` trusted, :ref:`PackedByteArray<class_PackedByteArray>` received **)**
  106. Compares two :ref:`PackedByteArray<class_PackedByteArray>`\ s for equality without leaking timing information in order to prevent timing attacks.
  107. See `this blog post <https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy>`__ for more information.
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_Crypto_method_decrypt:
  111. .. rst-class:: classref-method
  112. :ref:`PackedByteArray<class_PackedByteArray>` **decrypt** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PackedByteArray<class_PackedByteArray>` ciphertext **)**
  113. Decrypt the given ``ciphertext`` with the provided private ``key``.
  114. \ **Note:** The maximum size of accepted ciphertext is limited by the key size.
  115. .. rst-class:: classref-item-separator
  116. ----
  117. .. _class_Crypto_method_encrypt:
  118. .. rst-class:: classref-method
  119. :ref:`PackedByteArray<class_PackedByteArray>` **encrypt** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PackedByteArray<class_PackedByteArray>` plaintext **)**
  120. Encrypt the given ``plaintext`` with the provided public ``key``.
  121. \ **Note:** The maximum size of accepted plaintext is limited by the key size.
  122. .. rst-class:: classref-item-separator
  123. ----
  124. .. _class_Crypto_method_generate_random_bytes:
  125. .. rst-class:: classref-method
  126. :ref:`PackedByteArray<class_PackedByteArray>` **generate_random_bytes** **(** :ref:`int<class_int>` size **)**
  127. Generates a :ref:`PackedByteArray<class_PackedByteArray>` of cryptographically secure random bytes with given ``size``.
  128. .. rst-class:: classref-item-separator
  129. ----
  130. .. _class_Crypto_method_generate_rsa:
  131. .. rst-class:: classref-method
  132. :ref:`CryptoKey<class_CryptoKey>` **generate_rsa** **(** :ref:`int<class_int>` size **)**
  133. Generates an RSA :ref:`CryptoKey<class_CryptoKey>` that can be used for creating self-signed certificates and passed to :ref:`StreamPeerTLS.accept_stream<class_StreamPeerTLS_method_accept_stream>`.
  134. .. rst-class:: classref-item-separator
  135. ----
  136. .. _class_Crypto_method_generate_self_signed_certificate:
  137. .. rst-class:: classref-method
  138. :ref:`X509Certificate<class_X509Certificate>` **generate_self_signed_certificate** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`String<class_String>` issuer_name="CN=myserver,O=myorganisation,C=IT", :ref:`String<class_String>` not_before="20140101000000", :ref:`String<class_String>` not_after="20340101000000" **)**
  139. Generates a self-signed :ref:`X509Certificate<class_X509Certificate>` from the given :ref:`CryptoKey<class_CryptoKey>` and ``issuer_name``. The certificate validity will be defined by ``not_before`` and ``not_after`` (first valid date and last valid date). The ``issuer_name`` must contain at least "CN=" (common name, i.e. the domain name), "O=" (organization, i.e. your company name), "C=" (country, i.e. 2 lettered ISO-3166 code of the country the organization is based in).
  140. A small example to generate an RSA key and a X509 self-signed certificate.
  141. .. tabs::
  142. .. code-tab:: gdscript
  143. var crypto = Crypto.new()
  144. # Generate 4096 bits RSA key.
  145. var key = crypto.generate_rsa(4096)
  146. # Generate self-signed certificate using the given key.
  147. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  148. .. code-tab:: csharp
  149. var crypto = new Crypto();
  150. // Generate 4096 bits RSA key.
  151. CryptoKey key = crypto.GenerateRsa(4096);
  152. // Generate self-signed certificate using the given key.
  153. X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_Crypto_method_hmac_digest:
  157. .. rst-class:: classref-method
  158. :ref:`PackedByteArray<class_PackedByteArray>` **hmac_digest** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` key, :ref:`PackedByteArray<class_PackedByteArray>` msg **)**
  159. Generates an `HMAC <https://en.wikipedia.org/wiki/HMAC>`__ digest of ``msg`` using ``key``. The ``hash_type`` parameter is the hashing algorithm that is used for the inner and outer hashes.
  160. Currently, only :ref:`HashingContext.HASH_SHA256<class_HashingContext_constant_HASH_SHA256>` and :ref:`HashingContext.HASH_SHA1<class_HashingContext_constant_HASH_SHA1>` are supported.
  161. .. rst-class:: classref-item-separator
  162. ----
  163. .. _class_Crypto_method_sign:
  164. .. rst-class:: classref-method
  165. :ref:`PackedByteArray<class_PackedByteArray>` **sign** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` hash, :ref:`CryptoKey<class_CryptoKey>` key **)**
  166. Sign a given ``hash`` of type ``hash_type`` with the provided private ``key``.
  167. .. rst-class:: classref-item-separator
  168. ----
  169. .. _class_Crypto_method_verify:
  170. .. rst-class:: classref-method
  171. :ref:`bool<class_bool>` **verify** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PackedByteArray<class_PackedByteArray>` hash, :ref:`PackedByteArray<class_PackedByteArray>` signature, :ref:`CryptoKey<class_CryptoKey>` key **)**
  172. Verify that a given ``signature`` for ``hash`` of type ``hash_type`` against the provided public ``key``.
  173. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  174. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  175. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  176. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  177. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  178. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`