Commit f731dd64 authored by Cool Fire (HN)'s avatar Cool Fire (HN)

Make method less ugly and more flexible

parent 499551e3
......@@ -2,49 +2,17 @@
# Main class for v1 of API
class HashDbApi < Sinatra::Base
get '/v1/md5/:hash' do
# Check if it is a valid md5 hash
params['hash'].downcase!
halt 400 if /^[0-9a-f]{32}$/.match(params['hash']).nil?
# Attache to md5.db
db = LevelDB::DB.new "#{settings.database_prefix}md5.db"
# Perform lookup
result = db.get params['hash']
# Release database lock
db.close
get '/v1/:algo/:hash' do
# Check if valid hahshing algorithm
halt 404 unless %w[md5 sha1 sha256].include? params['algo']
# Format and return result
content_type :json
{ 'hash': params['hash'], 'found': !result.nil?, 'result': result }.to_json
end
get '/v1/sha1/:hash' do
# Check if it is a valid sha1 hash
params['hash'].downcase!
halt 400 if /^[0-9a-f]{40}$/.match(params['hash']).nil?
# Attache to sha1.db
db = LevelDB::DB.new "#{settings.database_prefix}sha1.db"
# Perform lookup
result = db.get params['hash']
# Release database lock
db.close
# Format and return result
content_type :json
{ 'hash': params['hash'], 'found': !result.nil?, 'result': result }.to_json
end
get '/v1/sha256/:hash' do
# Check if it is a valid sha256 hash
# Check if it is a valid md5 hash
params['hash'].downcase!
halt 400 if /^[0-9a-f]{64}$/.match(params['hash']).nil?
size = { 'md5' => 32, 'sha1' => 40, 'sha256' => 64 }[params['algo']]
halt 400 if /^[0-9a-f]{#{size}}$/.match(params['hash']).nil?
# Attache to sha256.db
db = LevelDB::DB.new "#{settings.database_prefix}sha256.db"
# Attach to database
db = LevelDB::DB.new "#{settings.database_prefix}#{params['algo']}.db"
# Perform lookup
result = db.get params['hash']
......
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