module RecaptchaMailhide::Encrypt

Constants

BLOCK_SIZE
INITIALIZATION_VECTOR

Public Class Methods

encrypt(string) click to toggle source

Encrpyts the given string using AES 128 and returns the result in URL-safe Base64 form.

# File lib/recaptcha_mailhide/encrypt.rb, line 12
def self.encrypt(string)
  aes = build_aes
  urlsafe_base64(aes.update(pad_string(string)) + aes.final)
end

Private Class Methods

binary_public_key() click to toggle source

Returns the private key converted from hex to binary.

# File lib/recaptcha_mailhide/encrypt.rb, line 52
def self.binary_public_key
  RecaptchaMailhide.configuration.private_key.unpack('a2' * 16).map {|x| x.hex }.pack('c' * 16)
end
build_aes() click to toggle source

Initializes the cipher.

# File lib/recaptcha_mailhide/encrypt.rb, line 38
def self.build_aes
  aes = OpenSSL::Cipher.new("aes-128-cbc")
  aes.encrypt
  aes.iv = INITIALIZATION_VECTOR
  aes.padding = 0

  aes.key = binary_public_key

  aes
end
pad_string(string) click to toggle source

Pads the given string to blocks of 16 bytes using bytes representing the number of bytes used for padding.

# File lib/recaptcha_mailhide/encrypt.rb, line 31
def self.pad_string(string)
  padding = BLOCK_SIZE - (string.length % BLOCK_SIZE)
  string + padding.chr * padding
end
urlsafe_base64(string) click to toggle source

Converts the given string to Base64 encoding, replacing '+' with '-', '/' with '_' and removing newlines in order to make it URL-safe.

# File lib/recaptcha_mailhide/encrypt.rb, line 23
def self.urlsafe_base64(string)
  Base64.encode64(string).tr('+/', '-_').gsub("\n", '')
end