Crypto
Provides access to advanced cryptographic functionalities.
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.
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
public Crypto();Methods15
ConstantTimeCompare2 overloads
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.
| Name | Type | Default |
|---|---|---|
trusted | byte[] | — |
received | byte[] | — |
boolpublic 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.
| Name | Type | Default |
|---|---|---|
trusted | ReadOnlySpan<byte> | — |
received | ReadOnlySpan<byte> | — |
boolDecrypt2 overloads
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.
| Name | Type | Default |
|---|---|---|
key | CryptoKey | — |
ciphertext | byte[] | — |
byte[]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.
| Name | Type | Default |
|---|---|---|
key | CryptoKey | — |
ciphertext | ReadOnlySpan<byte> | — |
byte[]Encrypt2 overloads
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.
| Name | Type | Default |
|---|---|---|
key | CryptoKey | — |
plaintext | byte[] | — |
byte[]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.
| Name | Type | Default |
|---|---|---|
key | CryptoKey | — |
plaintext | ReadOnlySpan<byte> | — |
byte[]GenerateRandomBytesbyte[]
public byte[] GenerateRandomBytes(int size);Generates a byte[] of cryptographically secure random bytes with given size.
| Name | Type | Default |
|---|---|---|
size | int | — |
byte[]GenerateRsaCryptoKey
public CryptoKey GenerateRsa(int size);Generates an RSA CryptoKey that can be used for creating self-signed certificates and passed to StreamPeerTls.AcceptStream.
| Name | Type | Default |
|---|---|---|
size | int | — |
CryptoKeyGenerateSelfSignedCertificateX509Certificate
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.
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");| Name | Type | Default |
|---|---|---|
key | CryptoKey | — |
issuerName | string | "CN=myserver,O=myorganisation,C=IT" |
notBefore | string | "20140101000000" |
notAfter | string | "20340101000000" |
X509CertificateHmacDigest2 overloads
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.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
key | byte[] | — |
msg | byte[] | — |
byte[]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.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
key | ReadOnlySpan<byte> | — |
msg | ReadOnlySpan<byte> | — |
byte[]Sign2 overloads
public byte[] Sign(HashingContext.HashType hashType, byte[] hash, CryptoKey key);Sign a given hash of type hash_type with the provided private key.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
hash | byte[] | — |
key | CryptoKey | — |
byte[]public byte[] Sign(HashingContext.HashType hashType, ReadOnlySpan<byte> hash, CryptoKey key);Sign a given hash of type hash_type with the provided private key.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
hash | ReadOnlySpan<byte> | — |
key | CryptoKey | — |
byte[]Verify2 overloads
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.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
hash | byte[] | — |
signature | byte[] | — |
key | CryptoKey | — |
boolpublic 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.
| Name | Type | Default |
|---|---|---|
hashType | HashingContext.HashType | — |
hash | ReadOnlySpan<byte> | — |
signature | ReadOnlySpan<byte> | — |
key | CryptoKey | — |
bool