Class: String

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

Overview

String

Small addon to String class

Instance Method Summary (collapse)

Instance Method Details

- (Object) by_five

by_five

Slice input into 5-letter groups



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/crypto_helper.rb', line 143

def by_five
  str = self.dup
  l = str.length
  r = l % 5
  if l <= 5 then
    return str
  else
    a = str.scan(%r{(\w{5})}).join(' ')
  end
  if r != 0 then
    a += ' '+ str[-r, r]
  end
  a
end

- (Object) condensed

condensed

Condense word by removing every duplicated letter



20
21
22
23
24
25
26
27
# File 'lib/crypto_helper.rb', line 20

def condensed
  self.each_char.inject('') {|s,c|
    if s.include?(c)
      c = ''
    end
    s+c
  }
end

- (Object) expand(letter = 'X')

expand

Insert an X between identical letters (mostly used for bigrammatic ciphers such as Playfair)



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/crypto_helper.rb', line 34

def expand(letter = 'X')
  a_str = self.split(//)
  i = 0
  while i < a_str.length do
    if a_str[i] == a_str[i+1] then
      a_str.insert(i+1, letter)
    end
    i += 2
  end
  a_str.join.to_s
end

- (Object) frequency(a = nil)

frequency

Returns the frequency of each letters (or only the ones given)



171
172
173
174
175
176
177
178
179
# File 'lib/crypto_helper.rb', line 171

def frequency(a = nil)
  if a.nil?
    a = self.dup.condensed
  end

  a.scan(/./).collect do |i|
      [i, self.count(i)]
  end
end

- (Object) replace_double(letter = 'Q')

replace_double

Replace the second of two identical letters by Q (used for Wheatstone cipher)



51
52
53
54
55
56
57
58
# File 'lib/crypto_helper.rb', line 51

def replace_double(letter = 'Q')
  self.each_char.inject('') {|s,c|
    if s[-1] == c
      c = letter
    end
    s+c
  }
end

- (Object) to_numeric

to_numeric

Generate a numeric key from a keyword

For each letter in the keyword, scan for the lowest letter, assign it an index # then scan again till there are no letter left

XXX modified to be 0-based

By Dave Thomas, IRC #ruby-lang on Thu Aug 9 17:36:39 CEST 2001



71
72
73
74
75
76
77
78
79
80
# File 'lib/crypto_helper.rb', line 71

def to_numeric
  letters = self.to_s.split('')
  sorted = letters.sort
  num_key = letters.collect do |l|
    k = sorted.index(l)
    sorted[k] = nil
    k
  end
  num_key
end

- (Object) to_numeric10

to_numeric10

1-based version modulo 10

Based on: For each letter in the keyword, scan for the lowest letter, assign it an index # then scan again till there are no letter left

By Dave Thomas, IRC #ruby-lang on Thu Aug 9 17:36:39 CEST 2001



111
112
113
114
115
116
117
118
119
120
# File 'lib/crypto_helper.rb', line 111

def to_numeric10
  letters = self.to_s.split('')
  sorted = letters.sort
  num_key = letters.collect do |l|
    k = sorted.index(l)
    sorted[k] = nil
    (k + 1) % 10
  end
  num_key
end

- (Object) to_numeric11

to_numeric11

1-based version modulo 10

Based on: Alternate version By dblack, IRC #ruby-lang



130
131
132
133
134
135
136
137
# File 'lib/crypto_helper.rb', line 130

def to_numeric11
  srt = self.split('').sort

  n_key = self.split('').map do |s|
    srt[srt.index(s)] = (srt.index(s) + 1) % 10
  end
  n_key
end

- (Object) to_numeric2

to_numeric2

Alternate version By dblack, IRC #ruby-lang

This version is more than 3 times as slow as #to_numeric See misc/bench.rb

XXX modified to be 0-based



92
93
94
95
96
97
98
99
# File 'lib/crypto_helper.rb', line 92

def to_numeric2
  srt = self.split('').sort

  n_key = self.split('').map do |s|
    srt[srt.index(s)] = srt.index(s)
  end
  n_key
end

- (Object) un_five

un_five

Reverse #by_five



162
163
164
165
# File 'lib/crypto_helper.rb', line 162

def un_five
  str = self.dup
  str.gsub(%r{ }, '')
end