Module: URLcrypt

Defined in:
lib/URLcrypt.rb

Defined Under Namespace

Classes: Chunk

Constant Summary collapse

TABLE =

avoid vowels to not generate four-letter words, etc. this is important because those words can trigger spam filters when URLs are used in emails

"1bcd2fgh3jklmn4pqrstAvwxyz567890".freeze

Class Method Summary collapse

Class Method Details

.chunks(str, size) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/URLcrypt.rb', line 37

def self.chunks(str, size)
  result = []
  bytes = str.bytes
  while bytes.any? do
    result << Chunk.new(bytes.take(size))
    bytes = bytes.drop(size)
  end
  result
end

.decode(data) ⇒ Object



52
53
54
# File 'lib/URLcrypt.rb', line 52

def self.decode(data)
  chunks(data, 8).collect(&:decode).flatten.join
end

.decrypt(data) ⇒ Object



56
57
58
59
60
61
# File 'lib/URLcrypt.rb', line 56

def self.decrypt(data)
  parts = data.split('Z').map{|part| decode(part)}
  decrypter = cipher(:decrypt)
  decrypter.iv = parts[0]
  decrypter.update(parts[1]) + decrypter.final 
end

.encode(data) ⇒ Object

strip ‘=’ padding, because we don’t need it



48
49
50
# File 'lib/URLcrypt.rb', line 48

def self.encode(data)
  chunks(data, 5).collect(&:encode).flatten.join.tr('=','')
end

.encrypt(data) ⇒ Object



63
64
65
66
67
# File 'lib/URLcrypt.rb', line 63

def self.encrypt(data)
  crypter = cipher(:encrypt)
  crypter.iv = iv = crypter.random_iv
  "#{encode(iv)}Z#{encode(crypter.update(data) + crypter.final)}"
end

.key=(key) ⇒ Object



9
10
11
# File 'lib/URLcrypt.rb', line 9

def self.key=(key)
  @key = key
end