Class: Rmega::Nodes::NodeKey

Inherits:
Object
  • Object
show all
Defined in:
lib/rmega/nodes/node_key.rb

Overview

The key associated to a node. It can be 128 or 256 bits long, when is 256 bits long is composed by:

  • A 128 bit AES-128 key

  • The upper 64 bit of the counter start value (the lower 64 bit are starting at 0 and incrementing by 1 for each AES block of 16 bytes)

  • A 64 bit meta-MAC of all chunk MACs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ NodeKey

Returns a new instance of NodeKey.



15
16
17
18
19
# File 'lib/rmega/nodes/node_key.rb', line 15

def initialize(string)
  @aes_key   = string[0..15]
  @ctr_nonce = string[16..23]
  @meta_mac  = string[24..31]
end

Instance Attribute Details

#aes_keyObject (readonly)

Returns the value of attribute aes_key.



12
13
14
# File 'lib/rmega/nodes/node_key.rb', line 12

def aes_key
  @aes_key
end

#ctr_nonceObject (readonly)

Returns the value of attribute ctr_nonce.



12
13
14
# File 'lib/rmega/nodes/node_key.rb', line 12

def ctr_nonce
  @ctr_nonce
end

#meta_macObject

Returns the value of attribute meta_mac.



12
13
14
# File 'lib/rmega/nodes/node_key.rb', line 12

def meta_mac
  @meta_mac
end

Class Method Details

.compact(string) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rmega/nodes/node_key.rb', line 30

def self.compact(string)
  if string.size > 16
    bytes = string.bytes.to_a
    return 16.times.inject([]) { |ary, i| ary[i] = bytes[i] ^ bytes[i+16]; ary }.map(&:chr).join
  else
    return string
  end
end

.load(string) ⇒ Object

note: folder key is 16 bytes long while file key is 32



26
27
28
# File 'lib/rmega/nodes/node_key.rb', line 26

def self.load(string)
  new("#{compact(string)}#{string[16..-1]}")
end

.randomObject



39
40
41
# File 'lib/rmega/nodes/node_key.rb', line 39

def self.random
  new(OpenSSL::Random.random_bytes(16 + 8 + 0))
end

Instance Method Details

#generateObject



21
22
23
# File 'lib/rmega/nodes/node_key.rb', line 21

def generate
  self.class.compact("#{@aes_key}#{@ctr_nonce}#{@meta_mac}") + @ctr_nonce + @meta_mac
end