Class: SetecAstronomy::KeePass::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/setec_astronomy/kee_pass/header.rb

Constant Summary collapse

ENCRYPTION_FLAGS =
[
   [1 , 'SHA2'    ],
   [2 , 'Rijndael'],
   [2 , 'AES'     ],
   [4 , 'ArcFour' ],
   [8 , 'TwoFish' ]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_bytes) ⇒ Header

Returns a new instance of Header.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/setec_astronomy/kee_pass/header.rb', line 50

def initialize(header_bytes)
  @signature1 = header_bytes[0..4].unpack('L*').first
  @signature2 = header_bytes[4..8].unpack('L*').first
  @flags   = header_bytes[8..12].unpack('L*').first
  @version = header_bytes[12..16].unpack('L*').first
  @master_seed = header_bytes[16...32]
  @encryption_iv = header_bytes[32...48]
  @ngroups = header_bytes[48..52].unpack('L*').first
  @nentries = header_bytes[52..56].unpack('L*').first
  @contents_hash = header_bytes[56..88]
  @master_seed2 = header_bytes[88...120]
  @rounds = header_bytes[120..-1].unpack('L*').first
end

Instance Attribute Details

#encryption_ivObject (readonly)

Returns the value of attribute encryption_iv.



47
48
49
# File 'lib/setec_astronomy/kee_pass/header.rb', line 47

def encryption_iv
  @encryption_iv
end

#nentriesObject (readonly)

Returns the value of attribute nentries.



48
49
50
# File 'lib/setec_astronomy/kee_pass/header.rb', line 48

def nentries
  @nentries
end

#ngroupsObject (readonly)

Returns the value of attribute ngroups.



48
49
50
# File 'lib/setec_astronomy/kee_pass/header.rb', line 48

def ngroups
  @ngroups
end

Instance Method Details

#encryption_typeObject



68
69
70
71
72
73
# File 'lib/setec_astronomy/kee_pass/header.rb', line 68

def encryption_type
  ENCRYPTION_FLAGS.each do |(flag_mask, encryption_type)|
    return encryption_type if @flags & flag_mask
  end
  'Unknown'
end

#final_key(master_key) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/setec_astronomy/kee_pass/header.rb', line 75

def final_key(master_key)
  key = Digest::SHA2.new.update(master_key).digest
  aes = FastAES.new(@master_seed2)

  @rounds.times do |i|
    key = aes.encrypt(key)
  end

  key = Digest::SHA2.new.update(key).digest
  key = Digest::SHA2.new.update(@master_seed + key).digest
  key
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/setec_astronomy/kee_pass/header.rb', line 64

def valid?
  @signature1 == 0x9AA2D903 && @signature2 == 0xB54BFB65
end