Neptune443

Get-PossibleWordCombinations

Feb 12th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-PossibleWordCombinations {
  2.     [cmdletbinding()]
  3.     param(
  4.         [parameter(Mandatory=$true)]
  5.         [String]$Word,
  6.         [parameter(Mandatory=$true)]
  7.         [hashtable]$Map,
  8.         [switch]$NoCaseConsideration
  9.     )
  10.     $list = New-Object System.Collections.ArrayList
  11.     $queue = New-Object System.Collections.Queue
  12.     [void]$list.Add($word)
  13.     $queue.Enqueue($word)
  14.     while($queue.Count -gt 0){
  15.         $o = $queue.Dequeue()
  16.         for($i = 0 ; $i -lt $o.Length;$i++){
  17.             foreach($key in $map.Keys) {
  18.                 if($o[$i] -ieq $key) {
  19.                     foreach($val in $map[$key]) {
  20.                         $newO = $o.Remove($i,1).Insert($i,$val)
  21.                         if( -not $list.Contains($newO)){
  22.                             [void]$list.Add($newO)
  23.                         }
  24.                         $queue.Enqueue($newO)
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     if($NoCaseConsideration) {
  31.         $list
  32.     } else {
  33.         .{
  34.             foreach($combo in $list) {
  35.                 for($i=0;$i -lt [math]::Pow(2,$combo.length);$i++){
  36.                     $res=@()
  37.                     for($j=0;$j -lt $combo.length;$j++) {
  38.                         if([bool]($i -band [math]::Pow(2,$j))){
  39.                             $res+=([string]$combo[$j]).ToUpper()
  40.                         } else {
  41.                             $res+=([string]$combo[$j]).ToLower()
  42.                         }
  43.                     }
  44.                     -join $res
  45.                 }
  46.             }
  47.         } | Sort-Object -Unique -CaseSensitive
  48.     }
  49. }
  50.  
  51. # Example:
  52. $passwords = Get-PossibleWordCombinations -Word "password" -Map @{a="@","4";o="0"}
  53. $passwords.Count
Advertisement
Add Comment
Please, Sign In to add comment