Advertisement
isendrak

BruteforceGenerator+WordlistProcessor.cs

Oct 25th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. public class BruteforceGenerator{
  6.     public char[] CharSet{ get; private set; }
  7.     public int Length{ get; private set; }
  8.     public short[] State{ get; private set; }
  9.     public string Current{ get; private set; }
  10.    
  11.     public BruteforceGenerator(char[] CharSet, int Length, short[] State){
  12.         this.CharSet = CharSet;
  13.         this.Length = Length;
  14.         this.State = State;
  15.         this.Current = "";
  16.     }
  17.    
  18.     public BruteforceGenerator(char[] CharSet, int Length){
  19.         this.CharSet = CharSet;
  20.         this.Length = Length;
  21.         this.State = new short[Length];
  22.         this.State[Length - 1] = -1;
  23.         this.Current = "";
  24.     }
  25.    
  26.     public string Next(){
  27.         if(Current == null) return null;
  28.         this.State[Length - 1]++;
  29.         for(int i = Length - 1; i >= 0; i--){
  30.             if(State[i] == CharSet.Length){
  31.                 State[i] = 0;
  32.                 if(i > 0) State[i - 1]++;
  33.                 else{
  34.                     Current = null;
  35.                     return null;
  36.                 }
  37.             }
  38.         }
  39.         Current = "";
  40.         for(int i = 0; i < Length; i++)
  41.             Current += CharSet[State[i]];
  42.         return Current;
  43.     }
  44. }
  45.  
  46. public class WordlistProcessor{
  47.     public string Filename{ get; private set; }
  48.     private TextReader WordlistReader;
  49.     public string Current{ get; private set; }
  50.     private Stack<string> Mutations;
  51.    
  52.     public WordlistProcessor(string Filename){
  53.         this.Current = "";
  54.         this.Mutations = new Stack<string>();
  55.         this.Filename = Filename;
  56.         this.WordlistReader = new StreamReader(new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read));
  57.     }
  58.    
  59.     private void ReadNext(){
  60.         String Line = WordlistReader.ReadLine();
  61.         Mutations.Clear();
  62.         if(Line == null) return;
  63.         string Mutation;
  64.         for(int i = 0; i < Math.Pow(2, Line.Length); i++){
  65.             Mutation = "";
  66.             for(int j = 0; j < Line.Length; j++){
  67.                 char c = Line[j];              
  68.                 if((i & (1 << j)) != 0) Mutation += (c >= 'a' && c <= 'z') ? (char)(c ^ 32) : c;
  69.                 else Mutation += (c >= 'A' && c <= 'Z') ? (char)(c ^ 32) : c;
  70.             }
  71.             Mutations.Push(Mutation);
  72.         }
  73.     }
  74.    
  75.     public string Next(){
  76.         if(Current == null) return null;
  77.         if(Mutations.Count > 0){
  78.             Current = Mutations.Pop();
  79.         }
  80.         else{
  81.             ReadNext();
  82.             if(Mutations.Count <= 0) Current = null;
  83.             else Current = Mutations.Pop();
  84.         }
  85.         return Current;
  86.     }
  87.    
  88.     public void Close(){
  89.         WordlistReader.Close();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement