Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Crypto
Eredita: RefCounted < Object
Fornisce accesso a funzionalità crittografiche avanzate.
Descrizione
La classe Crypto fornisce accesso a funzionalità crittografiche avanzate.
Attualmente, ciò include crittografia/decrittografia a chiave asimmetrica, firma/verifica e generazione di byte casuali crittograficamente sicuri, chiavi RSA, digest HMAC e X509Certificate autofirmati.
var crypto = Crypto.new()
# Genera una nuova chiave RSA.
var key = crypto.generate_rsa(4096)
# Genera un nuovo certificato autofirmato con la chiave specificata.
var cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
# Salva la chiave e il certificato nella cartella utente.
key.save("user://generated.key")
cert.save("user://generated.crt")
# Crittografia
var data = "Some data"
var encrypted = crypto.encrypt(key, data.to_utf8_buffer())
# Decrittografia
var decrypted = crypto.decrypt(key, encrypted)
# Firma
var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)
# Verifica
var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)
# Controlli
assert(verified)
assert(data.to_utf8_buffer() == decrypted)
using Godot;
using System.Diagnostics;
Crypto crypto = new Crypto();
// Genera una nuova chiave RSA.
CryptoKey key = crypto.GenerateRsa(4096);
// Genera un nuovo certificato autofirmato con la chiave specificata.
X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
// Salva la chiave e il certificato nella cartella utente.
key.Save("user://generated.key");
cert.Save("user://generated.crt");
// Crittografia
string data = "Some data";
byte[] encrypted = crypto.Encrypt(key, data.ToUtf8Buffer());
// Decrittografia
byte[] decrypted = crypto.Decrypt(key, encrypted);
// Firma
byte[] signature = crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), key);
// Verifica
bool verified = crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, key);
// Controlli
Debug.Assert(verified);
Debug.Assert(data.ToUtf8Buffer() == decrypted);
Metodi
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) |
Descrizioni dei metodi
bool constant_time_compare(trusted: PackedByteArray, received: PackedByteArray) 🔗
Confronta due PackedByteArray per verificarne l'uguaglianza senza divulgare informazioni sul tempo, in modo da prevenire attacchi temporali.
Consulta questo post su un blog per ulteriori informazioni.
PackedByteArray decrypt(key: CryptoKey, ciphertext: PackedByteArray) 🔗
Decritta il testo cifrato ciphertext con la chiave privata key.
Nota: La dimensione massima del testo cifrato accettato è limitata dalla dimensione della chiave.
PackedByteArray encrypt(key: CryptoKey, plaintext: PackedByteArray) 🔗
Crittografa il testo semplice plaintext con la chiave publica key.
Nota: La dimensione massima del testo semplice accettato è limitata dalla dimensione della chiave.
PackedByteArray generate_random_bytes(size: int) 🔗
Genera un PackedByteArray di byte casuali crittograficamente sicuri con la dimensione size.
CryptoKey generate_rsa(size: int) 🔗
Genera un CryptoKey RSA che può essere utilizzato per creare certificati autofirmati e passato a 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") 🔗
Genera un X509Certificate autofirmato dal CryptoKey e il mittente issuer_name forniti. La validità del certificato sarà definita da not_before e not_after (prima data valida e ultima data valida). issuer_name deve contenere almeno "CN=" (nome comune, ovvero il nome di dominio), "O=" (organizzazione, ovvero il nome della tua azienda), "C=" (paese, ovvero il codice ISO-3166 a 2 lettere del paese in cui ha sede l'organizzazione).
Un piccolo esempio per generare una chiave RSA e un certificato autofirmato X509.
var crypto = Crypto.new()
# Genera una chiave RSA a 4096 bit.
var key = crypto.generate_rsa(4096)
# Genera un certificato autofirmato utilizzando la chiave fornita.
var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
var crypto = new Crypto();
// Genera una chiave RSA da 4096 bit.
CryptoKey key = crypto.GenerateRsa(4096);
// Genera un certificato autofirmato utilizzando la chiave specificata.
X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
PackedByteArray hmac_digest(hash_type: HashType, key: PackedByteArray, msg: PackedByteArray) 🔗
Genera un digest HMAC del messaggo msg usando la chiave key. Il parametro hash_type è l'algoritmo di hashing usato per gli hash interni ed esterni.
Attualmente, sono supportati solo HashingContext.HASH_SHA256 e HashingContext.HASH_SHA1.
PackedByteArray sign(hash_type: HashType, hash: PackedByteArray, key: CryptoKey) 🔗
Firma un determinato hash di tipo hash_type con la chiave privata key fornita.
bool verify(hash_type: HashType, hash: PackedByteArray, signature: PackedByteArray, key: CryptoKey) 🔗
Verifica che una determinata firma signature per l'hash di tipo hash_type sia compatibile con la chiave pubblica key fornita.