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.

Crypto

继承: RefCounted < Object

提供对高阶加密功能的访问。

描述

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.

var crypto = Crypto.new()

# Generate new RSA key.
var key = crypto.generate_rsa(4096)

# Generate new self-signed certificate with the given key.
var cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")

# Save key and certificate in the user folder.
key.save("user://generated.key")
cert.save("user://generated.crt")

# Encryption
var data = "Some data"
var encrypted = crypto.encrypt(key, data.to_utf8_buffer())

# Decryption
var decrypted = crypto.decrypt(key, encrypted)

# Signing
var signature = crypto.sign(HashingContext.HASH_SHA256, data.sha256_buffer(), key)

# Verifying
var verified = crypto.verify(HashingContext.HASH_SHA256, data.sha256_buffer(), signature, key)

# Checks
assert(verified)
assert(data.to_utf8_buffer() == decrypted)

方法

bool

constant_time_compare(trusted: PackedByteArray, received: PackedByteArray)

PackedByteArray

decrypt(key: CryptoKey, ciphertext: PackedByteArray)

PackedByteArray

encrypt(key: CryptoKey, plaintext: PackedByteArray)

PackedByteArray

generate_random_bytes(size: int)

CryptoKey

generate_rsa(size: int)

X509Certificate

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

PackedByteArray

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

PackedByteArray

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

bool

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


方法说明

bool constant_time_compare(trusted: PackedByteArray, received: PackedByteArray) 🔗

比较两个 PackedByteArray 是否相等,不会泄漏时序信息,能够防止时序攻击。

详情见这篇博文


PackedByteArray decrypt(key: CryptoKey, ciphertext: PackedByteArray) 🔗

用提供的私钥 key 解密给定的密文 ciphertext

注意:所接受的密文的最大尺寸受到密钥大小的限制。


PackedByteArray encrypt(key: CryptoKey, plaintext: PackedByteArray) 🔗

用提供的公钥 key 加密给定的明文 plaintext

注意:所接受的明文的最大尺寸受到密钥大小的限制。


PackedByteArray generate_random_bytes(size: int) 🔗

生成具有给定大小 size 的加密安全随机字节的 PackedByteArray


CryptoKey generate_rsa(size: int) 🔗

生成可用于创建自签名证书并传递给 StreamPeerTLS.accept_stream 的 RSA CryptoKey


X509Certificate generate_self_signed_certificate(key: CryptoKey, issuer_name: String = "CN=myserver,O=myorganisation,C=IT", not_before: String = "20140101000000", not_after: String = "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 an X509 self-signed certificate.

var crypto = Crypto.new()
# Generate 4096 bits RSA key.
var key = crypto.generate_rsa(4096)
# Generate self-signed certificate using the given key.
var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")

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

使用密钥 key 生成 msgHMAC 摘要。hash_type 参数是用于内部和外部哈希的哈希算法。

目前仅支持 HashingContext.HASH_SHA256HashingContext.HASH_SHA1


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

使用提供的私钥 key 对类型为 hash_type 的给定 hash 进行签名。


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

使用提供的公钥 key 验证类型为 hash_type 的给定签名 signature