Class: KeySchedule

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

Constant Summary collapse

PC_1_L =
[0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09,
0x01, 0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12,
0x0a, 0x02, 0x3b, 0x33, 0x2b, 0x23, 0x1b,
0x13, 0x0b, 0x03, 0x3c, 0x34, 0x2c, 0x24]
PC_1_R =
[0x3f, 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f,
0x07, 0x3e, 0x36, 0x2e, 0x26, 0x1e, 0x16,
0x0e, 0x06, 0x3d, 0x35, 0x2d, 0x25, 0x1d,
0x15, 0x0d, 0x05, 0x1c, 0x14, 0x0c, 0x04]
PC_2 =
[0x0e, 0x11, 0x0b, 0x18, 0x01, 0x05,
0x03, 0x1c, 0x0f, 0x06, 0x15, 0x0a,
0x17, 0x13, 0x0c, 0x04, 0x1a, 0x08,
0x10, 0x07, 0x1b, 0x14, 0x0d, 0x02,
0x29, 0x34, 0x1f, 0x25, 0x2f, 0x37,
0x1e, 0x28, 0x33, 0x2d, 0x21, 0x30,
0x2c, 0x31, 0x27, 0x38, 0x22, 0x35,
0x2e, 0x2a, 0x32, 0x24, 0x1d, 0x20]
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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby-des/key_schedule.rb', line 27

def initialize(key)
  @key = key
  
  c = [] # c[0] is the PC_1_L permutation of the key, c[1..16] are the results of each left shift.
  d = [] # d[0] is the PC_1_R permutation of the key, d[1..16] are the results of each left shift.
  k = [] # k[0..15] are the sub keys created by combining c[i] and d[i] and permuting with PC_2.
  
  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/ruby-des/key_schedule.rb', line 3

def key
  @key
end

#sub_keysObject

Returns the value of attribute sub_keys.



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

def sub_keys
  @sub_keys
end