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'
# Application / Server
gem 'sinatra'
gem 'shotgun'
gem 'unicorn'
# Database
gem 'sequel'
gem 'pg'
# Encryption / Decryption
gem 'encryptor'
# Syntax Highlighting
gem 'pygments.rb'
# Sugar
gem 'haml'
gem 'sass'
gem 'unicorn'
gem 'shotgun'

View File

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

View File

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

View File

@ -1,20 +1,37 @@
module Paste
def get(params)
table.where(params).first
class Paste
attr_reader :contents
class << self
def find(id)
record = DB[:pastes].where(id: id).first
record ? new(record[:contents]) : nil
end
end
def add(params)
id = table.insert(params)
return unless id.is_a?(Integer)
get(id: id)
def initialize(contents)
@contents = contents
end
private
def table
DB[:pastes]
def save
encrypt!
DB[:pastes].insert(contents: contents)
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

View File

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

View File

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