First working version

parent b853df41
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'mongo', '~> 2.4'
GEM
remote: https://rubygems.org/
specs:
bson (4.2.2)
mongo (2.4.3)
bson (>= 4.2.1, < 5.0.0)
PLATFORMS
ruby
DEPENDENCIES
mongo (~> 2.4)
BUNDLED WITH
1.16.0
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'digest'
require 'mongo'
@buffersize = 10
nthreads = 2
mongo_addr = '127.0.0.1:27017'
mongo_db = 'test'
# Connect to mongodb
Mongo::Logger.logger.level = Logger::FATAL
client = Mongo::Client.new([mongo_addr], database: mongo_db)
# Define our collection
@col = client[:hashes]
# Define our read buffer
@buffer = SizedQueue.new @buffersize
# Function to constantly fill our read buffer
def fill_buffer
File.open('passwordlist.txt', 'r').each_line do |line|
@buffer.push line.strip
end
end
# Function to constantly drain our read buffer
def process_buffer(b = [])
loop do
password = @buffer.pop
break if password.nil?
b.push(
password: password,
md5: (Digest::MD5.hexdigest password),
sha1: (Digest::SHA1.hexdigest password),
sha256: (Digest::SHA256.hexdigest password)
)
# Submit if we've filled our buffer
if b.size == @buffersize
submit_results b
b.clear
end
end
# Submit a partially filled buffer if we're at the end
submit_results b unless b.size.zero?
end
# Submit calculated hashes to mongodb
def submit_results(results)
@col.insert_many(results)
end
# Fill password buffer
producer = Thread.new { fill_buffer }
# Start processing passwords
consumers = []
nthreads.times do
consumers.push(Thread.new { process_buffer })
end
# Wait for producer to finish
producer.join
puts 'No more passwords in file'
# Signal workers to finish
nthreads.times do
@buffer.push nil
end
# Wait until all workers are done
consumers.each(&:join)
puts 'All done!'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment