mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
* make lidgren use spans everywhere where it can convert custom pooling to shared array pool impl add unit tests for read/write add native socket extensions to socket so we can legit pass spans for SendTo/ReceiveFrom bump version in lidgren csproj replace some random "% 8" w/ "& 7" more minor nullability hacks to fix static analysis complaints made receiving packets use span minor native sockets refactor to use pinvoke add read/write constrained/prealloc'd bit stream impl to lidgren and update usages fixed missing stream cleanup remove outstanding stream cleanup since it refs buffer thru the class, can't read some other buf apply suggestions from code review remove unsafe cruft * add tests to gh actions * make stats use interpolation in tostring and remove m_bytesAllocated since it's all in the shared pool now * this pr still open so fuck it stats, human readability, faster BitsToHold methods * add api compatible version of ReadBytes * rename ReadOnlyStreamWrapper -> ReadOnlyWrapperStream rename WriteOnlyStreamWrapper -> WriteOnlyWrapperStream add AppendViaStream, AppenderStream impl add and update documentation on read/write bytes methods also fix some goofs
84 lines
2.0 KiB
C#
84 lines
2.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace Lidgren.Network
|
|
{
|
|
|
|
public abstract class NetCryptoProviderBase : NetEncryption
|
|
{
|
|
|
|
protected SymmetricAlgorithm m_algorithm;
|
|
|
|
public NetCryptoProviderBase(NetPeer peer, SymmetricAlgorithm algo)
|
|
: base(peer)
|
|
{
|
|
m_algorithm = algo;
|
|
m_algorithm.GenerateKey();
|
|
m_algorithm.GenerateIV();
|
|
}
|
|
|
|
public override void SetKey(ReadOnlySpan<byte> data, int offset, int count)
|
|
{
|
|
int len = m_algorithm.Key.Length;
|
|
// ReSharper disable once SuggestVarOrType_Elsewhere
|
|
var key = new byte[len];
|
|
for (int i = 0; i < len; i++)
|
|
key[i] = data[offset + (i % count)];
|
|
m_algorithm.Key = key;
|
|
|
|
len = m_algorithm.IV.Length;
|
|
key = new byte[len];
|
|
for (int i = 0; i < len; i++)
|
|
key[len - 1 - i] = data[offset + (i % count)];
|
|
m_algorithm.IV = key;
|
|
}
|
|
|
|
public override bool Encrypt(NetOutgoingMessage msg)
|
|
{
|
|
int unEncLenBits = msg.LengthBits;
|
|
|
|
using var ms = new MemoryStream(msg.LengthBytes);
|
|
var cs = new CryptoStream(ms, m_algorithm.CreateEncryptor(), CryptoStreamMode.Write);
|
|
cs.Write(msg.m_data.Slice(0, msg.LengthBytes));
|
|
cs.Close();
|
|
|
|
// get results
|
|
var arr = ms.ToArray();
|
|
ms.Close();
|
|
|
|
msg.EnsureBufferSize((arr.Length + 4) * 8);
|
|
msg.LengthBits = 0; // reset write pointer
|
|
msg.Write((uint) unEncLenBits);
|
|
msg.Write(arr);
|
|
msg.LengthBits = (arr.Length + 4) * 8;
|
|
|
|
return true;
|
|
}
|
|
|
|
public override bool Decrypt(NetIncomingMessage msg)
|
|
{
|
|
int unEncLenBits = (int) msg.ReadUInt32();
|
|
|
|
byte[] result;
|
|
using var ms = new MemoryStream(msg.m_buf, 4, msg.LengthBytes - 4);
|
|
var cs = new CryptoStream(ms, m_algorithm.CreateDecryptor(), CryptoStreamMode.Read);
|
|
|
|
var byteLen = NetUtility.BytesToHoldBits(unEncLenBits);
|
|
result = m_peer.GetStorage(byteLen);
|
|
cs.Read(result, 0, byteLen);
|
|
cs.Close();
|
|
|
|
// TODO: recycle existing msg
|
|
|
|
msg.m_buf = result;
|
|
msg.m_bitLength = unEncLenBits;
|
|
msg.m_readPosition = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
}
|