예제 #1
0
        public static SecureString GetPassword(string username)
        {
            lock (_lock)
            {
                if (cache != null)
                {
                    SecureString s;
                    if (cache.TryGetValue(username, out s))
                    {
                        return(s);
                    }
                }
                else
                {
                    cache = new Dictionary <string, SecureString>(StringComparer.OrdinalIgnoreCase);
                }

                if (state != StorageState.None)
                {
                    try
                    {
                        var i = GetStorage().Read <DataItem>(username);

                        using (var crypto = new Cryptography.Crypto())
                        {
                            SecureString s;

                            if (i.Data.Length > 0)
                            {
                                var data = crypto.Decrypt(i.Data);

                                try
                                {
                                    s = FromByteArray(data);
                                }
                                finally
                                {
                                    Array.Clear(data, 0, data.Length);
                                }
                            }
                            else
                            {
                                s = new SecureString();
                                s.MakeReadOnly();
                            }

                            cache.Add(username, s);

                            return(s);
                        }
                    }
                    catch (Exception e)
                    {
                        Util.Logging.Log(e);
                    }
                }
            }

            return(null);
        }
예제 #2
0
        private static void OnCryptoChanged()
        {
            var store = GetStorage();
            var items = new List <DataItem>();
            var o     = Settings.Encryption.Value;
            var scope = o != null ? o.Scope : Settings.EncryptionScope.CurrentUser;

            using (var crypto = new Cryptography.Crypto())
            {
                try
                {
                    foreach (var item in store.ReadAll <DataItem>())
                    {
                        try
                        {
                            if (crypto.GetScope(item.Data) == scope)
                            {
                                continue;
                            }

                            var data = crypto.Decrypt(item.Data);

                            try
                            {
                                items.Add(new DataItem()
                                {
                                    ID   = item.ID,
                                    Data = crypto.Compress(crypto.Encrypt(scope, data), Cryptography.Crypto.CryptoCompressionFlags.All),
                                });
                            }
                            finally
                            {
                                Array.Clear(data, 0, data.Length);
                            }
                        }
                        catch (Exception e)
                        {
                            Util.Logging.Log(e);
                        }
                    }
                }
                catch { }
            }

            if (items.Count > 0)
            {
                store.Write <DataItem>(items);
            }
        }