<commit_before>﻿using System;
using System.Linq;
using Moq;
using SafeOrbit.Cryptography.Encryption;
using SafeOrbit.Tests;

namespace SafeOrbit.Fakes
{
    /// <seealso cref="IFastEncryptor" />
    public class FastEncryptorFaker : StubProviderBase<IFastEncryptor>
    {
        public override IFastEncryptor Provide()
        {
            byte[] Encrypt(byte[] input, byte[] key) =>
                input.Concat(key).Concat(BitConverter.GetBytes(Convert.ToBase64String(input).GetHashCode())).ToArray();
            byte[] Decrypt(byte[] input, byte[] key)
            {
                var plainLength = input.Length - key.Length - sizeof(int);
                var rawBytes = input.Take(plainLength).ToArray();
                var keyBytes = input.Skip(plainLength).Take(key.Length).ToArray();
                if (!key.SequenceEqual(keyBytes))
                    throw new ArgumentException(
                        $"Wrong encryption key.{Environment.NewLine}" +
                        $"Expected was:{Environment.NewLine}" +
                        string.Join(",", key.Select(b => b)) + Environment.NewLine +
                        $"But got: {Environment.NewLine}" +
                        string.Join(",", keyBytes.Select(b => b)));
                return rawBytes;
            }

            var fake = new Mock<IFastEncryptor>();
            fake.Setup(f => f.Encrypt(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .Returns<byte[],byte[]>(Encrypt);
            fake.Setup(f => f.Decrypt(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .Returns<byte[], byte[]>(Decrypt);
            fake.Setup(f => f.EncryptAsync(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .ReturnsAsync((byte[] i, byte[] k) => Encrypt(i, k));
            fake.Setup(f => f.DecryptAsync(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .ReturnsAsync((byte[] i, byte[] k) => Decrypt(i, k));
            return fake.Object;
        }
    }
}<commit_msg>static inner local functions<commit_after>﻿using System;
using System.Linq;
using Moq;
using SafeOrbit.Cryptography.Encryption;
using SafeOrbit.Tests;

namespace SafeOrbit.Fakes
{
    /// <seealso cref="IFastEncryptor" />
    public class FastEncryptorFaker : StubProviderBase<IFastEncryptor>
    {
        public override IFastEncryptor Provide()
        {
            static byte[] Encrypt(byte[] input, byte[] key) =>
                input.Concat(key).Concat(BitConverter.GetBytes(Convert.ToBase64String(input).GetHashCode())).ToArray();
            static byte[] Decrypt(byte[] input, byte[] key)
            {
                var plainLength = input.Length - key.Length - sizeof(int);
                var rawBytes = input.Take(plainLength).ToArray();
                var keyBytes = input.Skip(plainLength).Take(key.Length).ToArray();
                if (!key.SequenceEqual(keyBytes))
                    throw new ArgumentException(
                        $"Wrong encryption key.{Environment.NewLine}" +
                        $"Expected was:{Environment.NewLine}" +
                        string.Join(",", key.Select(b => b)) + Environment.NewLine +
                        $"But got: {Environment.NewLine}" +
                        string.Join(",", keyBytes.Select(b => b)));
                return rawBytes;
            }

            var fake = new Mock<IFastEncryptor>();
            fake.Setup(f => f.Encrypt(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .Returns<byte[],byte[]>(Encrypt);
            fake.Setup(f => f.Decrypt(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .Returns<byte[], byte[]>(Decrypt);
            fake.Setup(f => f.EncryptAsync(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .ReturnsAsync((byte[] i, byte[] k) => Encrypt(i, k));
            fake.Setup(f => f.DecryptAsync(It.IsAny<byte[]>(), It.IsAny<byte[]>()))
                .ReturnsAsync((byte[] i, byte[] k) => Decrypt(i, k));
            return fake.Object;
        }
    }
}
{'fraction_non_alphanumeric': 0.16, 'fraction_numerical': 0.0010062893081761006, 'mean_word_length': 2.0584615384615383, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3398895', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1148, 'n_words': 203}