class_crypto.rst 16 KB

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