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

提供對高階加密功能的存取。

說明

Crypto 類提供對高階加密功能的存取。

目前,包括非對稱金鑰的加密/解密和簽名/驗證、生成加密安全隨機位元組、RSA 金鑰、HMAC 摘要以及自簽名的 X509Certificate。

var crypto = Crypto.new()

# 生成新的 RSA 金鑰。
var key = crypto.generate_rsa(4096)

# 使用給定的金鑰生成新的自簽名憑證。
var cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")

# 將金鑰和憑證儲存在使用者資料夾中。
key.save("user://generated.key")
cert.save("user://generated.crt")

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

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

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

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

# 校驗
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") 🔗

根據給定的 CryptoKey 和 issuer_name 生成自簽名的 X509Certificate。憑證有效性將由 not_before 和 not_after(第一個有效日期和最後一個有效日期)定義。issuer_name 必須至少包含“CN=”(通用名稱,即域名)、“O=”(組織,即你的公司名稱)、“C=”(國家,即 2 個字母的該組織所在的國家/地區的 ISO-3166 程式碼)。

生成 RSA 金鑰和 X509 自簽名憑證的小範例。

var crypto = Crypto.new()
# 生成 4096 位元 RSA 金鑰。
var key = crypto.generate_rsa(4096)
# 使用給定的金鑰生成自簽名憑證。
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 生成 msg 的 HMAC 摘要。hash_type 參數是用於內部和外部雜湊的雜湊算法。

目前僅支援 HashingContext.HASH_SHA256 和 HashingContext.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 型別之雜湊值 hash 的簽章 signature。