Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (
                string.IsNullOrWhiteSpace(this.language.Text) ||
                string.IsNullOrWhiteSpace(this.fileContents.Text))
            {
                MessageBox.Show("Ensure that Language and FileContents are set.");
                return;
            }
            var realFilename = Path.Combine("CryptoPatterns", $"crypto-patterns-{CreateHash(fileContents.Text)}.txt");
            var codeSnippet  = new CodeSnippet(
                1,
                fileName.Text,
                sourceUrl.Text,
                packageName.Text,
                (CodeLanguage)Enum.Parse(typeof(CodeLanguage), language.Text),
                algorithms.SelectedItems.Cast <string>().Select(x => (CryptoAlgorithm)Enum.Parse(typeof(CryptoAlgorithm), x)).ToArray(),
                isFullFile.Checked,
                fileContents.Text.Trim());

            File.WriteAllText(realFilename, codeSnippet.ToString());
            this.fileName.Text     = "";
            this.fileContents.Text = "";
            this.algorithms.SelectedItems.Clear();
            RefreshData();
        }
Exemplo n.º 2
0
        public static CodeSnippet FromString(string serialized)
        {
            var lines = serialized.Split(new char[] { '\n' });

            if (!lines[0].Trim().Equals($"version={KnownVersion}"))
            {
                return(null);
            }
            try
            {
                var obj = new CodeSnippet(
                    KnownVersion,
                    lines[1].Trim(),
                    lines[2].Trim(),
                    lines[3].Trim(),
                    (CodeLanguage)Enum.Parse(typeof(CodeLanguage), lines[4].Trim()),
                    lines[5].Trim().Split(new[] { ',' }).Where(x => !string.IsNullOrEmpty(x)).Select(x => (CryptoAlgorithm)Enum.Parse(typeof(CryptoAlgorithm), x)).ToArray(),
                    lines[6].Trim().Contains("checked=true"),
                    string.Join("\n", lines.Skip(8)));
                return(obj);
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("{0} ({1})", e.Message, e.GetType()));
                return(null);
            }
        }
Exemplo n.º 3
0
        private void fileListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var indices = fileListView.SelectedItems;

            if (indices.Count == 0)
            {
                return;
            }
            string filename = "";

            foreach (ListViewItem index in indices)
            {
                filename = index.Text;
                break;
            }
            if (filename == "")
            {
                MessageBox.Show("Weird.");
            }

            var lines  = File.ReadAllText(filename);
            var result = CodeSnippet.FromString(lines);

            if (result is null)
            {
                MessageBox.Show(string.Format("Error loading {0}: Could not parse this as a CodeSnippet.", filename), "Error", MessageBoxButtons.OK);
                return;
            }

            fileName.Text    = result.name;
            sourceUrl.Text   = result.sourceUrl;
            packageName.Text = result.packageName;
            language.Text    = result.language.ToString();
            algorithms.SelectedItems.Clear();
            foreach (var alg in result.algorithms)
            {
                algorithms.SelectedItems.Add(alg.ToString());
            }
            isFullFile.Checked = result.isFullFile;
            fileContents.Text  = result.content;
        }