Class: Bequest::Data
- Inherits:
-
Object
- Object
- Bequest::Data
- Defined in:
- lib/bequest/data.rb
Instance Method Summary collapse
- #dump(path) ⇒ Object
-
#initialize(secret_data, opts = {}) ⇒ Data
constructor
A new instance of Data.
- #unpack(password, mac_addr) ⇒ Object
Constructor Details
#initialize(secret_data, opts = {}) ⇒ Data
Returns a new instance of Data.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bequest/data.rb', line 9 def initialize(secret_data, opts = {}) opts = {:expires_at => nil, :password => '', :mac_addr => ''}.merge(opts) compressed = Zlib::Deflate.deflate(secret_data) key = key(opts[:password], opts[:mac_addr]) iv = Digest::MD5.hexdigest(rand.to_s) encrypted_data = encrypt(compressed, key, iv) encrypted_expires_at = opts[:expires_at] ? encrypt(opts[:expires_at].to_i.to_s, key, iv) : nil body = [encrypted_expires_at, opts[:password].any?, opts[:mac_addr].any?, encrypted_data, iv] checksum = Digest::MD5.hexdigest(body.join) @data = [checksum, body] end |
Instance Method Details
#dump(path) ⇒ Object
22 23 24 25 |
# File 'lib/bequest/data.rb', line 22 def dump(path) FileUtils.mkdir_p(File.dirname(path)) File.open(path, "w") { |f| f.write(Marshal::dump(self)) } end |
#unpack(password, mac_addr) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bequest/data.rb', line 27 def unpack(password, mac_addr) password = prompt_if_required(password) mac_addr = get_if_required(mac_addr) if Digest::MD5.hexdigest(body.join) == checksum compressed = decrypt(encrypted_data, key(password, mac_addr), iv) begin data = Zlib::Inflate.inflate(compressed) if encrypted_expires_at expires_at = Time.at(decrypt(encrypted_expires_at, key(password, mac_addr), iv).to_i) if expires_at.to_i < Time.now.to_i [nil, :expired, expires_at] else [data, :ok, expires_at] end else [data, :ok, nil] end rescue [nil, :unauthorized, nil] end else [nil, :tampered, nil] end end |