Module: URLcrypt
- Defined in:
- lib/URLcrypt.rb
Defined Under Namespace
Classes: Chunk, DecryptError
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
62
|
# File 'lib/URLcrypt.rb', line 56
def self.decrypt(data)
iv, encrypted = data.split('Z').map{|part| decode(part)}
fail DecryptError, "not a valid string to decrypt" unless iv && encrypted
decrypter = cipher(:decrypt)
decrypter.iv = iv
decrypter.update(encrypted) + 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
64
65
66
67
68
|
# File 'lib/URLcrypt.rb', line 64
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
|