Crypto
Hérite de : RefCounted < Object
Fournit un accès à des fonctionnalités cryptographiques avancées.
Description
La classe Crypto permet d'accéder à des fonctionnalités cryptographiques plus avancées.
Pour l'instant, cela inclut l'encryptage/décryptage de clé asymétrique, la signature/vérification, et la génération cryptographique sécurisée de bytes aléatoires, de clés RSA, de HMAC et de X509Certificates auto-signés.
var crypto = Crypto.new()
# Générer une nouvelle clé RSA.
var key = crypto.generate_rsa(4096)
# Générer un nouveau certificat auto-signé avec le clé.
var cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
# Enregistrer la clé et le certificat dans le dossier utilisateur.
key.save("user://generated.key")
cert.save("user://generated.crt")
# Cryptage
var data = "Some data"
var encrypted = crypto.encrypt(key, data.to_utf8_buffer())
# Décryptage
var decrypted = crypto.decrypt(key, encrypted)
# Signature
var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
# Vérification
var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
# Tests
assert(verified)
assert(data.to_utf8_buffer() == decrypted)
using Godot;
using System.Diagnostics;
Crypto crypto = new Crypto();
// Générer une nouvelle clé RSA.
CryptoKey key = crypto.GenerateRsa(4096);
// Générer un nouveau certificat auto-signé avec le clé.
X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
// Enregistrer la clé et le certificat dans le dossier utilisateur.
key.Save("user://generated.key");
cert.Save("user://generated.crt");
// Cryptage
string data = "Some data";
byte[] encrypted = crypto.Encrypt(key, data.ToUtf8Buffer());
// Décryptage
byte[] decrypted = crypto.Decrypt(key, encrypted);
// Signature
byte[] signature = crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), key);
// Vérification
bool verified = crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, key);
// Tests
Debug.Assert(verified);
Debug.Assert(data.ToUtf8Buffer() == decrypted);
Méthodes
constant_time_compare(trusted: PackedByteArray, received: PackedByteArray) |
|
decrypt(key: CryptoKey, ciphertext: PackedByteArray) |
|
encrypt(key: CryptoKey, plaintext: PackedByteArray) |
|
generate_random_bytes(size: int) |
|
generate_rsa(size: int) |
|
generate_self_signed_certificate(key: CryptoKey, issuer_name: String = "CN=myserver,O=myorganisation,C=IT", not_before: String = "20140101000000", not_after: String = "20340101000000") |
|
hmac_digest(hash_type: HashType, key: PackedByteArray, msg: PackedByteArray) |
|
sign(hash_type: HashType, hash: PackedByteArray, key: CryptoKey) |
|
verify(hash_type: HashType, hash: PackedByteArray, signature: PackedByteArray, key: CryptoKey) |
Descriptions des méthodes
bool constant_time_compare(trusted: PackedByteArray, received: PackedByteArray) 🔗
Compare deux PackedByteArrays pour l'égalité sans fuite temporelle d'informations afin de prévenir les attaques temporelles.
Voir ce post de blog pour plus d'informations.
PackedByteArray decrypt(key: CryptoKey, ciphertext: PackedByteArray) 🔗
Déchiffre le texte crypté ciphertext donné avec le clé privée key fournie.
Note : La taille maximale pour le texte crypté est limitée par la taille de la clé.
PackedByteArray encrypt(key: CryptoKey, plaintext: PackedByteArray) 🔗
Encrypte le texte brut plaintext donné avec le clé publique key fournie.
Note : La taille maximale pour le texte brut est limitée par la taille de la clé.
PackedByteArray generate_random_bytes(size: int) 🔗
Génère un PackedByteArray d'octets aléatoires de manière cryptographiquement sécurisée avec la taille size donnée.
CryptoKey generate_rsa(size: int) 🔗
Génère une CryptoKey RSA qui peut être utilisé pour créer des certificats autosignés et transmis à StreamPeerTLS.accept_stream().
X509Certificate generate_self_signed_certificate(key: CryptoKey, issuer_name: String = "CN=myserver,O=myorganisation,C=IT", not_before: String = "20140101000000", not_after: String = "20340101000000") 🔗
Génère un X509Certificate auto-signé avec la clé CryptoKey et le nom d'émetteur issuer_name donnés. La date de validité du certificat est définie par not_before et not_after (la date de début et de fin de validité). Le nom issuer_name doit contenir au moins "CN=" (le nom commun, c.à.d. le nom de domaine), "O=" (l'organisation, c.à.d. le nom de votre entreprise) et "C=" (le pays, c.à.d. un code ISO-3166 à deux lettres du pays où l'entreprise est située).
Un court exemple pour générer une clé RSA et un certificat X509 auto-signé.
var crypto = Crypto.new()
# Générer un clé RSA 4096 bits.
var cle = crypto.generate_rsa(4096)
# Générer un certificat auto-signé à partir de la clé donnée.
var cert = crypto.generate_self_signed_certificate(cle, "CN=example.com,O=Mon Studio de Jeux,C=FR")
var crypto = new Crypto();
// Générer un clé RSA 4096 bits.
CryptoKey cle = crypto.GenerateRsa(4096);
// Générer un certificat auto-signé à partir de la clé donnée.
X509Certificate cert = crypto.GenerateSelfSignedCertificate(cle, "CN=example.com,O=Mon Studio de Jeux, C=FR");
PackedByteArray hmac_digest(hash_type: HashType, key: PackedByteArray, msg: PackedByteArray) 🔗
Génère un résumé HMAC du message msg à partir de la clé key. Le paramètre hash_type est l'algorithme de hachage utilisé pour les hachages intérieurs et extérieurs.
Actuellement, seuls les algorithmes HashingContext.HASH_SHA256 et HashingContext.HASH_SHA1 sont supportés.
PackedByteArray sign(hash_type: HashType, hash: PackedByteArray, key: CryptoKey) 🔗
Signe le hachage hash donné de type hash_type avec la clé privée key fournie.
bool verify(hash_type: HashType, hash: PackedByteArray, signature: PackedByteArray, key: CryptoKey) 🔗
Vérifie la signature donnée pour le hachage hash du type hash_type avec la clé publique key fournie.