You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SaltMessage.cs 1.0 kB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Shadowsocks.Protocol.Shadowsocks.Crypto;
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. namespace Shadowsocks.Protocol.Shadowsocks
  5. {
  6. public class SaltMessage : IProtocolMessage
  7. {
  8. private readonly int length;
  9. public Memory<byte> Salt { get; private set; }
  10. public SaltMessage(int length, bool roll = false)
  11. {
  12. this.length = length;
  13. if (roll)
  14. {
  15. Salt = new byte[length];
  16. CryptoUtils.RandomSpan(Salt.Span);
  17. }
  18. }
  19. public bool Equals([AllowNull] IProtocolMessage other) => throw new NotImplementedException();
  20. public int Serialize(Memory<byte> buffer)
  21. {
  22. Salt.CopyTo(buffer);
  23. return length;
  24. }
  25. public (bool success, int length) TryLoad(ReadOnlyMemory<byte> buffer)
  26. {
  27. if (buffer.Length < length) return (false, length);
  28. buffer.Slice(0, length).CopyTo(Salt);
  29. return (true, length);
  30. }
  31. }
  32. }