Class: Voltron::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/voltron/encrypt.rb,
lib/voltron/encrypt/engine.rb,
lib/voltron/encrypt/version.rb,
lib/generators/voltron/encrypt/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Engine

Constant Summary collapse

VERSION =
"0.1.2".freeze

Instance Method Summary collapse

Instance Method Details

#blacklisted?(input) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/voltron/encrypt.rb', line 37

def blacklisted?(input)
  encoded = encode(input)

  pattern = ["\\b([_\\-])*"]
  encoded.split("").each do |c|
    subs = translations[c.downcase] || []
    c = "\\#{c}" if c == "-"
    pattern << "[#{c}#{subs.join}]([_\\-])*"
  end
  pattern << "\\b"

  regex = Regexp.new(pattern.join, Regexp::IGNORECASE)
  !blacklist(encoded.length).match(regex).nil?
end

#decode(input) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/voltron/encrypt.rb', line 25

def decode(input)
  inp = input.to_s.split("")
  out = 0

  begin
    chr = inp.shift
    out += (digits.length**inp.length)*digits.index(chr)
  end until inp.empty?

  out - Voltron.config.encrypt.offset.to_i # Decrease the number by the same offset amount
end

#encode(input) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/voltron/encrypt.rb', line 9

def encode(input)
  radix = digits.length
  i = input.to_i + Voltron.config.encrypt.offset.to_i # Increase the number just so we don't end up with id's like "E" or "d3" on low number ids

  raise ArgumentError.new("Value #{val} cannot be less than zero") if i < 0

  out = []
  begin
    rem = i % radix
    i /= radix
    out << digits[rem]
  end until i == 0

  out.reverse.join
end