File: password_hash.rb

package info (click to toggle)
ruby-spreadsheet 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,964 kB
  • sloc: ruby: 6,943; makefile: 10
file content (24 lines) | stat: -rw-r--r-- 468 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Spreadsheet
  module Excel
module Password
  class <<self
    ##
    # Makes an excel-compatible hash
    def password_hash(password)
      hash = 0
      password.chars.reverse_each { |chr| hash = rol15(hash ^ chr[0].ord) }
      hash ^ password.size ^ 0xCE4B
    end

    private
    ##
    # rotates hash 1 bit left, using lower 15 bits
    def rol15(hash)
      new_hash = hash << 1
      (new_hash & 0x7FFF) | (new_hash >> 15)
    end
  end

end
  end
end