3using System.Threading.Tasks;
19 private readonly
byte[] key;
25 this.instance = Instance;
33 public byte[]
Key => this.key;
64 if (!
string.IsNullOrEmpty(ContractId))
66 Tuple<SymmetricCipherAlgorithms, string, byte[]> T = await Client.TryLoadContractSharedSecret(ContractId);
68 if (!(T.Item3 is
null))
70 DefaultAlgorithm = T.Item1;
76 return await
Create(ContractId, DefaultAlgorithm, Client, CreatorJid,
Key);
97 if (!
string.IsNullOrEmpty(ContractId))
98 await Client.SaveContractSharedSecret(ContractId, CreatorJid,
Key,
Algorithm,
false);
114 public byte[]
Encrypt(
string ParameterName,
string ParameterType, uint ParameterIndex,
string CreatorJid,
byte[] ContractNonce,
119 Data =
new byte[this.key.Length + ContractNonce.Length + 4];
120 int c = this.key.Length;
121 int d = ContractNonce.Length;
123 Array.Copy(this.key, 0, Data, 0, c);
124 Array.Copy(ContractNonce, 0, Data, c, d);
125 Array.Copy(BitConverter.GetBytes(ParameterIndex), 0, Data, c + d, 4);
129 int SuffixLength = Data[0];
131 if (ClearText is
null)
132 Data =
new byte[SuffixLength];
138 for (i = 1, c = Data.Length; i < c; i++)
140 if ((j = Data[i]) != 0)
150 byte[] Data2 = Encoding.UTF8.GetBytes(ClearText);
153 Data =
new byte[1 + c + SuffixLength];
155 Data[0] = (byte)Prefix;
156 Array.Copy(Data2, 0, Data, 1, c);
159 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid,
160 Convert.ToBase64String(ContractNonce), ParameterIndex);
161 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
178 public string Decrypt(
string ParameterName,
string ParameterType, uint ParameterIndex,
string CreatorJid,
byte[] ContractNonce,
181 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid, Convert.ToBase64String(ContractNonce), ParameterIndex);
182 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
183 byte[] Data = this.instance.Decrypt(CipherText, this.key, IV, AssociatedData);
185 if (Data.Length == 0 || Data[0] == 0)
188 int c = Data.Length - 1;
190 while (c >= 0 && Data[c] == 0)
193 return Encoding.UTF8.GetString(Data, 1, c);
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
byte[] Key
Key used to encrypt parameters.
static Task< ParameterEncryptionAlgorithm > Create(SymmetricCipherAlgorithms Algorithm, ContractsClient Client)
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
SymmetricCipherAlgorithms Algorithm
Symmetric Cipher Algorithm used to encrypt parameters.
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms DefaultAlgorithm, ContractsClient Client)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms Algorithm, ContractsClient Client, string CreatorJid, byte[] Key)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
string Decrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, byte[] CipherText)
Decrypts an encrypted parameter value.
byte[] Encrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, string ClearText)
Encrypts a parameter value.
Abstract base class for symmetric ciphers.
static IE2eSymmetricCipher Create(SymmetricCipherAlgorithms Algorithm)
Creates an instance of a symmetric cipher algorithm.
XmppClient Client
XMPP Client.
Contains methods for simple hash calculations.
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Interface for parameter encryption algorithms.
Interface for symmetric ciphers.
byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
SymmetricCipherAlgorithms
Enumeration of symmetric cipher algorithms available in the library.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.