Class: SecurePStore

Inherits:
PStore
  • Object
show all
Defined in:
lib/diary-ruby/ext/secure_pstore.rb

Overview

Wrap PStore, combine with OpenSSL::Cipher to secure store on disk with a given passphrase

SecurePStore

Useable exactly like PStore except for initialization.

With PStore:

wiki = PStore.new("wiki_pages.pstore")
wiki.transaction do  # begin transaction; do all of this or none of it
  # store page...
  wiki[home_page.page_name] = home_page
  # ensure that an index has been created...
  wiki[:wiki_index] ||= Array.new
  # update wiki index...
  wiki[:wiki_index].push(*home_page.wiki_page_references)
end                  # commit changes to wiki data store file

With SecurePStore:

wiki = SecurePStore.new("wiki_pages.pstore", passphrase: 'do it this way instead')
wiki.transaction do  # begin transaction; do all of this or none of it
  # store page...
  wiki[home_page.page_name] = home_page
  # ensure that an index has been created...
  wiki[:wiki_index] ||= Array.new
  # update wiki index...
  wiki[:wiki_index].push(*home_page.wiki_page_references)
end                  # commit changes to wiki data store file

Simple!

Instance Method Summary collapse

Constructor Details

#initialize(file_name, secure_opts = {}) ⇒ SecurePStore

:call-seq:

initialize( file_name, secure_opts = {} )

Creates a new SecureStore object, which will store data in file_name. If the file does not already exist, it will be created.

Options passed in through secure_opts will be used behind the scenes when writing the encrypted file to disk.



48
49
50
51
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 48

def initialize file_name, secure_opts = {}
  @opt = secure_opts
  super
end

Instance Method Details

#dump(table) ⇒ Object

Override PStore’s private low-level storage methods, similar to YAML::Store



55
56
57
58
59
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 55

def dump(table)  # :nodoc:
  marshalled = Marshal::dump(table)
  # return encrypted
  Encryptor.encrypt(marshalled, @opt[:passphrase])
end

#empty_marshal_checksumObject



81
82
83
84
85
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 81

def empty_marshal_checksum
  @empty_marshal_checksum ||= begin
                                Digest::MD5.digest(empty_marshal_data)
                              end
end

#empty_marshal_dataObject



74
75
76
77
78
79
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 74

def empty_marshal_data
  @empty_marshal_data ||= begin
                            m = Marshal.dump({})
                            Encryptor.encrypt(m, @opt[:passphrase])
                          end
end

#load(content) ⇒ Object

:nodoc:



61
62
63
64
65
66
67
68
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 61

def load(content)  # :nodoc:
  begin
    dcontent = Encryptor.decrypt(content, @opt[:passphrase])
    Marshal::load(dcontent)
  rescue OpenSSL::Cipher::CipherError => ex
    raise PStore::Error.new("Failed to decrypt stored data: #{ ex.message }")
  end
end

#marshal_dump_supports_canonical_option?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/diary-ruby/ext/secure_pstore.rb', line 70

def marshal_dump_supports_canonical_option?
  false
end