Crypto

Inherits: Reference < Object

Acceso a funcionalidades criptográficas avanzadas.

Descripción

La clase Criptográfica le permite acceder a algunas funcionalidades criptográficas más avanzadas en Godot.

Por ahora, esto incluye la generación de bytes aleatorios criptográficamente seguros, claves RSA y generación de certificados X509 autofirmados, encriptación/desencriptación de claves asimétricas y firma/verificación.

extends Node

var crypto = Crypto.new()
var clave = CryptoKey.new()
var certificado = X509Certificate.new()

func _ready():
    # Genera una clave nueva RSA.
    clave = crypto.generate_rsa(4096)
    # Genera un certificado autofirmado con la clave dada.
    certificado = crypto.generate_self_signed_certificate(clave, "CN=mydomain.com,O=My Game Company,C=IT")
    # Guarda la clave y el certificado en un directorio del usuario.
    clave.save("user://generada.key")
    certificado.save("user://generada.crt")
    # Encripción
    var datos = "Algunos datos"
    var encriptado = crypto.encrypt(clave, datos.to_utf8())
    # Desencriptado
    var desencriptado = crypto.decrypt(clave, encriptado)
    # Firmada
    var firma = crypto.sign(HashingContext.HASH_SHA256, datos.sha256_buffer(), clave)
    # Verificar
    var verificado = crypto.verify(HashingContext.HASH_SHA256, datos.sha256_buffer(), firma, clave)
    # Cheque
    assert(verificado)
    assert(datos.to_utf8() == desencriptado)

Nota: No disponible en los exportables HTML5.

Métodos

bool

constant_time_compare ( PoolByteArray trusted, PoolByteArray received )

PoolByteArray

decrypt ( CryptoKey key, PoolByteArray ciphertext )

PoolByteArray

encrypt ( CryptoKey key, PoolByteArray plaintext )

PoolByteArray

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" )

PoolByteArray

hmac_digest ( HashType hash_type, PoolByteArray key, PoolByteArray msg )

PoolByteArray

sign ( HashType hash_type, PoolByteArray hash, CryptoKey key )

bool

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

Descripciones de Métodos

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

See this blog post for more information.


Decrypt the given ciphertext with the provided private key.

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


Encrypt the given plaintext with the provided public key.

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


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


Genera una RSA CryptoKey que puede ser utilizada para crear certificados autofirmados y pasarla a StreamPeerSSL.accept_stream.


Genera un X509Certificate autofirmado a partir de la CryptoKey y el issuer_name dados. La validez del certificado se definirá mediante not_before y not_after (primera fecha de validez y última fecha de validez). El issuer_name debe contener al menos "CN=" (nombre común, es decir, el nombre del dominio), "O=" (organización, es decir, el nombre de su empresa), "C=" (país, es decir, el código ISO-3166 de dos letras del país en el que la organización tiene su sede).

Un pequeño ejemplo para generar una clave RSA y un certificado autofirmado X509.

var criptografia = Crypto.new()
# Genera una clave de 4096 bits RSA.
var clave = criptografia.generate_rsa(4096)
# Genera un certificado autofirmado usando la clave.
var certificado = criptografia.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")

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.


Firma un hash de tipo hash_type con la key privada proporcionada.


Verifique que un signature dado para hash de tipo hash_tipo contra el key público proporcionado.