module BB::Crypto

Crypto utilities.

Public Class Methods

decrypt(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Decrypt a String.

@param [String] ciphertext Input String (ciphertext) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] Plaintext

# File lib/blackbox/crypto.rb, line 39
def decrypt(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
  cipher = OpenSSL::Cipher.new(cipher_type)
  cipher.decrypt
  cipher.key = key
  if iv.nil?
    iv_len = ciphertext.slice!(0).unpack('C')[0]
    cipher.iv = ciphertext.slice!(0..iv_len - 1) unless iv_len == 0
  else
    cipher.iv = iv
  end
  cipher.update(ciphertext) + cipher.final
end
decrypt_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Decode and Decrypt a Base64-String.

@param [String] ciphertext Input String (base64(ciphertext)) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] Plaintext

# File lib/blackbox/crypto.rb, line 71
def decrypt_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
  decrypt(Base64.decode64(ciphertext), key, cipher_type, iv)
end
decrypt_urlsafe_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Decode and Decrypt an urlsafe Base64-String.

@param [String] ciphertext Input String (urlsafe_base64(ciphertext)) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] Plaintext

# File lib/blackbox/crypto.rb, line 94
def decrypt_urlsafe_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
  decrypt(Base64.urlsafe_decode64(ciphertext), key, cipher_type, iv)
end
encrypt(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Encrypt a String.

@param [String] plaintext Input String (plaintext) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] When iv == nil: iv_length+iv+ciphertext @return [String] When iv != nil: ciphertext

# File lib/blackbox/crypto.rb, line 19
def encrypt(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
  cipher = OpenSSL::Cipher.new(cipher_type)
  cipher.encrypt
  cipher.key = key
  if iv.nil?
    iv = cipher.random_iv
    [iv.length].pack('C') + iv + cipher.update(plaintext) + cipher.final
  else
    cipher.iv = iv
    cipher.update(plaintext) + cipher.final
  end
end
encrypt_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Encrypt a String and encode the resulting ciphertext to Base64.

@param [String] plaintext Input String (plaintext) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] When iv == nil: base64(iv_length+iv+ciphertext) @return [String] When iv != nil: base64(ciphertext)

# File lib/blackbox/crypto.rb, line 60
def encrypt_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
  Base64.strict_encode64(encrypt(plaintext, key, cipher_type, iv))
end
encrypt_urlsafe_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil) click to toggle source

Encrypt a String and encode the resulting ciphertext to urlsafe Base64.

@param [String] plaintext Input String (plaintext) @param [String] key Encryption key @param [String] cipher_type OpenSSL cipher @param [String] iv Initialization vector @return [String] When iv == nil: urlsafe_base64(iv_length+iv+ciphertext) @return [String] When iv != nil: urlsafe_base64(ciphertext)

# File lib/blackbox/crypto.rb, line 83
def encrypt_urlsafe_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
  Base64.urlsafe_encode64(encrypt(plaintext, key, cipher_type, iv))
end