Module: BB::Crypto

Defined in:
lib/blackbox/crypto.rb

Overview

Crypto utilities.

Defined Under Namespace

Classes: ControlToken

Class Method Summary (collapse)

Class Method Details

+ (String) decrypt(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)

Decrypt a String.

Parameters:

  • ciphertext (String)

    Input String (ciphertext)

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/blackbox/crypto.rb', line 38

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 0 == iv_len
  else
    cipher.iv = iv
  end
  cipher.update(ciphertext) + cipher.final
end

+ (String) decrypt_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)

Decode and Decrypt a Base64-String.

Parameters:

  • ciphertext (String)

    Input String (base64(ciphertext))

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:



70
71
72
# File 'lib/blackbox/crypto.rb', line 70

def decrypt_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
  decrypt(Base64.decode64(ciphertext), key, cipher_type, iv)
end

+ (String) decrypt_urlsafe_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)

Decode and Decrypt an urlsafe Base64-String.

Parameters:

  • ciphertext (String)

    Input String (urlsafe_base64(ciphertext))

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:



93
94
95
# File 'lib/blackbox/crypto.rb', line 93

def decrypt_urlsafe_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
  decrypt(Base64.urlsafe_decode64(ciphertext), key, cipher_type, iv)
end

+ (String) encrypt(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)

Encrypt a String.

Parameters:

  • plaintext (String)

    Input String (plaintext)

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:

  • (String)

    When iv == nil: iv_length+iv+ciphertext

  • (String)

    When iv != nil: ciphertext



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blackbox/crypto.rb', line 18

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

+ (String) encrypt_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)

Encrypt a String and encode the resulting ciphertext to Base64.

Parameters:

  • plaintext (String)

    Input String (plaintext)

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:

  • (String)

    When iv == nil: base64(iv_length+iv+ciphertext)

  • (String)

    When iv != nil: base64(ciphertext)



59
60
61
# File 'lib/blackbox/crypto.rb', line 59

def encrypt_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
  Base64.strict_encode64(encrypt(plaintext, key, cipher_type, iv))
end

+ (String) encrypt_urlsafe_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)

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

Parameters:

  • plaintext (String)

    Input String (plaintext)

  • key (String)

    Encryption key

  • cipher_type (String) (defaults to: 'aes-256-cbc')

    OpenSSL cipher

  • iv (String) (defaults to: nil)

    Initialization vector

Returns:

  • (String)

    When iv == nil: urlsafe_base64(iv_length+iv+ciphertext)

  • (String)

    When iv != nil: urlsafe_base64(ciphertext)



82
83
84
# File 'lib/blackbox/crypto.rb', line 82

def encrypt_urlsafe_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
  Base64.urlsafe_encode64(encrypt(plaintext, key, cipher_type, iv))
end