Module: Signer::Key

Defined in:
lib/signer.rb

Class Method Summary collapse

Class Method Details

.read(io, wmid, password = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/signer.rb', line 9

def self.read(io, wmid, password = nil)
  if !password.nil?

    header = io.read 30
    data = io.read

    begin
      return try_read_encrypted wmid, password, header, data
    rescue
      return try_read_encrypted wmid, password[0...password.length / 2], header, data
    end
  end

  reserved1, sign_flag = io.read(4).unpack("vv")
  crc    = io.read(16)
  length, = io.read(4).unpack("V")

  data = io.read
  raise "unexpected data length" if length != data.length

  digest = OpenSSL::Digest::MD4.new
  digest.update [ reserved1, 0 ].pack("v*")
  digest.update [ 0, 0, 0, 0, length ].pack("V*")
  digest.update data

  calculated_crc = digest.digest

  raise "invalid key digest" if crc != calculated_crc

  data_io = StringIO.new data[4..-1], 'rb'

  e = read_bn_from data_io
  n = read_bn_from data_io

  { :e => e, :n => n }
end

.read_bn_from(io) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/signer.rb', line 55

def self.read_bn_from(io)
  bytes, = io.read(2).unpack("v")

  data = io.read(bytes).reverse

  OpenSSL::BN.new(data, 2)
end

.try_read_encrypted(wmid, password, header, data) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/signer.rb', line 46

def self.try_read_encrypted(wmid, password, header, data)
    nested_io = StringIO.new ''.encode('BINARY'), 'rb+'
    nested_io.write header
    nested_io.write wm_encrypt(wmid, password, data)
    nested_io.rewind

    return self.read nested_io, wmid
end

.wm_encrypt(wmid, password, data) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/signer.rb', line 63

def self.wm_encrypt(wmid, password, data)
  data = data.dup

  digest = OpenSSL::Digest::MD4.new
  digest.update wmid
  digest.update password

  key = digest.digest.unpack("C*")
  i = 0
  while i < data.length
    data[i] = (data[i].ord ^ key[i % key.length]).chr

    i += 1
  end

  data
end