Class: PasswordRecord

Inherits:
BiffRecord show all
Defined in:
lib/surpass/biff_record.rb

Overview

This record is part of the worksheet/workbook protection. It stores a 16-bit hash value, calculated from the worksheet or workbook protection password.

Constant Summary collapse

RECORD_ID =
0x0013

Constants inherited from BiffRecord

BiffRecord::BIFF_LIMIT, BiffRecord::CONTINUE_RECORD_ID

Instance Attribute Summary

Attributes inherited from BiffRecord

#record_data

Instance Method Summary collapse

Methods inherited from BiffRecord

#record_header, #to_biff

Constructor Details

#initialize(password = "") ⇒ PasswordRecord

Returns a new instance of PasswordRecord.



289
290
291
# File 'lib/surpass/biff_record.rb', line 289

def initialize(password = "")
  @record_data = [password_hash(password)].pack('v')
end

Instance Method Details

#password_hash(plaintext) ⇒ Object

Based on the algorithm provided by Daniel Rentz of OpenOffice.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/surpass/biff_record.rb', line 294

def password_hash(plaintext)
  return 0 if plaintext === ""
  hash = 0x0000
  plaintext.unpack('C*').each_with_index do |t, i|
    c = t << (i + 1)
    low_15 = c & 0x7fff
    high_15 = c & 0x7fff << 15
    high_15 = high_15 >> 15
    c = low_15 | high_15
    hash = (hash ^ c)
  end
  hash = (hash ^ plaintext.length)
  hash = (hash ^ 0xCE4B)
  hash
end