Browse Source

use ConcurrentDictionary instead of Dictionary

tags/3.0
Gang Zhuo 8 years ago
parent
commit
ea43e2a72d
1 changed files with 9 additions and 16 deletions
  1. +9
    -16
      shadowsocks-csharp/Encryption/IVEncryptor.cs

+ 9
- 16
shadowsocks-csharp/Encryption/IVEncryptor.cs View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Text;
using System.Net;
@@ -24,7 +25,7 @@ namespace Shadowsocks.Encryption
protected Dictionary<string, int[]> ciphers;
private static readonly Dictionary<string, byte[]> CachedKeys = new Dictionary<string, byte[]>();
private static readonly ConcurrentDictionary<string, byte[]> CachedKeys = new ConcurrentDictionary<string, byte[]>();
protected byte[] _encryptIV;
protected byte[] _decryptIV;
protected bool _decryptIVReceived;
@@ -62,22 +63,14 @@ namespace Shadowsocks.Encryption
}
keyLen = ciphers[_method][0];
ivLen = ciphers[_method][1];
if (!CachedKeys.ContainsKey(k))
_key = CachedKeys.GetOrAdd(k, (nk) =>
{
lock (CachedKeys)
{
if (!CachedKeys.ContainsKey(k))
{
byte[] passbuf = Encoding.UTF8.GetBytes(password);
_key = new byte[32];
byte[] iv = new byte[16];
bytesToKey(passbuf, _key);
CachedKeys[k] = _key;
}
}
}
if (_key == null)
_key = CachedKeys[k];
byte[] passbuf = Encoding.UTF8.GetBytes(password);
byte[] key = new byte[32];
byte[] iv = new byte[16];
bytesToKey(passbuf, key);
return key;
});
}
protected void bytesToKey(byte[] password, byte[] key)


Loading…
Cancel
Save