Class: Cryptosphere::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptosphere/blobs/blob.rb

Direct Known Subclasses

Tree

Defined Under Namespace

Classes: Builder

Constant Summary collapse

PREFIX =

Prefix added to the beginning of every Cryptosphere node

"blob::"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, key) ⇒ Blob

Returns a new instance of Blob.



38
39
40
41
# File 'lib/cryptosphere/blobs/blob.rb', line 38

def initialize(id, key)
  @id, @key = id, key
  @path = File.join(self.class.path, @id)
end

Class Attribute Details

.pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/cryptosphere/blobs/blob.rb', line 12

def path
  @path
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/cryptosphere/blobs/blob.rb', line 9

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/cryptosphere/blobs/blob.rb', line 9

def key
  @key
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/cryptosphere/blobs/blob.rb', line 9

def path
  @path
end

Class Method Details

.[](obj) ⇒ Object

Create a node from a given object



31
32
33
34
35
# File 'lib/cryptosphere/blobs/blob.rb', line 31

def [](obj)
  builder = Builder.new
  builder << obj
  builder.finish
end

.setup(options = {}) ⇒ Object

Configure the Cryptosphere Node store



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cryptosphere/blobs/blob.rb', line 15

def setup(options = {})
  unless options[:root]
    raise ArgumentError, "no :root path given"
  end

  unless File.directory? options[:root]
    raise ArgumentError, "no such directory: #{options[:root]}"
  end

  @path = File.expand_path("nodes", options[:root])
  FileUtils.mkdir @path unless File.directory? @path

  nil
end

Instance Method Details

#decryptObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cryptosphere/blobs/blob.rb', line 43

def decrypt
  raise "can't decrypt node without key" unless @key

  cipher = Cryptosphere.block_cipher
  cipher.decrypt
  cipher.key = @key[0...32]
  cipher.iv  = @key[32...64]

  output = ''

  File.open(@path, 'r') do |file|
    while data = file.read(4096)
      output << cipher.update(data)
    end
  end

  output << cipher.final
end