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.1 kB

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