Created
November 8, 2018 08:18
-
-
Save trienow/112b074f0716f26ad2955d9f9645fdf7 to your computer and use it in GitHub Desktop.
A C# Npp Automation to scramble some text into a SecureString
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Windows.Forms; | |
| using NppScripts; | |
| using System.Collections.Generic; | |
| public class Script : NppScript | |
| { | |
| public override void Run() | |
| { | |
| string input = Npp.GetAllText(); | |
| List<CharInfo> stringParts = new List<CharInfo>(input.Length); | |
| Random rnd = new Random(); | |
| //Create Relations | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| stringParts.Add(new CharInfo(input[i], i)); | |
| } | |
| //Scramble | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| CharInfo tuple = stringParts[i]; | |
| stringParts.RemoveAt(i); | |
| stringParts.Insert(rnd.Next(input.Length), tuple); | |
| } | |
| //Log | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| Npp.Output.WriteLine(stringParts[i].ToString()); | |
| } | |
| Npp.Output.WriteLine(""); | |
| //Reconstruct | |
| int lastAppendPosition = stringParts[0].Position; | |
| List<int> sortedPositions = new List<int>(input.Length) { lastAppendPosition }; | |
| for (int i = 1; i < input.Length; i++) | |
| { | |
| CharInfo ci = stringParts[i]; | |
| if (ci.Position < lastAppendPosition) | |
| { | |
| int j = 0; | |
| for (; j < i && ci.Position > sortedPositions[j]; j++) ; | |
| ci.Action = j; | |
| sortedPositions.Insert(ci.Action, ci.Position); | |
| } | |
| else | |
| { | |
| sortedPositions.Add(ci.Position); | |
| lastAppendPosition = ci.Position; | |
| } | |
| } | |
| //Log | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| Npp.Output.WriteLine(stringParts[i].ToString()); | |
| } | |
| Npp.Output.WriteLine(""); | |
| //Test | |
| List<char> test = new List<char>(input.Length); | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| if (stringParts[i].Action < 0) | |
| { | |
| test.Add(stringParts[i].Data); | |
| } | |
| else | |
| { | |
| test.Insert(stringParts[i].Action, stringParts[i].Data); | |
| } | |
| } | |
| string output = new string(test.ToArray()); | |
| Npp.Output.WriteLine("Input: " + input); | |
| Npp.Output.WriteLine("Output: " + output); | |
| Npp.Output.WriteLine("Success: " + (input == output)); | |
| //Create Code | |
| List<string> outputLines = new List<string>(input.Length); | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| if (stringParts[i].Action < 0) | |
| { | |
| outputLines.Add("s.AppendChar('" + stringParts[i].Data + "');"); | |
| } | |
| else | |
| { | |
| outputLines.Add("s.InsertAt(" + stringParts[i].Action +", '" + stringParts[i].Data + "');"); | |
| } | |
| } | |
| Npp.SetAllText(string.Join("\r\n", outputLines)); | |
| } | |
| } | |
| public class CharInfo | |
| { | |
| public char Data { get; set; } | |
| public int Position { get; set; } | |
| public int Action { get; set; } | |
| public CharInfo(char data, int position) | |
| { | |
| Data = data; | |
| Position = position; | |
| Action = -1; | |
| } | |
| public static bool operator <(CharInfo a, CharInfo b) | |
| { | |
| return a.Position < b.Position; | |
| } | |
| public static bool operator >(CharInfo a, CharInfo b) | |
| { | |
| return a.Position > b.Position; | |
| } | |
| public override string ToString() | |
| { | |
| return "{Data: " + Data + ", Position: " + Position + ", Action: " + Action + "}"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment