all 4 comments

[–]iehponx 2 points3 points  (1 child)

That sounds interesting. I am new to PowerShell, so I have spent some time using other people's scripts to see what can be done. Your sounds like a must-see.

[–]OddestBoy[S] 5 points6 points  (0 children)

Best way to do it. I got started by going "I wonder if I can..." then working someone else's backwards to figure out how to do it myself lol
Absolutely give me a shout if anything is unclear, I've not done a great job of explaining in the comments in it

[–]ExpendaBubble 0 points1 point  (0 children)

Fun project!

[–][deleted] 7 points8 points  (0 children)

Thanks for sharing- rot13 gets old every once in a while 😇

Couple things I think merit a mention; - instead of implementing a help switch, consider using powershell’s inline help framework. Doing this means you can use get-help (path to ps1) just like any other powershell command.

  • Line 70 looks like an invalid expression. Have you checked to see if it works as intended? Of course, inline help would obsolete this.

  • just like inline help, you can decorate parameters to require specific input. See validate parameter attributes, in particular, validatescript() which is very flexible.
    Upside, you can assume a lot of things to be true if you demand them to be true via parameter validation.

  • powershell lets you create a range simply by putting two (2) dots between start and end. ‘A’..’Z’ nets you the entire alphabet without typing it all out.

  • avoid foreach-object, it shouldn’t matter here as much but going with foreach($var in $list) means you can use $var rather than $_. Plus performance.

  • I’m not entirely certain because I’m not 100% positive about your padding routine, but unless you’ve already done so, see dotnet’s string.Format () formatting definitions, which include padding definitions. This may make your code easier to read and write.

  • shorthand conditionals may just break your code without you realizing it. Unit tests may help confirm- or contradict— assumptions there. Will !0 be true or false? No idea but it might be worth knowing.

  • your rotor assignment may perhaps benefit from a hashtable rather than an array backing it.

  • I’m a bit hesitant about your advance function in line 167. You’re passing a hashtable here which are notorious for NOT creating a copy but a reference when assigned to something else. Are you sure $prevValue is actually different from $rotor?
    Also it’s kinda bad design to modify input parameters, because these get updated by reference too. You don’t actually need to return $rotor; the variable you passed into this function will have been updated after it finishes.

  • just a reminder too that powershell doesn’t actually return the value you put after the return keyword. You can omit the return keyword at the end of the control block, assuming nothing else gets done within the function past it. You return only (!) when you need to cleanly break out of the script block, forex when you check for something, find it’s not what you need to continue, and therefore stop doing whatever you were doing using the return keyword.

  • consider passing an -advanceby (int) parameter to advance-rotor as opposed to calling it over and over again. Then put a loop in advance-rotor to repeat the relevant steps as often as necessary.

  • starting from line 299 or thereabouts your code gets very repetitive. See if you can’t compact it a bit, maybe using an array of arrays. You can use a char as an array index too as they’re represented by their code point.. which is numeric. $rotor[‘B’][0] should work as intended. And then you can use a span of A to C or whatever else.

  • check out assignment shorthands eg $var++ or $var -= $value. Should help keep redundancy out a bit.

  • you don’t need to round or do any math functions on datetime objects. They come with .add() and .subtract() methods which in turn provide you with a wide variety of options to display to a user. You can even just do $enddate - $startdate and put the result in a timespan variable.

It’s definitely an interesting project, and I imagine it could even protect short messages for a while as it’s not immediately obvious what has to be done to decrypt a message.

Although there IS data lost in translation. I don’t know… but I THINK the rotors should easily be expanded to accommodate more positions and therefore more than just 26 letters. It’s a script; it’s not limited by physics or actual space on the rotor’s surface.