Exemplo n.º 1
0
        public static string GetCredentialCripted(Credential credential)
        {
            if (credential != null)
            {
                credential.ShowPassword = true;
                string salt = Path.GetRandomFileName().Replace(".", "");

                string        credentialSerialized = JsonConvert.SerializeObject(credential);
                string        credentialCrypted    = credentialSerialized.Encrypt(salt);
                StreamToShare streamToShare        = new StreamToShare()
                {
                    Stream = credentialCrypted,
                    Salt   = salt
                };
                credential.ShowPassword = false;
                string streamJson = JsonConvert.SerializeObject(streamToShare);
                return(streamJson.ToBase64());
            }
            throw new Exception("Impossibile creare lo stream di condivisione.");
        }
Exemplo n.º 2
0
        public static void Import(Group groupOwner)
        {
            CheckCredentialsLoaded();

            string stream = Helpers.AskStream();

            if (string.IsNullOrEmpty(stream))
            {
                return;
            }

            if (string.IsNullOrEmpty(stream))
            {
                throw new InvalidOperationException("Stream non valido.");
            }

            StreamToShare sharedStream = JsonConvert.DeserializeObject <StreamToShare>(stream.FromBase64());

            if (sharedStream == null)
            {
                throw new InvalidOperationException("Stream non valido.");
            }

            string streamCredential = sharedStream.Stream;
            string salt             = sharedStream.Salt;

            Credential credentialImported = JsonConvert.DeserializeObject <Credential>(streamCredential.Decrypt(salt));

            if (credentialImported == null)
            {
                throw new Exception("Impossibile importare le credenziali");
            }

            int proxID = 1;

            if (credentials.Count > 0)
            {
                proxID = credentials.Max(g => g.ID) + 1;
            }

            if (string.IsNullOrEmpty(credentialImported.Title))
            {
                throw new Exception("Il titolo non può essere vuoto.");
            }

            if (string.IsNullOrEmpty(credentialImported.Email))
            {
                throw new Exception("L' e-mail non può essere vuota.");
            }

            if (string.IsNullOrEmpty(credentialImported.Password))
            {
                throw new Exception("La password non può essere vuota.");
            }

            int existingSameCredentials = credentials.Count(c => c.Title.ToLower() == credentialImported.Title.ToLower() && c.GroupOwner == groupOwner);

            if (existingSameCredentials > 0)
            {
                credentialImported.Title = credentialImported.Title + $"({existingSameCredentials + 1})";
            }

            credentialImported.ID         = proxID;
            credentialImported.GroupOwner = groupOwner;

            credentials.Add(credentialImported);
            Save();
        }