예제 #1
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splitCommand = command.Split(" ");
            var argCount     = 2;

            if (splitCommand.Length < argCount)
            {
                throw new CommandHandleException($"{argCount} аргумента");
            }

            var word = _container.Get(splitCommand[1]);

            if (word == null)
            {
                throw new CommandHandleException("не найдено");
            }

            if (splitCommand.Length > 2 && splitCommand[2] == "dec")
            {
                var decodedVal = _coder.DecryptFromString(word.Value, _appSettings.Key);
                _output.WriteLine($"{word.Key} - {decodedVal}");
            }
            else
            {
                _output.WriteLine($"{word.Key} - {word.Value}");
            }

            return(true);
        }
예제 #2
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splt = command.Split(" ");

            if (splt.Length < 2)
            {
                return(false);
            }

            if (_container.HasRecords())
            {
                throw new CommandHandleException("уже есть загруженные записи, сначала очистите - 'clear'");
            }

            if (!_fileAction.Exists(splt[1]))
            {
                throw new CommandHandleException("file not found");
            }

            var data = _fileAction.ReadAllLines(splt[1]);

            if (data.Length < 2)
            {
                throw new CommandHandleException("file is empty, please create new");
            }

            var encLoginSplit = data[0].Split(Consts.FileDataStringSeparate);

            if (encLoginSplit.Length < 2)
            {
                throw new CommandHandleException("file is wrong sign");
            }

            var encLogin = encLoginSplit[1];

            string savedLogin = "";

            try
            {
                savedLogin = _coder.DecryptFromString(encLogin, _appSettings.Key);
            }
            catch
            {
            }

            if (!_appSettings.LoginIsGood(savedLogin))
            {
                throw new CommandHandleException(
                          "bad password for this db, please load any db or change password(clear command)");
            }

            foreach (var line in data[1..])
예제 #3
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splitCommand = command.Split(" ");
            var argCount     = 3;

            if (splitCommand.Length < argCount)
            {
                throw new CommandHandleException($"{argCount} аргумента");
            }

            //var bytes = _coder.CustomStringToBytes(splitCommand[1]);
            //var str = _coder.DecryptFromBytes(bytes, splitCommand[2]);
            var str = _coder.DecryptFromString(splitCommand[1], splitCommand[2]);

            _output.WriteLine(str);

            return(true);
        }
예제 #4
0
        public bool TryDo(string command)
        {
            if (!command.StartsWith(_customName))
            {
                return(false);
            }

            var splitCommand = command.Split(" ");
            var argCount     = 2;

            if (splitCommand.Length < argCount)
            {
                throw new CommandHandleException($"min {argCount} аргумента");
            }

            string loginForSave    = _appSettings.Login;
            string passwordForSave = _appSettings.Key;
            bool   differentCredit = false;

            if (splitCommand.Length > 2)
            {
                if (splitCommand.Length < 4)
                {
                    throw new CommandHandleException($"2 or 4 arg");
                }
                else
                {
                    loginForSave    = splitCommand[2];
                    passwordForSave = splitCommand[3];
                    differentCredit = true;
                }
            }


            string pathForSave = splitCommand[1];//"./"

            if (_fileAction.Exists(pathForSave))
            {
                throw new CommandHandleException($"file is exist");
            }

            List <string> forWrite = new List <string>();

            forWrite.Add("check" + Consts.FileDataStringSeparate + _coder.EncryptWithString(loginForSave, passwordForSave));
            foreach (var item in _container.GetAll())
            {
                string value = item.Value;
                if (!differentCredit)
                {
                    value = item.Value;
                }
                else
                {
                    value = _coder.EncryptWithString(
                        _coder.DecryptFromString(value, _appSettings.Key), passwordForSave);
                }

                forWrite.Add(item.Key + Consts.FileDataStringSeparate + value);
            }

            _fileAction.WriteAllLines(pathForSave, forWrite);

            return(true);
        }