Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

Crypto

Inherits: RefCounted < Object

Provides access to advanced cryptographic functionalities.

Description

The Crypto class provides access to advanced cryptographic functionalities.

Currently, this includes asymmetric key encryption/decryption, signing/verification, and generating cryptographically secure random bytes, RSA keys, HMAC digests, and self-signed X509Certificates.

GDScriptC#

  1. extends Node
  2. var crypto = Crypto.new()
  3. var key = CryptoKey.new()
  4. var cert = X509Certificate.new()
  5. func _ready():
  6. # Generate new RSA key.
  7. key = crypto.generate_rsa(4096)
  8. # Generate new self-signed certificate with the given key.
  9. cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
  10. # Save key and certificate in the user folder.
  11. key.save("user://generated.key")
  12. cert.save("user://generated.crt")
  13. # Encryption
  14. var data = "Some data"
  15. var encrypted = crypto.encrypt(key, data.to_utf8_buffer())
  16. # Decryption
  17. var decrypted = crypto.decrypt(key, encrypted)
  18. # Signing
  19. var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
  20. # Verifying
  21. var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
  22. # Checks
  23. assert(verified)
  24. assert(data.to_utf8_buffer() == decrypted)
  1. using Godot;
  2. using System.Diagnostics;
  3. public partial class MyNode : Node
  4. {
  5. private Crypto _crypto = new Crypto();
  6. private CryptoKey _key = new CryptoKey();
  7. private X509Certificate _cert = new X509Certificate();
  8. public override void _Ready()
  9. {
  10. // Generate new RSA key.
  11. _key = _crypto.GenerateRsa(4096);
  12. // Generate new self-signed certificate with the given key.
  13. _cert = _crypto.GenerateSelfSignedCertificate(_key, "CN=mydomain.com,O=My Game Company,C=IT");
  14. // Save key and certificate in the user folder.
  15. _key.Save("user://generated.key");
  16. _cert.Save("user://generated.crt");
  17. // Encryption
  18. string data = "Some data";
  19. byte[] encrypted = _crypto.Encrypt(_key, data.ToUtf8Buffer());
  20. // Decryption
  21. byte[] decrypted = _crypto.Decrypt(_key, encrypted);
  22. // Signing
  23. byte[] signature = _crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), _key);
  24. // Verifying
  25. bool verified = _crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, _key);
  26. // Checks
  27. Debug.Assert(verified);
  28. Debug.Assert(data.ToUtf8Buffer() == decrypted);
  29. }
  30. }

Methods

bool

constant_time_compare ( PackedByteArray trusted, PackedByteArray received )

PackedByteArray

decrypt ( CryptoKey key, PackedByteArray ciphertext )

PackedByteArray

encrypt ( CryptoKey key, PackedByteArray plaintext )

PackedByteArray

generate_random_bytes ( int size )

CryptoKey

generate_rsa ( int size )

X509Certificate

generate_self_signed_certificate ( CryptoKey key, String issuer_name=”CN=myserver,O=myorganisation,C=IT”, String not_before=”20140101000000”, String not_after=”20340101000000” )

PackedByteArray

hmac_digest ( HashType hash_type, PackedByteArray key, PackedByteArray msg )

PackedByteArray

sign ( HashType hash_type, PackedByteArray hash, CryptoKey key )

bool

verify ( HashType hash_type, PackedByteArray hash, PackedByteArray signature, CryptoKey key )


Method Descriptions

bool constant_time_compare ( PackedByteArray trusted, PackedByteArray received )

Compares two PackedByteArrays for equality without leaking timing information in order to prevent timing attacks.

See this blog post for more information.


PackedByteArray decrypt ( CryptoKey key, PackedByteArray ciphertext )

Decrypt the given ciphertext with the provided private key.

Note: The maximum size of accepted ciphertext is limited by the key size.


PackedByteArray encrypt ( CryptoKey key, PackedByteArray plaintext )

Encrypt the given plaintext with the provided public key.

Note: The maximum size of accepted plaintext is limited by the key size.


PackedByteArray generate_random_bytes ( int size )

Generates a PackedByteArray of cryptographically secure random bytes with given size.


CryptoKey generate_rsa ( int size )

Generates an RSA CryptoKey that can be used for creating self-signed certificates and passed to StreamPeerTLS.accept_stream.


X509Certificate generate_self_signed_certificate ( CryptoKey key, String issuer_name=”CN=myserver,O=myorganisation,C=IT”, String not_before=”20140101000000”, String not_after=”20340101000000” )

Generates a self-signed X509Certificate from the given 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).

A small example to generate an RSA key and a X509 self-signed certificate.

GDScriptC#

  1. var crypto = Crypto.new()
  2. # Generate 4096 bits RSA key.
  3. var key = crypto.generate_rsa(4096)
  4. # Generate self-signed certificate using the given key.
  5. var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
  1. var crypto = new Crypto();
  2. // Generate 4096 bits RSA key.
  3. CryptoKey key = crypto.GenerateRsa(4096);
  4. // Generate self-signed certificate using the given key.
  5. X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");

PackedByteArray hmac_digest ( HashType hash_type, PackedByteArray key, PackedByteArray msg )

Generates an HMAC digest of msg using key. The hash_type parameter is the hashing algorithm that is used for the inner and outer hashes.

Currently, only HashingContext.HASH_SHA256 and HashingContext.HASH_SHA1 are supported.


PackedByteArray sign ( HashType hash_type, PackedByteArray hash, CryptoKey key )

Sign a given hash of type hash_type with the provided private key.


bool verify ( HashType hash_type, PackedByteArray hash, PackedByteArray signature, CryptoKey key )

Verify that a given signature for hash of type hash_type against the provided public key.