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.

SodiumEncryptStream.cs 2.6 kB

7 years ago
7 years ago
7 years ago
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace Discord.Audio.Streams
  5. {
  6. /// <summary>
  7. /// Encrypts an RTP frame using libsodium.
  8. /// </summary>
  9. public class SodiumEncryptStream : AudioOutStream
  10. {
  11. private readonly AudioClient _client;
  12. private readonly AudioStream _next;
  13. private readonly byte[] _nonce;
  14. private bool _hasHeader;
  15. private ushort _nextSeq;
  16. private uint _nextTimestamp;
  17. public SodiumEncryptStream(AudioStream next, IAudioClient client)
  18. {
  19. _next = next;
  20. _client = (AudioClient)client;
  21. _nonce = new byte[24];
  22. }
  23. /// <exception cref="InvalidOperationException">Header received with no payload.</exception>
  24. public override void WriteHeader(ushort seq, uint timestamp, bool missed)
  25. {
  26. if (_hasHeader)
  27. throw new InvalidOperationException("Header received with no payload.");
  28. _nextSeq = seq;
  29. _nextTimestamp = timestamp;
  30. _hasHeader = true;
  31. }
  32. /// <exception cref="InvalidOperationException">Received payload without an RTP header.</exception>
  33. /// <exception cref="OperationCanceledException">The token has had cancellation requested.</exception>
  34. /// <exception cref="ObjectDisposedException">The associated <see cref="T:System.Threading.CancellationTokenSource" /> has been disposed.</exception>
  35. public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
  36. {
  37. cancelToken.ThrowIfCancellationRequested();
  38. if (!_hasHeader)
  39. throw new InvalidOperationException("Received payload without an RTP header.");
  40. _hasHeader = false;
  41. if (_client.SecretKey == null)
  42. return;
  43. Buffer.BlockCopy(buffer, offset, _nonce, 0, 12); //Copy nonce from RTP header
  44. count = SecretBox.Encrypt(buffer, offset + 12, count - 12, buffer, 12, _nonce, _client.SecretKey);
  45. _next.WriteHeader(_nextSeq, _nextTimestamp, false);
  46. await _next.WriteAsync(buffer, 0, count + 12, cancelToken).ConfigureAwait(false);
  47. }
  48. public override async Task FlushAsync(CancellationToken cancelToken)
  49. {
  50. await _next.FlushAsync(cancelToken).ConfigureAwait(false);
  51. }
  52. public override async Task ClearAsync(CancellationToken cancelToken)
  53. {
  54. await _next.ClearAsync(cancelToken).ConfigureAwait(false);
  55. }
  56. }
  57. }