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
+2 -1
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)
+30 -13
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