Class: GravityFallsMessage::Cipher

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

Class Method Summary collapse

Class Method Details

.a1z26(arr, encode = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gravity_falls_message/cipher.rb', line 8

def self.a1z26 arr, encode=false
  answer = arr.collect do |c|
    if /-/ === c
      ''
    elsif /\W/ === c
      c
    elsif c != ''
      encode ? alphabet.index(c.upcase) + 1 : alphabet[c.to_i - 1]
    end
  end
  encode ? answer.join('-').gsub(/-?(\s|")-/, '\1').gsub(/-(\W)-?/,'\1') : answer.join('')
end

.alphabetObject



4
5
6
# File 'lib/gravity_falls_message/cipher.rb', line 4

def self.alphabet
  ('A'..'Z').to_a
end

.atbash(arr, encode = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/gravity_falls_message/cipher.rb', line 21

def self.atbash arr, encode=false
  answer = arr.collect do |c|
    if /\W/ === c
      c
    else
      encode ? alphabet[alphabet.reverse.index(c.upcase)] : alphabet.reverse[alphabet.index(c.upcase)]
    end
  end
  answer.join('')
end

.binary(input, encode = false) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/gravity_falls_message/cipher.rb', line 32

def self.binary input, encode=false
  if encode
    answer = input.unpack('B*')[0]
  else
    answer = ''
    (0..input.length).step(8).each {|i| answer << input[i,8].to_i(2).chr}
  end
  answer.delete("\0")
end

.caesar(arr, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gravity_falls_message/cipher.rb', line 42

def self.caesar arr, options={}
  encode = options[:encode]
  default_shift = options[:shift] || 3
  key = options[:key].gsub(/[^A-Z]/i,'').upcase.split(//) if options[:key]
  answer = arr.collect do |c|
    val = key ? alphabet.index(key[0]) : default_shift
    shift_value = encode ? val : (val * -1)
    if /\W/ === c
      c
    else
      ind = alphabet.index(c.upcase) + shift_value
      ind = ind >= 26 ? ind - 26 : ind
      key.rotate! if key
      alphabet[ind]
    end
  end
  answer.join('').upcase
end

.vigenere(arr, options = {}) ⇒ Object



61
62
63
# File 'lib/gravity_falls_message/cipher.rb', line 61

def self.vigenere arr, options={}
  caesar(arr, options)
end