class_crypto.rst 17 KB

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