-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrute_force.rb
executable file
·52 lines (47 loc) · 1.25 KB
/
brute_force.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/ruby
text_file = File.open(ARGV[0], 'r')
pattern_file = File.open(ARGV[1], 'r')
debug = false
text_size = text_file.size
pattern_size = pattern_file.size
matches_counter = 0
t_index = 0
buffer = Array.new
occurencies = 0
while t_index <= (text_size - pattern_size) do
t_char = text_file.getc
p_char = pattern_file.getc
puts t_index.to_s + ": comparando " + t_char + " e " + p_char if debug
while t_char == p_char && matches_counter < pattern_size
puts t_index.to_s+": matched! (" + t_char + " and " + p_char + ")" if debug
matches_counter = matches_counter + 1
t_index = t_index + 1
buffer << t_char
t_char = text_file.getc
p_char = pattern_file.getc
end
if buffer.size == pattern_size
text_file.ungetc(t_char)
t_index = t_index - 1
occurencies = occurencies + 1
buffer = Array.new
end
if buffer.length > 0
buffer = buffer.drop(1)
buffer.reverse!
puts buffer.inspect if debug
text_file.ungetc(t_char)
t_index = t_index - 1
buffer.each do |c|
text_file.ungetc(c)
t_index = t_index - 1
end
end
pattern_file.rewind
buffer = Array.new
matches_counter = 0
t_index = t_index + 1
end
puts occurencies.to_s + " occurencie(s)."
pattern_file.close
text_file.close