Class: Key::SCKey

Inherits:
SKey show all
Includes:
Crypto
Defined in:
lib/key/sckey.rb

Overview

SCKey

class for straddling checkerboard substitution keys

SC-keys needs to be condensed and rings generated for ciphering/deciphering

See en.wikipedia.org/wiki/Straddling_checkerboard

Constant Summary

BASE =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ/-'

Instance Attribute Summary (collapse)

Attributes inherited from SKey

#alpha, #ralpha

Instance Method Summary (collapse)

Methods included from Crypto

#addmod10, #chainadd, #expand5to10, #find_hole, #keyshuffle, #normalize, #p1_encode, #str_to_numeric, #submod10

Methods inherited from SKey

#decode, #encode

Constructor Details

- (SCKey) initialize(key, longc = [ 8, 9 ])

Returns a new instance of SCKey



26
27
28
29
30
31
# File 'lib/key/sckey.rb', line 26

def initialize(key, longc = [ 8, 9 ])
  super(key)
  @longc = longc
  @full_key = keyshuffle(@key, BASE)
  gen_rings()
end

Instance Attribute Details

- (Object) full_key (readonly)

Returns the value of attribute full_key



24
25
26
# File 'lib/key/sckey.rb', line 24

def full_key
  @full_key
end

- (Object) longc (readonly)

Returns the value of attribute longc



24
25
26
# File 'lib/key/sckey.rb', line 24

def longc
  @longc
end

- (Object) shortc (readonly)

Returns the value of attribute shortc



24
25
26
# File 'lib/key/sckey.rb', line 24

def shortc
  @shortc
end

Instance Method Details

- (Object) gen_rings

gen_rings

Assign a code number for each letter. Each code number is sequentially allocated from two pools, one with 0..7 and the other with 80..99.

Allocation is made on the following criterias

  • if letter is one of ESANTIRU assign a single code number

  • else assign of of the two letters ones

Generate both the encoding and decoding rings.

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/key/sckey.rb', line 45

def gen_rings
  shortc = (0..9).collect{|i| i unless @longc.include?(i) }.compact
  raise DataError if shortc.nil?
  long = @longc.collect{|i| (0..9).collect{|j| i*10+j } }.flatten
  raise DataError if long.nil?
  @shortc = shortc.dup

  word = @full_key.dup
  word.scan(/./) do |c|
    if 'ESANTIRU'.include? c then
      ind = shortc.shift
    else
      ind = long.shift
    end
    @alpha[c] = ind.to_s
    @ralpha[ind.to_s] = c
  end
end

- (Boolean) is_long?(digit)

is_long?

Returns:

  • (Boolean)


66
67
68
# File 'lib/key/sckey.rb', line 66

def is_long?(digit)
  return @longc.include?(digit)
end