Class: KeySchedule

Inherits:
Object
  • Object
show all
Defined in:
lib/des/key_schedule.rb

Constant Summary collapse

PC_1_L =
[57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36]
PC_1_R =
[63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4]
PC_2 =
[14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47,
55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32]
ROTATIONS =
[1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ KeySchedule

Returns a new instance of KeySchedule.



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
# File 'lib/des/key_schedule.rb', line 13

def initialize(key)
  @key = key

  # c[0] is the PC_1_L permutation of the key, c[1..16] are the results of each left shift.
  c = []
  # d[0] is the PC_1_R permutation of the key, d[1..16] are the results of each left shift.
  d = []
  # k[0..15] are the sub keys created by combining c[i] and d[i] and permuting with PC_2.
  k = []

  c << PC_1_L.collect { |p| key[p - 1] }
  d << PC_1_R.collect { |p| key[p - 1] }

  16.times do |i|
    c << c[i]
    d << d[i]

    ROTATIONS[i].times do
      c[i + 1] << c[i + 1].shift
      d[i + 1] << d[i + 1].shift
    end

    k << PC_2.collect { |p| (c[i + 1] + d[i + 1])[p - 1] }
  end

  @sub_keys = k
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/des/key_schedule.rb', line 3

def key
  @key
end

#sub_keysObject

Returns the value of attribute sub_keys.



2
3
4
# File 'lib/des/key_schedule.rb', line 2

def sub_keys
  @sub_keys
end