Class: Tassadar::MPQ::BlockEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/tassadar/mpq/block_encryptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, offset, buffer, size) ⇒ BlockEncryptor

Returns a new instance of BlockEncryptor.



6
7
8
9
10
11
# File 'lib/tassadar/mpq/block_encryptor.rb', line 6

def initialize(key, offset, buffer, size)
  @key = key
  @offset = offset
  @buffer = buffer
  @size = size
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



4
5
6
# File 'lib/tassadar/mpq/block_encryptor.rb', line 4

def buffer
  @buffer
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/tassadar/mpq/block_encryptor.rb', line 4

def key
  @key
end

#offsetObject

Returns the value of attribute offset.



4
5
6
# File 'lib/tassadar/mpq/block_encryptor.rb', line 4

def offset
  @offset
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/tassadar/mpq/block_encryptor.rb', line 4

def size
  @size
end

Class Method Details

.hash_string(key, offset) ⇒ Object



13
14
15
# File 'lib/tassadar/mpq/block_encryptor.rb', line 13

def self.hash_string(key, offset)
  BlockEncryptor.new(key, offset, nil, nil).hash_string
end

Instance Method Details

#decryptObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tassadar/mpq/block_encryptor.rb', line 17

def decrypt
  seed1 = hash_string
  seed2 = 0xEEEEEEEE
  result = ""
  size = @size

  while size >= 4
    seed2 += encryption_table[0x400 + (seed1 & 0xFF)]
    seed2 &= 0xFFFFFFFF

    value = buffer.readbytes(4).unpack("V").first
    value = (value ^ (seed1 + seed2)) & 0xFFFFFFFF

    result += [value].pack("V")

    seed1 = ((~seed1 << 0x15) + 0x11111111) | (seed1 >> 0x0B)
    seed1 &= 0xFFFFFFFF
    seed2 = value + seed2 + (seed2 << 5) + 3 & 0xFFFFFFFF
    size = size - 4
  end

  result
end

#encryptObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tassadar/mpq/block_encryptor.rb', line 41

def encrypt
  seed1 = hash_string
  seed2 = 0xEEEEEEEE
  encrypted_block = []

  while @size >= 4
    seed2 += encryption_table[0x400 + (seed1 & 0xFF)]
    seed2 &= 0xFFFFFFFF

    value = buffer.readbytes(4).unpack("V").first
    value = (value ^ (seed1 + seed2)) & 0xFFFFFFFF

    encrypted_block << value

    seed = ((~seed1 << 0x15) + 0x11111111) | (seed1 >> 0x0B)
    seed2 = value + seed2 + (seed2 << 5) + 3 & 0xFFFFFFFF

    @size = @size - 4
  end

  encrypted_block.pack("V*")
end

#hash_stringObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tassadar/mpq/block_encryptor.rb', line 64

def hash_string
  seed1 = 0x7FED7FED
  seed2 = 0xEEEEEEEE

  @key.upcase.each_byte do |char|
    value = encryption_table[@offset + char]
    seed1 = (value ^ (seed1 + seed2)) & 0xFFFFFFFF
    seed2 = char + seed1 + seed2 + (seed2 << 5) + 3 & 0xFFFFFFFF
  end

  seed1
end