Skip to content
eduardgushchin edited this page Oct 5, 2026 · 1 revision

Home | API by Category | Complete API Index | API Status

Field Value
Full name Electron2D.Crypto
Namespace Electron2D
Kind class
Category API Networking

Overview

Generates keys/certificates and performs RSA, digest-signature, HMAC and secure-random operations.

Syntax

public class Electron2D.Crypto

Remarks

Operations/disposal require the constructing thread. Key operations import an independent BCL key snapshot under the resource lock and release it after the call. Generated resources are caller-owned. Key generation, import, asymmetric operations and copied results are cold work; prepared contexts handle streams.

Inherits: ElectronObject

Detailed engine reference

Members

Member Kind Summary
public Crypto() constructor Creates a cryptographic operation provider.
public System.Boolean ConstantTimeCompare(System.ReadOnlySpan<System.Byte> trusted, System.ReadOnlySpan<System.Byte> received) method Compares equal-length byte strings without content-dependent early exit.
public System.Byte[] Decrypt(Electron2D.CryptoKey key, System.ReadOnlySpan<System.Byte> ciphertext) method Decrypts one modulus-sized RSA ciphertext using a private key and PKCS#1 v1.5 padding.
public System.Byte[] Encrypt(Electron2D.CryptoKey key, System.ReadOnlySpan<System.Byte> plaintext) method Encrypts one RSA plaintext using PKCS#1 v1.5 padding.
public Electron2D.CryptoKey GenerateRSA(System.Int32 size) method Generates a private RSA key resource.
public System.Byte[] GenerateRandomBytes(System.Int32 size) method Generates copied cryptographically secure random bytes.
public System.Void GenerateRandomBytes(System.Span<System.Byte> destination) method Fills caller storage with cryptographically secure random bytes.
public Electron2D.X509Certificate GenerateSelfSignedCertificate(Electron2D.CryptoKey key, System.String issuerName = "CN=myserver,O=myorganisation,C=IT", System.String notBefore = "20140101000000", System.String notAfter = "20340101000000") method Generates a SHA-256 signed X.509 v3 self-signed CA certificate with path length zero.
public System.Byte[] HMACDigest(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> key, System.ReadOnlySpan<System.Byte> message) method Computes a complete HMAC-SHA1/HMAC-SHA256 message digest.
public System.Byte[] Sign(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> hash, Electron2D.CryptoKey key) method Signs a precomputed MD5/SHA1/SHA256 digest.
protected override System.Void ValidateDisposal() method
public System.Boolean Verify(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> hash, System.ReadOnlySpan<System.Byte> signature, Electron2D.CryptoKey key) method Verifies a precomputed digest and RSA PKCS#1/ECDSA DER signature.

Member Details

Crypto()

Kind: constructor

public Crypto()

Summary

Creates a cryptographic operation provider.

ConstantTimeCompare(System.ReadOnlySpan<System.Byte>, System.ReadOnlySpan<System.Byte>)

Kind: method

public System.Boolean ConstantTimeCompare(System.ReadOnlySpan<System.Byte> trusted, System.ReadOnlySpan<System.Byte> received)

Summary

Compares equal-length byte strings without content-dependent early exit.

Remarks

Lengths are public to the comparison; different lengths reject before comparing content.

Returns

True for equal bytes, including two empty spans; false for different lengths or content.

Parameters

  • trusted: Trusted bytes.
  • received: Received bytes.

Decrypt(Electron2D.CryptoKey, System.ReadOnlySpan<System.Byte>)

Kind: method

public System.Byte[] Decrypt(Electron2D.CryptoKey key, System.ReadOnlySpan<System.Byte> ciphertext)

Summary

Decrypts one modulus-sized RSA ciphertext using a private key and PKCS#1 v1.5 padding.

Returns

Caller-owned plaintext bytes.

Parameters

  • key: Loaded private RSA key.
  • ciphertext: One RSA ciphertext.

Exceptions

  • T:System.Security.Cryptography.CryptographicException: Key role, ciphertext length or padding is invalid.

Encrypt(Electron2D.CryptoKey, System.ReadOnlySpan<System.Byte>)

Kind: method

public System.Byte[] Encrypt(Electron2D.CryptoKey key, System.ReadOnlySpan<System.Byte> plaintext)

Summary

Encrypts one RSA plaintext using PKCS#1 v1.5 padding.

Returns

One modulus-sized ciphertext.

Parameters

  • key: Loaded public or private RSA key.
  • plaintext: At most modulus byte length minus eleven.

Exceptions

  • T:System.NotSupportedException: An EC key is supplied.
  • T:System.Security.Cryptography.CryptographicException: The key or plaintext size is invalid.

GenerateRSA(System.Int32)

Kind: method

public Electron2D.CryptoKey GenerateRSA(System.Int32 size)

Summary

Generates a private RSA key resource.

Returns

A caller-owned canonical private key.

Parameters

  • size: Key size in bits, subject to the current BCL provider's supported sizes.

Exceptions

  • T:System.Security.Cryptography.CryptographicException: Key size or generation is unsupported.

GenerateRandomBytes(System.Int32)

Kind: method

public System.Byte[] GenerateRandomBytes(System.Int32 size)

Summary

Generates copied cryptographically secure random bytes.

Returns

A caller-owned array of the requested length.

Parameters

  • size: Nonnegative requested byte count; zero is valid.

Exceptions

  • T:System.ArgumentOutOfRangeException: Size is negative.

GenerateRandomBytes(System.Span<System.Byte>)

Kind: method

public System.Void GenerateRandomBytes(System.Span<System.Byte> destination)

Summary

Fills caller storage with cryptographically secure random bytes.

Parameters

  • destination: Borrowed output storage, including an empty span.

GenerateSelfSignedCertificate(Electron2D.CryptoKey, System.String, System.String, System.String)

Kind: method

public Electron2D.X509Certificate GenerateSelfSignedCertificate(Electron2D.CryptoKey key, System.String issuerName = "CN=myserver,O=myorganisation,C=IT", System.String notBefore = "20140101000000", System.String notAfter = "20340101000000")

Summary

Generates a SHA-256 signed X.509 v3 self-signed CA certificate with path length zero.

Returns

A caller-owned certificate resource without private-key material.

Parameters

  • key: Loaded RSA/EC private key, borrowed during the call.
  • issuerName: Subject/issuer X.500 name containing nonempty CN, O and two-letter C.
  • notBefore: First valid UTC instant as yyyyMMddHHmmss.
  • notAfter: Last valid UTC instant in the same format, strictly later.

Exceptions

  • T:System.ArgumentException: Name or dates are invalid.
  • T:System.Security.Cryptography.CryptographicException: A usable private key is unavailable.

HMACDigest(Electron2D.HashType, System.ReadOnlySpan<System.Byte>, System.ReadOnlySpan<System.Byte>)

Kind: method

public System.Byte[] HMACDigest(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> key, System.ReadOnlySpan<System.Byte> message)

Summary

Computes a complete HMAC-SHA1/HMAC-SHA256 message digest.

Returns

Caller-owned authentication bytes.

Parameters

  • hashType: SHA1 or SHA256.
  • key: Borrowed nonempty authentication key.
  • message: Borrowed nonempty message bytes.

Exceptions

  • T:System.NotSupportedException: MD5 is selected.
  • T:System.ArgumentException: Key or message is empty.

Sign(Electron2D.HashType, System.ReadOnlySpan<System.Byte>, Electron2D.CryptoKey)

Kind: method

public System.Byte[] Sign(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> hash, Electron2D.CryptoKey key)

Summary

Signs a precomputed MD5/SHA1/SHA256 digest.

Returns

RSA PKCS#1 v1.5 or ECDSA DER signature bytes.

Parameters

  • hashType: Digest identity.
  • hash: Exactly 16, 20 or 32 bytes.
  • key: Loaded private RSA/EC key.

Exceptions

  • T:System.ArgumentException: Digest length is invalid.
  • T:System.Security.Cryptography.CryptographicException: The key or signing operation is invalid.

ValidateDisposal()

Kind: method

protected override System.Void ValidateDisposal()

Verify(Electron2D.HashType, System.ReadOnlySpan<System.Byte>, System.ReadOnlySpan<System.Byte>, Electron2D.CryptoKey)

Kind: method

public System.Boolean Verify(Electron2D.HashType hashType, System.ReadOnlySpan<System.Byte> hash, System.ReadOnlySpan<System.Byte> signature, Electron2D.CryptoKey key)

Summary

Verifies a precomputed digest and RSA PKCS#1/ECDSA DER signature.

Returns

False for a mismatched or malformed signature.

Parameters

  • hashType: Digest identity.
  • hash: Exactly the selected digest length.
  • signature: Untrusted signature bytes.
  • key: Loaded public or private RSA/EC key.

Exceptions

  • T:System.ArgumentException: The supplied digest has the wrong length.

Clone this wiki locally