protected override void InitCipher(byte[] iv, bool isEncrypt)
        {
            base.InitCipher(iv, isEncrypt);
            _crypto?.Dispose();

            if (cipherFamily == CipherFamily.Rc4Md5)
            {
                Span <byte> temp    = stackalloc byte[keyLen + ivLen];
                var         realKey = new byte[MD5Length];
                key.CopyTo(temp);
                iv.CopyTo(temp.Slice(keyLen));
                MD5Utils.Fast440(temp, realKey);

                _crypto = StreamCryptoCreate.Rc4(realKey);
                return;
            }

            _crypto = cipherFamily switch
            {
                CipherFamily.AesCfb => StreamCryptoCreate.AesCfb(isEncrypt, key, iv),
                CipherFamily.Chacha20 => StreamCryptoCreate.ChaCha20(key, iv),
                CipherFamily.Rc4 => StreamCryptoCreate.Rc4(key),
                _ => throw new NotSupportedException()
            };
        }
Exemplo n.º 2
0
    private static void Test(IStreamCrypto crypto, int originSize, string hex, string hex2)
    {
        Assert.AreEqual(@"RC4", crypto.Name);

        Span <byte> h1 = hex.FromHex();
        Span <byte> h2 = hex2.FromHex();

        Span <byte> i = new byte[originSize];
        Span <byte> o = stackalloc byte[i.Length];

        crypto.Update(i, o);
        Assert.IsTrue(o.SequenceEqual(h1));

        crypto.Update(i, o);
        Assert.IsTrue(o.SequenceEqual(h2));

        crypto.Reset();

        crypto.Update(h1, o);
        Assert.IsTrue(o.SequenceEqual(i));

        crypto.Update(h2, o);
        Assert.IsTrue(o.SequenceEqual(i));

        crypto.Dispose();
    }
Exemplo n.º 3
0
    private static void Test(IStreamCrypto crypto, Span <byte> origin)
    {
        Span <byte> o = stackalloc byte[origin.Length];

        crypto.Update(origin, o);

        crypto.Dispose();
    }
Exemplo n.º 4
0
        private static void Test(IStreamCrypto crypto, string hex, string hex2)
        {
            Assert.AreEqual(@"AES-CFB", crypto.Name);

            Span <byte> h1 = hex.FromHex();
            Span <byte> h2 = hex2.FromHex();

            Span <byte> o = stackalloc byte[h1.Length];

            crypto.Update(h1, o);
            Assert.IsTrue(o.SequenceEqual(h2));

            crypto.Reset();

            crypto.Update(h1.Slice(0, 73), o);
            crypto.Update(h1.Slice(73), o.Slice(73));
            Assert.IsTrue(o.SequenceEqual(h2));

            crypto.Dispose();
        }
Exemplo n.º 5
0
        private const int Step = 1 * 1024 * 1024;         // 1 MB

        public static void Test(IStreamCrypto crypto)
        {
            ReadOnlySpan <byte> i = new byte[Step];
            Span <byte>         o = new byte[Step];

            var sw     = Stopwatch.StartNew();
            var length = 0ul;

            do
            {
                crypto.Update(i, o);
                ++length;
            } while (sw.ElapsedMilliseconds < 2000);

            sw.Stop();
            crypto.Dispose();

            var result = length * Step / sw.Elapsed.TotalSeconds / 1024.0 / 1024.0;

            Console.WriteLine($@"{result:F2} MB/s");
        }
Exemplo n.º 6
0
    public void Test(IStreamCrypto crypto)
    {
        Span <byte> o            = new byte[_step];
        ulong       length       = 0ul;
        double      totalSeconds = 0.0;

        do
        {
            ReadOnlySpan <byte> i  = RandomNumberGenerator.GetBytes(_step);
            Stopwatch           sw = Stopwatch.StartNew();
            crypto.Update(i, o);
            sw.Stop();
            totalSeconds += sw.Elapsed.TotalSeconds;
            ++length;
        } while (totalSeconds < _duration);

        crypto.Dispose();

        double result = length * (ulong)_step / totalSeconds / 1024.0 / 1024.0;

        Console.WriteLine($@"{result:F2} MB/s");
    }
 protected virtual void InitCipher(bool isEncrypt)
 {
     Crypto?.Dispose();
     Crypto = CreateCrypto(isEncrypt, KeySpan, IvSpan);
 }