reckfellENGINE

Crypto

Provides access to advanced cryptographic functionalities.

On this page
class CryptoReckfellSharpInherits RefCounted

Provides access to advanced cryptographic functionalities.

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.

C#
using Reckfell;
using System.Diagnostics;

Crypto crypto = new Crypto();

// Generate new RSA key.
CryptoKey key = crypto.GenerateRsa(4096);

// Generate new self-signed certificate with the given key.
X509Certificate 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");

// Encryption
string data = "Some data";
byte[] encrypted = crypto.Encrypt(key, data.ToUtf8Buffer());

// Decryption
byte[] decrypted = crypto.Decrypt(key, encrypted);

// Signing
byte[] signature = crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), key);

// Verifying
bool verified = crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, key);

// Checks
Debug.Assert(verified);
Debug.Assert(data.ToUtf8Buffer() == decrypted);

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

CryptoConstructor
# Crypto
C#
public Crypto();

Methods15

ConstantTimeCompare2 overloads
# ConstantTimeCompare · 1/2
C#
public bool ConstantTimeCompare(byte[] trusted, byte[] received);

Compares two byte[]s for equality without leaking timing information in order to prevent timing attacks. See this blog post for more information.

Parameters
NameTypeDefault
trustedbyte[]—
receivedbyte[]—
Returns bool
# ConstantTimeCompare · 2/2
C#
public bool ConstantTimeCompare(ReadOnlySpan<byte> trusted, ReadOnlySpan<byte> received);

Compares two byte[]s for equality without leaking timing information in order to prevent timing attacks. See this blog post for more information.

Parameters
NameTypeDefault
trustedReadOnlySpan<byte>—
receivedReadOnlySpan<byte>—
Returns bool
Decrypt2 overloads
# Decrypt · 1/2
C#
public byte[] Decrypt(CryptoKey key, byte[] ciphertext);

Decrypt the given ciphertext with the provided private key. Note: The maximum size of accepted ciphertext is limited by the key size.

Parameters
NameTypeDefault
keyCryptoKey—
ciphertextbyte[]—
Returns byte[]
# Decrypt · 2/2
C#
public byte[] Decrypt(CryptoKey key, ReadOnlySpan<byte> ciphertext);

Decrypt the given ciphertext with the provided private key. Note: The maximum size of accepted ciphertext is limited by the key size.

Parameters
NameTypeDefault
keyCryptoKey—
ciphertextReadOnlySpan<byte>—
Returns byte[]
Encrypt2 overloads
# Encrypt · 1/2
C#
public byte[] Encrypt(CryptoKey key, byte[] plaintext);

Encrypt the given plaintext with the provided public key. Note: The maximum size of accepted plaintext is limited by the key size.

Parameters
NameTypeDefault
keyCryptoKey—
plaintextbyte[]—
Returns byte[]
# Encrypt · 2/2
C#
public byte[] Encrypt(CryptoKey key, ReadOnlySpan<byte> plaintext);

Encrypt the given plaintext with the provided public key. Note: The maximum size of accepted plaintext is limited by the key size.

Parameters
NameTypeDefault
keyCryptoKey—
plaintextReadOnlySpan<byte>—
Returns byte[]
GenerateRandomBytesbyte[]
# GenerateRandomBytes
C#
public byte[] GenerateRandomBytes(int size);

Generates a byte[] of cryptographically secure random bytes with given size.

Parameters
NameTypeDefault
sizeint—
Returns byte[]
GenerateRsaCryptoKey
# GenerateRsa
C#
public CryptoKey GenerateRsa(int size);

Generates an RSA CryptoKey that can be used for creating self-signed certificates and passed to StreamPeerTls.AcceptStream.

Parameters
NameTypeDefault
sizeint—
Returns CryptoKey
GenerateSelfSignedCertificateX509Certificate
# GenerateSelfSignedCertificate
C#
public X509Certificate GenerateSelfSignedCertificate(CryptoKey key, string issuerName = "CN=myserver,O=myorganisation,C=IT", string notBefore = "20140101000000", string notAfter = "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.

C#
var crypto = new Crypto();
// Generate 4096 bits RSA key.
CryptoKey key = crypto.GenerateRsa(4096);
// Generate self-signed certificate using the given key.
X509Certificate cert = crypto.GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT");
Parameters
NameTypeDefault
keyCryptoKey—
issuerNamestring"CN=myserver,O=myorganisation,C=IT"
notBeforestring"20140101000000"
notAfterstring"20340101000000"
Returns X509Certificate
HmacDigest2 overloads
# HmacDigest · 1/2
C#
public byte[] HmacDigest(HashingContext.HashType hashType, byte[] key, byte[] msg);

Generates an HMAC(https://en.wikipedia.org/wiki/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.HashType.Sha256 and HashingContext.HashType.Sha1 are supported.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
keybyte[]—
msgbyte[]—
Returns byte[]
# HmacDigest · 2/2
C#
public byte[] HmacDigest(HashingContext.HashType hashType, ReadOnlySpan<byte> key, ReadOnlySpan<byte> msg);

Generates an HMAC(https://en.wikipedia.org/wiki/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.HashType.Sha256 and HashingContext.HashType.Sha1 are supported.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
keyReadOnlySpan<byte>—
msgReadOnlySpan<byte>—
Returns byte[]
Sign2 overloads
# Sign · 1/2
C#
public byte[] Sign(HashingContext.HashType hashType, byte[] hash, CryptoKey key);

Sign a given hash of type hash_type with the provided private key.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
hashbyte[]—
keyCryptoKey—
Returns byte[]
# Sign · 2/2
C#
public byte[] Sign(HashingContext.HashType hashType, ReadOnlySpan<byte> hash, CryptoKey key);

Sign a given hash of type hash_type with the provided private key.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
hashReadOnlySpan<byte>—
keyCryptoKey—
Returns byte[]
Verify2 overloads
# Verify · 1/2
C#
public bool Verify(HashingContext.HashType hashType, byte[] hash, byte[] signature, CryptoKey key);

Verify that a given signature for hash of type hash_type against the provided public key.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
hashbyte[]—
signaturebyte[]—
keyCryptoKey—
Returns bool
# Verify · 2/2
C#
public bool Verify(HashingContext.HashType hashType, ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, CryptoKey key);

Verify that a given signature for hash of type hash_type against the provided public key.

Parameters
NameTypeDefault
hashTypeHashingContext.HashType—
hashReadOnlySpan<byte>—
signatureReadOnlySpan<byte>—
keyCryptoKey—
Returns bool
Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.