Пример #1
0
        public async Task ComcryptDecomcryptTest()
        {
            var message = new Message {
                StringMessage = $"Sensitive ReceivedLetter 0", MessageId = 0
            };
            var data = JsonSerializer.SerializeToUtf8Bytes(message);

            var hashKey = await ArgonHash
                          .GetHashKeyAsync(Passphrase, Salt, HouseofCat.Encryption.Constants.Aes256.KeySize)
                          .ConfigureAwait(false);

            _output.WriteLine(Encoding.UTF8.GetString(hashKey));
            _output.WriteLine($"HashKey: {Encoding.UTF8.GetString(hashKey)}");

            // Comcrypt
            var payload = await Gzip.CompressAsync(data);

            var encryptedPayload = AesEncrypt.Aes256Encrypt(payload, hashKey);

            // Decomcrypt
            var decryptedData = AesEncrypt.Aes256Decrypt(encryptedPayload, hashKey);

            Assert.NotNull(decryptedData);

            var decompressed = await Gzip.DecompressAsync(decryptedData);

            JsonSerializer.SerializeToUtf8Bytes(decompressed);
            _output.WriteLine($"Data: {Encoding.UTF8.GetString(data)}");
            _output.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decryptedData)}");

            Assert.Equal(data, decompressed);
        }
Пример #2
0
        public bool Encrypt(Letter letter)
        {
            if (letter.LetterMetadata.Encrypted || (_hashKey == null && _hashKey.Length == 0))
            {
                return(false);
            }                 // Don't double encrypt.

            try
            {
                letter.Body = AesEncrypt.Aes256Encrypt(letter.Body, _hashKey);
                letter.LetterMetadata.Encrypted = true;
                letter.LetterMetadata.CustomFields[Constants.HeaderForEncrypted]   = true;
                letter.LetterMetadata.CustomFields[Constants.HeaderForEncryption]  = Constants.HeaderValueForArgonAesEncrypt;
                letter.LetterMetadata.CustomFields[Constants.HeaderForEncryptDate] = Time.GetDateTimeNow(Time.Formats.CatRFC3339);
            }
            catch { return(false); }

            return(true);
        }
Пример #3
0
        public async Task EncryptDecryptTest()
        {
            var data = new byte[] { 0xFF, 0x00, 0xAA, 0xFF, 0x00, 0x00, 0xFF, 0xAA, 0x00, 0xFF, 0x00, 0xFF };

            var hashKey = await ArgonHash
                          .GetHashKeyAsync(Passphrase, Salt, 32)
                          .ConfigureAwait(false);

            _output.WriteLine(Encoding.UTF8.GetString(hashKey));
            _output.WriteLine($"HashKey: {Encoding.UTF8.GetString(hashKey)}");

            var encryptedData = AesEncrypt.Aes256Encrypt(data, hashKey);

            _output.WriteLine($"Encrypted: {Encoding.UTF8.GetString(encryptedData)}");

            var decryptedData = AesEncrypt.Aes256Decrypt(encryptedData, hashKey);

            _output.WriteLine($"Data: {Encoding.UTF8.GetString(data)}");
            _output.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decryptedData)}");

            Assert.Equal(data, decryptedData);
        }
Пример #4
0
        private async Task ProcessDeliveriesAsync(ChannelReader <Letter> channelReader)
        {
            while (await channelReader.WaitToReadAsync().ConfigureAwait(false))
            {
                while (channelReader.TryRead(out var letter))
                {
                    if (letter == null)
                    {
                        continue;
                    }

                    if (_compress)
                    {
                        letter.Body = await Gzip.CompressAsync(letter.Body).ConfigureAwait(false);

                        letter.LetterMetadata.Compressed = _compress;
                        letter.LetterMetadata.CustomFields[Constants.HeaderForCompressed]  = _compress;
                        letter.LetterMetadata.CustomFields[Constants.HeaderForCompression] = Constants.HeaderValueForGzipCompress;
                    }

                    if (_encrypt)
                    {
                        letter.Body = AesEncrypt.Aes256Encrypt(letter.Body, _hashKey);
                        letter.LetterMetadata.Encrypted = _encrypt;
                        letter.LetterMetadata.CustomFields[Constants.HeaderForEncrypted]   = _encrypt;
                        letter.LetterMetadata.CustomFields[Constants.HeaderForEncryption]  = Constants.HeaderValueForArgonAesEncrypt;
                        letter.LetterMetadata.CustomFields[Constants.HeaderForEncryptDate] = Time.GetDateTimeNow(Time.Formats.CatRFC3339);
                    }

                    _logger.LogDebug(LogMessages.AutoPublishers.LetterPublished, letter.LetterId, letter.LetterMetadata?.Id);

                    await PublishAsync(letter, _createPublishReceipts, _withHeaders)
                    .ConfigureAwait(false);
                }
            }
        }
Пример #5
0
 public void EncryptDecrypt512()
 {
     var decryptedData = AesEncrypt.Aes256Decrypt(AesEncrypt.Aes256Encrypt(Payload2, HashKey), HashKey);
 }
Пример #6
0
 public void Encrypt2048()
 {
     var encryptedData = AesEncrypt.Aes256Encrypt(Payload4, HashKey);
 }
Пример #7
0
 public void Encrypt1024()
 {
     var encryptedData = AesEncrypt.Aes256Encrypt(Payload3, HashKey);
 }