1
0
Fork 0

Encryption

This commit is contained in:
Gregory Eremin 2013-07-09 15:04:03 +07:00
parent ab5072c1c5
commit d7ff408863
6 changed files with 60 additions and 23 deletions

14
Gemfile
View File

@ -1,12 +1,22 @@
source 'https://rubygems.org' source 'https://rubygems.org'
# Application / Server
gem 'sinatra' gem 'sinatra'
gem 'shotgun'
gem 'unicorn'
# Database
gem 'sequel' gem 'sequel'
gem 'pg' gem 'pg'
# Encryption / Decryption
gem 'encryptor'
# Syntax Highlighting
gem 'pygments.rb' gem 'pygments.rb'
# Sugar
gem 'haml' gem 'haml'
gem 'sass' gem 'sass'
gem 'unicorn'
gem 'shotgun'

View File

@ -1,6 +1,7 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
encryptor (1.1.3)
haml (4.0.3) haml (4.0.3)
tilt tilt
kgio (2.8.0) kgio (2.8.0)
@ -32,6 +33,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
encryptor
haml haml
pg pg
pygments.rb pygments.rb

View File

@ -1,8 +1,9 @@
DB = Sequel.connect(CONFIG[:database_url]) DB = Sequel.connect(CONFIG[:database_url])
# DB.drop_table(:pastes) if DB.table_exists?(:pastes)
DB.create_table(:pastes) do DB.create_table(:pastes) do
primary_key :id primary_key :id
String :handle
String :type String :type
Text :contents Text :contents
end unless DB.table_exists?(:pastes) end unless DB.table_exists?(:pastes)

View File

@ -1,20 +1,37 @@
module Paste class Paste
def get(params) attr_reader :contents
table.where(params).first
class << self
def find(id)
record = DB[:pastes].where(id: id).first
record ? new(record[:contents]) : nil
end
end end
def add(params) def initialize(contents)
id = table.insert(params) @contents = contents
return unless id.is_a?(Integer)
get(id: id)
end end
private def save
encrypt!
def table DB[:pastes].insert(contents: contents)
DB[:pastes]
end end
extend self def decrypt(key)
@key = key
decrypt!
self
end
def key
@key ||= SecureRandom.hex
end
def encrypt!
@contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key))
end
def decrypt!
@contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key)
end
end end

View File

@ -1,8 +1,10 @@
require 'securerandom' require 'securerandom'
require 'base64'
require 'bundler/setup' require 'bundler/setup'
require 'sinatra' require 'sinatra'
require 'sequel' require 'sequel'
require 'encryptor'
require 'haml' require 'haml'
require 'app/config' require 'app/config'
@ -17,12 +19,17 @@ class Pastemaster < Sinatra::Application
end end
post '/' do post '/' do
record = Paste.add(handle: SecureRandom.hex, contents: params[:contents]) paste = Paste.new(params[:contents])
redirect record ? "/#{id}/#{record[:handle]}" : '' id = paste.save
redirect "/#{id}/#{paste.key}"
end end
get '/:id/:handle' do get '/:id/:key' do
@record = Paste.get(id: params[:id], handle: params[:handle]) @paste = Paste.find(params[:id])
redirect '/' unless @paste
@paste.decrypt(params[:key])
haml :show, layout: :default haml :show, layout: :default
end end
end end

View File

@ -1,5 +1,5 @@
%p %p
- if @record - if @paste
= @record[:contents] = @paste.contents
- else - else
Sorry no have mister .nohave Sorry no have mister