Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hashdb-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Cool Fire
hashdb-api
Commits
f731dd64
Commit
f731dd64
authored
Dec 20, 2017
by
Cool Fire (HN)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make method less ugly and more flexible
parent
499551e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
40 deletions
+8
-40
routes/v1.rb
routes/v1.rb
+8
-40
No files found.
routes/v1.rb
View file @
f731dd64
...
...
@@ -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?
# Attach
e 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'
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment