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()
            };
        }
Ejemplo n.º 2
0
    public ChaCha20Poly1305Crypto(ReadOnlySpan <byte> key)
    {
        if (key.Length < KeySize)
        {
            throw new ArgumentException(@"Key length must be 32 bytes.", nameof(key));
        }

        _chacha20 = StreamCryptoCreate.ChaCha20(key);

        _buffer = ArrayPool <byte> .Shared.Rent(32);
    }
 protected override IStreamCrypto CreateCrypto(bool isEncrypt, ReadOnlySpan <byte> key, ReadOnlySpan <byte> iv)
 {
     return(StreamCryptoCreate.ChaCha20(key, iv));
 }