1
0
Fork 0
fatkitten/app/paste.rb

38 lines
622 B
Ruby
Raw Normal View History

2013-07-09 08:04:03 +00:00
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 initialize(contents)
@contents = contents
2013-07-09 07:53:49 +00:00
end
2013-07-09 08:04:03 +00:00
def save
encrypt!
DB[:pastes].insert(contents: contents)
end
2013-07-09 07:53:49 +00:00
2013-07-09 08:04:03 +00:00
def decrypt(key)
@key = key
decrypt!
self
2013-07-09 07:53:49 +00:00
end
2013-07-09 08:04:03 +00:00
def key
@key ||= SecureRandom.hex
end
2013-07-09 07:53:49 +00:00
2013-07-09 08:04:03 +00:00
def encrypt!
@contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key))
2013-07-09 07:53:49 +00:00
end
2013-07-09 08:04:03 +00:00
def decrypt!
@contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key)
end
2013-07-09 07:53:49 +00:00
end