Commit 2d012a49 authored by Cool Fire's avatar Cool Fire

Quick and dirty API to query leveldb for hashes

parents
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'leveldb'
gem 'sinatra'
group :test, :development do
gem 'rack'
gem 'rubocop'
end
# frozen_string_literal: true
require File.expand_path '../hashdb_api.rb', __FILE__
run HashDbApi.new
# frozen_string_literal: true
require 'json'
require 'leveldb'
require 'sinatra/base'
# Main class for api endpoints
class HashDbApi < Sinatra::Base
enable :lock
set :database_prefix, '/databases/'
get '/' do
'Nothing here but us chickens'
end
end
require_relative 'routes/v1'
# frozen_string_literal: true
# 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 unless /^[0-9a-f]{32}$/.match? params['hash']
# 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
# 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 md5 hash
params['hash'].downcase!
halt 400 unless /^[0-9a-f]{40}$/.match? params['hash']
# Attache to md5.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 md5 hash
params['hash'].downcase!
halt 400 unless /^[0-9a-f]{64}$/.match? params['hash']
# Attache to md5.db
db = LevelDB::DB.new "#{settings.database_prefix}sha256.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
end
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