Module: BB::Crypto
- Defined in:
- lib/blackbox/crypto.rb
Overview
Crypto utilities.
Defined Under Namespace
Classes: ControlToken
Class Method Summary (collapse)
-
+ (String) decrypt(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
Decrypt a String.
-
+ (String) decrypt_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
Decode and Decrypt a Base64-String.
-
+ (String) decrypt_urlsafe_base64(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
Decode and Decrypt an urlsafe Base64-String.
-
+ (String) encrypt(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
Encrypt a String.
-
+ (String) encrypt_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
Encrypt a String and encode the resulting ciphertext to Base64.
-
+ (String) encrypt_urlsafe_base64(plaintext, key, cipher_type = 'aes-256-cbc', iv = nil)
Encrypt a String and encode the resulting ciphertext to urlsafe Base64.
Class Method Details
+ (String) decrypt(ciphertext, key, cipher_type = 'aes-256-cbc', iv = nil)
Decrypt a String.
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.
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.
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.
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.
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.
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 |