Commit 223d6bb8 authored by Cool Fire's avatar Cool Fire

Add API endpoint to insert new passwords

parent ea410a92
Pipeline #482 passed with stages
in 3 minutes and 23 seconds
# frozen_string_literal: true
require 'digest'
require 'json'
require 'leveldb'
require 'sinatra/base'
......@@ -2,6 +2,32 @@
# Main class for v1 of API
class HashDbApi < Sinatra::Base
put '/v1/insert' do
# Check if password is set
halt 400 if params['password'].nil?
# Calculate hashes
hashes = {
md5: (Digest::MD5.hexdigest params['password']),
sha1: (Digest::SHA1.hexdigest params['password']),
sha256: (Digest::SHA256.hexdigest params['password'])
# Insert hashes into database
hashes.each do |algo, hash|
db = "#{settings.database_prefix}#{algo}.db"
db.put hash, params['password']
# Add password to hashes to construct results
hashes['password'] = params['password']
# Show results
content_type :json
get '/v1/:algo/:hash' do
# Check if valid hahshing algorithm
halt 404 unless %w[md5 sha1 sha256].include? params['algo']
......@@ -22,6 +48,10 @@ class HashDbApi < Sinatra::Base
# Format and return result
content_type :json
{ 'hash': params['hash'], 'found': !result.nil?, 'result': result }.to_json
'hash': params['hash'],
'found': !result.nil?,
'result': result
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