hrules 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hrules.rb +229 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1631b7b32ea2ebaf29bd6b5fe58c3aef7191d4022cdac8d9c51f08c8ce79c638
4
+ data.tar.gz: 7984c4f9d831c9504ae43188739f1b08c502fdacd079015938c955bc28053b81
5
+ SHA512:
6
+ metadata.gz: 8ee81f8a9d9b3c823bfb55aea43d8305735157911a48e314c2c7345ec34820f379c7271b449b1d021b59c8c92c17a416c3c1759d2a3a5a60176b33c0083a1e67
7
+ data.tar.gz: bb7ce51c39e41748c5cede438e7f31b9f1296feceea946cfe194cf68fcab021d17dbad4744e59a6356d775d275a91e4004d0898f446c2fe7d2637c6b18ba5a99
@@ -0,0 +1,229 @@
1
+ #!/usr/bin/env ruby
2
+ require 'active_support/inflector'
3
+
4
+ rules = [
5
+ {character: "u", mutation: "upcase", takes_param: false, param_count: 0},
6
+ {character: "l", mutation: "downcase", takes_param: false, param_count: 0},
7
+ {character: "c", mutation: "capitalize", takes_param: false, param_count: 0},
8
+ {character: "C", mutation: "inverted_capitalize", takes_param: false, param_count: 0},
9
+ {character: "t", mutation: "swapcase", takes_param: false, param_count: 0},
10
+ {character: "T", mutation: "toggle_at_position", takes_param: true, param_count: 1},
11
+ {character: "r", mutation: "reverse", takes_param: false, param_count: 0},
12
+ {character: "d", mutation: "duplicate", takes_param: false, param_count: 0},
13
+ {character: "p", mutation: "duplicate_n_times", takes_param: true, param_count: 1},
14
+ {character: "f", mutation: "reflect", takes_param: false, param_count: 0},
15
+ {character: "{", mutation: "rotate_left", takes_param: false, param_count: 0},
16
+ {character: "}", mutation: "rotate_right", takes_param: false, param_count: 0},
17
+ {character: "$", mutation: "append_character", takes_param: true, param_count: 1},
18
+ {character: "^", mutation: "prepend_character", takes_param: true, param_count: 1},
19
+ {character: "[", mutation: "delete_first", takes_param: false, param_count: 0},
20
+ {character: "]", mutation: "delete_last", takes_param: false, param_count: 0},
21
+ {character: "D", mutation: "delete_at_position", takes_param: true, param_count: 1},
22
+ {character: "x", mutation: "extract_from_position", takes_param: true, param_count: 2},
23
+ {character: "O", mutation: "omit_from_position", takes_param: true, param_count: 2},
24
+ {character: "i", mutation: "insert_at_x", takes_param: true, param_count: 2},
25
+ {character: "o", mutation: "overwrite_at_x", takes_param: true, param_count: 2},
26
+ {character: "'", mutation: "truncate_from_x", takes_param: true, param_count: 1},
27
+ {character: "s", mutation: "replace_with_char", takes_param: true, param_count: 2},
28
+ {character: "@", mutation: "purge_instances_of_x", takes_param: true, param_count: 1},
29
+ {character: "z", mutation: "dup_first_n_times", takes_param: true, param_count: 1},
30
+ {character: "Z", mutation: "dup_last_n_times", takes_param: true, param_count: 1},
31
+ {character: "q", mutation: "duplicate_all", takes_param: false, param_count: 0},
32
+ {character: "*", mutation: "swap", takes_param: true, param_count: 2},
33
+ {character: "E", mutation: "titlecase", takes_param: false, param_count: 0},
34
+ {character: "y", mutation: "dup_block_forward", takes_param: true, param_count: 1},
35
+ {character: "Y", mutation: "dup_block_back", takes_param: true, param_count: 1},
36
+ ]
37
+
38
+ class String
39
+
40
+ def duplicate
41
+ return "#{self}#{self}"
42
+ end
43
+
44
+ def inverted_capitalize
45
+ self.capitalize.swapcase
46
+ end
47
+
48
+ def toggle_at_position(position)
49
+ if self[position.to_i] then self[position.to_i] = self[position.to_i]&.upcase end
50
+ return self
51
+ end
52
+
53
+ def duplicate_n_times(times)
54
+ return self * times.to_i
55
+ end
56
+
57
+ def reflect
58
+ "#{self}#{self.reverse}"
59
+ end
60
+
61
+ def rotate_left
62
+ self.split("").rotate(1).join("")
63
+ end
64
+
65
+ def rotate_right
66
+ self.split("").rotate(-1).join("")
67
+ end
68
+
69
+ def append_character(character)
70
+ "#{self}#{character}"
71
+ end
72
+
73
+ def prepend_character(character)
74
+ "#{character}#{self}"
75
+ end
76
+
77
+ def delete_first
78
+ self[1..-1]
79
+ end
80
+
81
+ def delete_last
82
+ self[0..-2]
83
+ end
84
+
85
+ def delete_at_position(position)
86
+ self.split("").reject.with_index { |v, i| i == position.to_i }.join("")
87
+ end
88
+
89
+ def extract_from_position(positions)
90
+ start_p, end_p = positions[0].to_i, positions[1].to_i
91
+ self[start_p..end_p]
92
+ end
93
+
94
+ def omit_from_position(positions)
95
+ start_p, end_p = positions[0].to_i, positions[1].to_i
96
+ range = (start_p..end_p).to_a
97
+ self.split("").reject.with_index { |v, i| range.include?(i) }.join("")
98
+ end
99
+
100
+ def insert_at_x(positions)
101
+ loc, char = positions[0].to_i, positions[1]
102
+ if loc.between?(0, (self.length - 1))
103
+ self.insert(loc, char)
104
+ end
105
+ self
106
+ end
107
+
108
+ def overwrite_at_x(positions)
109
+ loc, char = positions[0].to_i, positions[1]
110
+ self[loc] = char
111
+ self
112
+ rescue IndexError
113
+ self
114
+ end
115
+
116
+ def truncate_from_x(position)
117
+ self[0..position.to_i]
118
+ rescue IndexError
119
+ self
120
+ end
121
+
122
+ def replace_with_char(characters)
123
+ replace, replace_with = characters.split("")[0], characters.split("")[1]
124
+ self.gsub(replace, replace_with)
125
+ rescue IndexError
126
+ self
127
+ end
128
+
129
+ def purge_instances_of_x(character)
130
+ self.gsub(character, "")
131
+ rescue IndexError
132
+ self
133
+ end
134
+
135
+ def dup_first_n_times(times)
136
+ self[0] = self[0] * times.to_i
137
+ return self
138
+ rescue IndexError
139
+ self
140
+ end
141
+
142
+ def dup_last_n_times(times)
143
+ self[-1] = (self[-1] + (self[-1] * times.to_i))
144
+ return self
145
+ rescue IndexError
146
+ self
147
+ end
148
+
149
+ def duplicate_all
150
+ returned = ""
151
+ self.split("").each.with_index do |v, i|
152
+ returned << self[i] = v * 2
153
+ end
154
+ returned
155
+ rescue IndexError
156
+ self
157
+ end
158
+
159
+ def mutate(rule_string)
160
+ to_return = self
161
+ skip = 0
162
+ rule_string.each_char.with_index do |character, index|
163
+ if index < skip
164
+ next
165
+ end
166
+ rule = Rules.find_rule(character)
167
+ next unless rule
168
+ if rule.takes_parameters?
169
+ thing = rule_string[ ((index +1 )..(index + rule.param_count)) ]
170
+ to_return = to_return.send(rule.mutation, thing )
171
+ skip = index + rule.param_count + 1
172
+ else
173
+ to_return = to_return.send(rule.mutation)
174
+ end
175
+ end
176
+ return to_return
177
+ end
178
+
179
+ def swap(characters)
180
+ pos1, pos2 = characters.split("")[0].to_i, characters.split("")[1].to_i
181
+ if pos1.between?(0, (self.length - 1)) and pos2.between?(0, (self.length - 1))
182
+ self[pos1] = self[pos2]
183
+ end
184
+ self
185
+ rescue IndexError
186
+ self
187
+ end
188
+
189
+ def dup_block_back(position)
190
+ block = self[position.to_i..-1]
191
+ self.insert(-1, block) if block
192
+ self
193
+ end
194
+
195
+ def dup_block_forward(position)
196
+ block = self[0..position.to_i]
197
+ self.insert(0, block) if block
198
+ self
199
+ end
200
+ end
201
+
202
+
203
+ class Rules
204
+ @@rules = []
205
+ attr_accessor :character, :mutation, :takes_param, :param_count
206
+
207
+ def initialize(character, mutation, takes_param, param_count)
208
+ @character = character
209
+ @mutation = mutation
210
+ @takes_param = takes_param
211
+ @param_count = param_count
212
+ @@rules << self
213
+ end
214
+
215
+ def self.find_rule(character)
216
+ @@rules.select { |r| r.character == character }.first
217
+ end
218
+
219
+ def takes_parameters?
220
+ self.takes_param
221
+ end
222
+
223
+ def self.all_rules
224
+ @@rules
225
+ end
226
+ end
227
+
228
+
229
+ rules.each { |r| Rules.new(r[:character], r[:mutation], r[:takes_param], r[:param_count]) }
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hrules
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Damerell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple addition to the String class in Ruby to mutate based on Hashcat-style
14
+ rules
15
+ email: aidan.damerell@icloud.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hrules.rb
21
+ homepage: https://rubygems.org/gems/hrules
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hashcat rules in Ruby
45
+ test_files: []