class_crypto.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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:`Reference<class_Reference>` **<** :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. ::
  15. extends Node
  16. var crypto = Crypto.new()
  17. var key = CryptoKey.new()
  18. var cert = X509Certificate.new()
  19. func _ready():
  20. # Generate new RSA key.
  21. key = crypto.generate_rsa(4096)
  22. # Generate new self-signed certificate with the given key.
  23. cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
  24. # Save key and certificate in the user folder.
  25. key.save("user://generated.key")
  26. cert.save("user://generated.crt")
  27. # Encryption
  28. var data = "Some data"
  29. var encrypted = crypto.encrypt(key, data.to_utf8())
  30. # Decryption
  31. var decrypted = crypto.decrypt(key, encrypted)
  32. # Signing
  33. var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
  34. # Verifying
  35. var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
  36. # Checks
  37. assert(verified)
  38. assert(data.to_utf8() == decrypted)
  39. **Note:** Not available in HTML5 exports.
  40. Methods
  41. -------
  42. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`bool<class_bool>` | :ref:`constant_time_compare<class_Crypto_method_constant_time_compare>` **(** :ref:`PoolByteArray<class_PoolByteArray>` trusted, :ref:`PoolByteArray<class_PoolByteArray>` received **)** |
  44. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`decrypt<class_Crypto_method_decrypt>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PoolByteArray<class_PoolByteArray>` ciphertext **)** |
  46. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`encrypt<class_Crypto_method_encrypt>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PoolByteArray<class_PoolByteArray>` plaintext **)** |
  48. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`generate_random_bytes<class_Crypto_method_generate_random_bytes>` **(** :ref:`int<class_int>` size **)** |
  50. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`CryptoKey<class_CryptoKey>` | :ref:`generate_rsa<class_Crypto_method_generate_rsa>` **(** :ref:`int<class_int>` size **)** |
  52. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :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" **)** |
  54. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`hmac_digest<class_Crypto_method_hmac_digest>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` key, :ref:`PoolByteArray<class_PoolByteArray>` msg **)** |
  56. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`sign<class_Crypto_method_sign>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` hash, :ref:`CryptoKey<class_CryptoKey>` key **)** |
  58. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`bool<class_bool>` | :ref:`verify<class_Crypto_method_verify>` **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` hash, :ref:`PoolByteArray<class_PoolByteArray>` signature, :ref:`CryptoKey<class_CryptoKey>` key **)** |
  60. +-----------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. Method Descriptions
  62. -------------------
  63. .. _class_Crypto_method_constant_time_compare:
  64. - :ref:`bool<class_bool>` **constant_time_compare** **(** :ref:`PoolByteArray<class_PoolByteArray>` trusted, :ref:`PoolByteArray<class_PoolByteArray>` received **)**
  65. Compares two :ref:`PoolByteArray<class_PoolByteArray>`\ s for equality without leaking timing information in order to prevent timing attacks.
  66. See `this blog post <https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy>`__ for more information.
  67. ----
  68. .. _class_Crypto_method_decrypt:
  69. - :ref:`PoolByteArray<class_PoolByteArray>` **decrypt** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PoolByteArray<class_PoolByteArray>` ciphertext **)**
  70. Decrypt the given ``ciphertext`` with the provided private ``key``.
  71. **Note:** The maximum size of accepted ciphertext is limited by the key size.
  72. ----
  73. .. _class_Crypto_method_encrypt:
  74. - :ref:`PoolByteArray<class_PoolByteArray>` **encrypt** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`PoolByteArray<class_PoolByteArray>` plaintext **)**
  75. Encrypt the given ``plaintext`` with the provided public ``key``.
  76. **Note:** The maximum size of accepted plaintext is limited by the key size.
  77. ----
  78. .. _class_Crypto_method_generate_random_bytes:
  79. - :ref:`PoolByteArray<class_PoolByteArray>` **generate_random_bytes** **(** :ref:`int<class_int>` size **)**
  80. Generates a :ref:`PoolByteArray<class_PoolByteArray>` of cryptographically secure random bytes with given ``size``.
  81. ----
  82. .. _class_Crypto_method_generate_rsa:
  83. - :ref:`CryptoKey<class_CryptoKey>` **generate_rsa** **(** :ref:`int<class_int>` size **)**
  84. 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>`.
  85. ----
  86. .. _class_Crypto_method_generate_self_signed_certificate:
  87. - :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" **)**
  88. 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).
  89. A small example to generate an RSA key and a X509 self-signed certificate.
  90. ::
  91. var crypto = Crypto.new()
  92. # Generate 4096 bits RSA key.
  93. var key = crypto.generate_rsa(4096)
  94. # Generate self-signed certificate using the given key.
  95. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  96. ----
  97. .. _class_Crypto_method_hmac_digest:
  98. - :ref:`PoolByteArray<class_PoolByteArray>` **hmac_digest** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` key, :ref:`PoolByteArray<class_PoolByteArray>` msg **)**
  99. 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.
  100. Currently, only :ref:`HashingContext.HASH_SHA256<class_HashingContext_constant_HASH_SHA256>` and :ref:`HashingContext.HASH_SHA1<class_HashingContext_constant_HASH_SHA1>` are supported.
  101. ----
  102. .. _class_Crypto_method_sign:
  103. - :ref:`PoolByteArray<class_PoolByteArray>` **sign** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` hash, :ref:`CryptoKey<class_CryptoKey>` key **)**
  104. Sign a given ``hash`` of type ``hash_type`` with the provided private ``key``.
  105. ----
  106. .. _class_Crypto_method_verify:
  107. - :ref:`bool<class_bool>` **verify** **(** :ref:`HashType<enum_HashingContext_HashType>` hash_type, :ref:`PoolByteArray<class_PoolByteArray>` hash, :ref:`PoolByteArray<class_PoolByteArray>` signature, :ref:`CryptoKey<class_CryptoKey>` key **)**
  108. Verify that a given ``signature`` for ``hash`` of type ``hash_type`` against the provided public ``key``.
  109. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  110. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  111. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`