|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Shadowsocks.Encryption.Stream |
| 5 | +{ |
| 6 | + class PlainEncryptor |
| 7 | + : EncryptorBase, IDisposable |
| 8 | + { |
| 9 | + const int CIPHER_NONE = 1; |
| 10 | + |
| 11 | + private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> { |
| 12 | + { "plain", new EncryptorInfo("PLAIN", 0, 0, CIPHER_NONE) }, |
| 13 | + { "none", new EncryptorInfo("PLAIN", 0, 0, CIPHER_NONE) } |
| 14 | + }; |
| 15 | + |
| 16 | + public PlainEncryptor(string method, string password) : base(method, password) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + public static List<string> SupportedCiphers() |
| 21 | + { |
| 22 | + return new List<string>(_ciphers.Keys); |
| 23 | + } |
| 24 | + |
| 25 | + protected Dictionary<string, EncryptorInfo> getCiphers() |
| 26 | + { |
| 27 | + return _ciphers; |
| 28 | + } |
| 29 | + |
| 30 | + #region TCP |
| 31 | + |
| 32 | + public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) |
| 33 | + { |
| 34 | + Buffer.BlockCopy(buf, 0, outbuf, 0, length); |
| 35 | + outlength = length; |
| 36 | + } |
| 37 | + |
| 38 | + public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) |
| 39 | + { |
| 40 | + Buffer.BlockCopy(buf, 0, outbuf, 0, length); |
| 41 | + outlength = length; |
| 42 | + } |
| 43 | + |
| 44 | + #endregion |
| 45 | + |
| 46 | + #region UDP |
| 47 | + |
| 48 | + public override void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength) |
| 49 | + { |
| 50 | + Buffer.BlockCopy(buf, 0, outbuf, 0, length); |
| 51 | + outlength = length; |
| 52 | + } |
| 53 | + |
| 54 | + public override void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength) |
| 55 | + { |
| 56 | + Buffer.BlockCopy(buf, 0, outbuf, 0, length); |
| 57 | + outlength = length; |
| 58 | + } |
| 59 | + |
| 60 | + #endregion |
| 61 | + |
| 62 | + |
| 63 | + #region IDisposable |
| 64 | + |
| 65 | + private bool _disposed; |
| 66 | + |
| 67 | + // instance based lock |
| 68 | + private readonly object _lock = new object(); |
| 69 | + |
| 70 | + public override void Dispose() |
| 71 | + { |
| 72 | + Dispose(true); |
| 73 | + GC.SuppressFinalize(this); |
| 74 | + } |
| 75 | + |
| 76 | + ~PlainEncryptor() |
| 77 | + { |
| 78 | + Dispose(false); |
| 79 | + } |
| 80 | + |
| 81 | + protected virtual void Dispose(bool disposing) |
| 82 | + { |
| 83 | + lock (_lock) |
| 84 | + { |
| 85 | + if (_disposed) return; |
| 86 | + _disposed = true; |
| 87 | + } |
| 88 | + |
| 89 | + if (disposing) |
| 90 | + { |
| 91 | + // free managed objects |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #endregion |
| 96 | + |
| 97 | + } |
| 98 | +} |
0 commit comments