Skip to content

Commit

Permalink
Apply scoped to some instances of Span<T>/ReadOnlySpan<T>.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Aug 26, 2023
1 parent 2fbb555 commit 5d7e27e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/server/web/Cryptography/PasswordStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public static bool IsPasswordValid(string password)

public abstract byte[] GenerateSalt();

public abstract byte[] CalculateHash(ReadOnlySpan<char> password, ReadOnlySpan<byte> salt);
public abstract byte[] CalculateHash(scoped ReadOnlySpan<char> password, ReadOnlySpan<byte> salt);
}
2 changes: 1 addition & 1 deletion src/server/web/Cryptography/Pbkdf2PasswordStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override byte[] GenerateSalt()
return RandomNumberGenerator.GetBytes(Length);
}

public override byte[] CalculateHash(ReadOnlySpan<char> password, ReadOnlySpan<byte> salt)
public override byte[] CalculateHash(scoped ReadOnlySpan<char> password, ReadOnlySpan<byte> salt)
{
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2
return Rfc2898DeriveBytes.Pbkdf2(password, salt, 210_000, HashAlgorithmName.SHA512, Length);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/common/IO/StreamAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public StreamAccessor(Stream stream)
Stream = stream;
}

public void Read(Span<byte> buffer)
public void Read(scoped Span<byte> buffer)
{
Stream.ReadExactly(buffer);
}

public void Write(ReadOnlySpan<byte> buffer)
public void Write(scoped ReadOnlySpan<byte> buffer)
{
Stream.Write(buffer);
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/core/Bridge/BridgeDataComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected BridgeDataComponent()
IV = iv;
}

protected abstract void InitializeKey(Span<byte> key);
protected abstract void InitializeKey(scoped Span<byte> key);

protected abstract void InitializeIV(Span<byte> iv);
protected abstract void InitializeIV(scoped Span<byte> iv);
}
13 changes: 8 additions & 5 deletions src/shared/core/Net/GameConnectionConduit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ public bool TryPostPacket(GamePacket packet)
return TryWritePacket(packet, completion: null);
}

public void PostPacket(TeraGamePacketCode code, ReadOnlySpan<byte> payload)
public void PostPacket(TeraGamePacketCode code, scoped ReadOnlySpan<byte> payload)
{
_ = TryPostPacket(code, payload);
}

public void PostPacket(AriseGamePacketCode code, ReadOnlySpan<byte> payload)
public void PostPacket(AriseGamePacketCode code, scoped ReadOnlySpan<byte> payload)
{
_ = TryPostPacket(code, payload);
}

public bool TryPostPacket(TeraGamePacketCode code, ReadOnlySpan<byte> payload)
public bool TryPostPacket(TeraGamePacketCode code, scoped ReadOnlySpan<byte> payload)
{
return TryWritePacket(GameConnectionChannel.Tera, (ushort)code, payload, completion: null);
}

public bool TryPostPacket(AriseGamePacketCode code, ReadOnlySpan<byte> payload)
public bool TryPostPacket(AriseGamePacketCode code, scoped ReadOnlySpan<byte> payload)
{
return TryWritePacket(GameConnectionChannel.Arise, (ushort)code, payload, completion: null);
}
Expand Down Expand Up @@ -136,7 +136,10 @@ public async ValueTask<bool> TrySendPacketAsync(AriseGamePacketCode code, ReadOn
}

private bool TryWritePacket(
GameConnectionChannel channel, ushort code, ReadOnlySpan<byte> payload, TaskCompletionSource<bool>? completion)
GameConnectionChannel channel,
ushort code,
scoped ReadOnlySpan<byte> payload,
TaskCompletionSource<bool>? completion)
{
var buffer = Connection.Manager.Buffers.Get();

Expand Down
4 changes: 2 additions & 2 deletions src/shared/module/PatchableBridgeDataComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Arise.Bridge;
public sealed class PatchableBridgeDataComponent : BridgeDataComponent
{
[Obfuscation]
protected override void InitializeKey(Span<byte> key)
protected override void InitializeKey(scoped Span<byte> key)
{
// Filled in by the server's BridgeModuleGenerator.

Expand All @@ -26,7 +26,7 @@ protected override void InitializeKey(Span<byte> key)
}

[Obfuscation]
protected override void InitializeIV(Span<byte> iv)
protected override void InitializeIV(scoped Span<byte> iv)
{
// Filled in by the server's BridgeModuleGenerator.

Expand Down

0 comments on commit 5d7e27e

Please sign in to comment.