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
71 lines
1.5 KiB
C#
71 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Lidgren.Network
|
|
{
|
|
/// <summary>
|
|
/// Example class; not very good encryption
|
|
/// </summary>
|
|
public class NetXorEncryption : NetEncryption
|
|
{
|
|
private Memory<byte> m_key;
|
|
|
|
/// <summary>
|
|
/// NetXorEncryption constructor
|
|
/// </summary>
|
|
public NetXorEncryption(NetPeer peer, Memory<byte> key)
|
|
: base(peer)
|
|
{
|
|
m_key = key;
|
|
}
|
|
|
|
public override void SetKey(ReadOnlySpan<byte> data, int offset, int count)
|
|
{
|
|
m_key = new byte[count];
|
|
data.CopyTo(m_key.Span);
|
|
}
|
|
|
|
/// <summary>
|
|
/// NetXorEncryption constructor
|
|
/// </summary>
|
|
public NetXorEncryption(NetPeer peer, string key)
|
|
: base(peer)
|
|
{
|
|
m_key = Encoding.UTF8.GetBytes(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Encrypt an outgoing message
|
|
/// </summary>
|
|
public override bool Encrypt(NetOutgoingMessage msg)
|
|
{
|
|
int numBytes = msg.LengthBytes;
|
|
var dataSpan = msg.m_data;
|
|
var keySpan = m_key.Span;
|
|
for (int i = 0; i < numBytes; i++)
|
|
{
|
|
int offset = i % keySpan.Length;
|
|
dataSpan[i] = (byte)(dataSpan[i] ^ keySpan[offset]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decrypt an incoming message
|
|
/// </summary>
|
|
public override bool Decrypt(NetIncomingMessage msg)
|
|
{
|
|
int numBytes = msg.LengthBytes;
|
|
var keySpan = m_key.Span;
|
|
var dataSpan = msg.m_data;
|
|
for (int i = 0; i < numBytes; i++)
|
|
{
|
|
int offset = i % keySpan.Length;
|
|
dataSpan[i] = (byte)(dataSpan[i] ^ keySpan[offset]);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|