Encryption
This commit is contained in:
+2
-1
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user