Sha256: f8f2bcbfd230b5a7d4fa6a4eb60caf60a83eb0893af24fb1ea383608731e2413
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
require 'openssl' module URLcrypt # 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 TABLE = "1bcd2fgh3jklmn4pqrstAvwxyz567890".freeze def self.key=(key) @key = key end class Chunk def initialize(bytes) @bytes = bytes end def decode bytes = @bytes.take_while {|c| c != 61} # strip padding bytes = bytes.find_all{|b| !TABLE.index(b.chr).nil? } # remove invalid characters n = (bytes.length * 5.0 / 8.0).floor p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0 c = bytes.inject(0) {|m,o| (m << 5) + TABLE.index(o.chr)} >> p (0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr} end def encode n = (@bytes.length * 8.0 / 5.0).ceil p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0 c = @bytes.inject(0) {|m,o| (m << 8) + o} << p [(0..n-1).to_a.reverse.collect {|i| TABLE[(c >> i * 5) & 0x1f].chr}, ("=" * (8-n))] # TODO: remove '=' padding generation end end 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 # strip '=' padding, because we don't need it def self.encode(data) chunks(data, 5).collect(&:encode).flatten.join.tr('=','') end def self.decode(data) chunks(data, 8).collect(&:decode).flatten.join end 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 def self.encrypt(data) crypter = cipher(:encrypt) crypter.iv = iv = crypter.random_iv "#{encode(iv)}Z#{encode(crypter.update(data) + crypter.final)}" end private def self.cipher(mode) cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.send(mode) cipher.key = @key cipher end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
urlcrypt-0.1.0 | lib/URLcrypt.rb |