mirror of
https://github.com/godotengine/godot.git
synced 2026-01-06 10:11:57 +03:00
Fix C# examples in documentation
- Fix documentation after C# renames. - Add missing `partial` in C# class declarations. - Change `delta` parameter type to `double` in C#. - Ensure parameters match base declaration. - Use `$` string interpolation in C#. - Fix invalid or outdated C# code. - Changed some examples to follow our style guide more closely.
This commit is contained in:
@@ -9,9 +9,11 @@
|
||||
[codeblocks]
|
||||
[gdscript]
|
||||
extends Node
|
||||
|
||||
var crypto = Crypto.new()
|
||||
var key = CryptoKey.new()
|
||||
var cert = X509Certificate.new()
|
||||
|
||||
func _ready():
|
||||
# Generate new RSA key.
|
||||
key = crypto.generate_rsa(4096)
|
||||
@@ -35,35 +37,35 @@
|
||||
[/gdscript]
|
||||
[csharp]
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
public class CryptoNode : Node
|
||||
public partial class MyNode : Node
|
||||
{
|
||||
public Crypto Crypto = new Crypto();
|
||||
public CryptoKey Key = new CryptoKey();
|
||||
public X509Certificate Cert = new X509Certificate();
|
||||
private Crypto _crypto = new Crypto();
|
||||
private CryptoKey _key = new CryptoKey();
|
||||
private X509Certificate _cert = new X509Certificate();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// Generate new RSA key.
|
||||
Key = Crypto.GenerateRsa(4096);
|
||||
_key = _crypto.GenerateRsa(4096);
|
||||
// Generate new self-signed certificate with the given key.
|
||||
Cert = Crypto.GenerateSelfSignedCertificate(Key, "CN=mydomain.com,O=My Game Company,C=IT");
|
||||
_cert = _crypto.GenerateSelfSignedCertificate(_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");
|
||||
_key.Save("user://generated.key");
|
||||
_cert.Save("user://generated.crt");
|
||||
// Encryption
|
||||
string data = "Some data";
|
||||
byte[] encrypted = Crypto.Encrypt(Key, data.ToUTF8());
|
||||
byte[] encrypted = _crypto.Encrypt(_key, data.ToUtf8());
|
||||
// Decryption
|
||||
byte[] decrypted = Crypto.Decrypt(Key, encrypted);
|
||||
byte[] decrypted = _crypto.Decrypt(_key, encrypted);
|
||||
// Signing
|
||||
byte[] signature = Crypto.Sign(HashingContext.HashType.Sha256, Data.SHA256Buffer(), Key);
|
||||
byte[] signature = _crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), _key);
|
||||
// Verifying
|
||||
bool verified = Crypto.Verify(HashingContext.HashType.Sha256, Data.SHA256Buffer(), signature, Key);
|
||||
bool verified = _crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, _key);
|
||||
// Checks
|
||||
Debug.Assert(verified);
|
||||
Debug.Assert(data.ToUTF8() == decrypted);
|
||||
Debug.Assert(data.ToUtf8() == decrypted);
|
||||
}
|
||||
}
|
||||
[/csharp]
|
||||
|
||||
Reference in New Issue
Block a user