Class: Ably::Util::Crypto
- Inherits:
-
Object
- Object
- Ably::Util::Crypto
- Defined in:
- lib/submodules/ably-ruby/lib/ably/util/crypto.rb
Constant Summary collapse
- DEFAULTS =
{ algorithm: 'aes', mode: 'cbc', key_length: 256, }
- BLOCK_LENGTH =
16
Instance Attribute Summary collapse
-
#cipher_params ⇒ Ably::Models::CipherParams
readonly
Configured Models::CipherParams for this Crypto object, see #initialize for a list of configureable options.
Class Method Summary collapse
-
.cipher_type(options) ⇒ Object
private
The Cipher algorithm string such as AES-128-CBC.
-
.generate_random_key(key_length = DEFAULTS.fetch(:key_length)) ⇒ Object
Generate a random encryption key from the supplied keylength (or the default key_length of 256 if none supplied).
-
.get_default_params(params = {}) ⇒ Ably::Models::CipherParams
Configured cipher params with :key, :algorithm, :mode, :key_length attributes.
Instance Method Summary collapse
-
#decrypt(encrypted_payload_with_iv) ⇒ String
Decrypt payload using configured Cipher.
-
#encrypt(payload, encrypt_options = {}) ⇒ String
Encrypt payload using configured Cipher.
-
#initialize(params) ⇒ Ably::Util::Crypto
constructor
Creates a Crypto object.
-
#random_iv ⇒ String
Generate a random IV.
Constructor Details
#initialize(params) ⇒ Ably::Util::Crypto
Creates a Ably::Util::Crypto object
32 33 34 35 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 32 def initialize(params) @fixed_iv = params[:fixed_iv] @cipher_params = Ably::Models::CipherParams(params) end |
Instance Attribute Details
#cipher_params ⇒ Ably::Models::CipherParams (readonly)
Configured Models::CipherParams for this Crypto object, see #initialize for a list of configureable options
17 18 19 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 17 def cipher_params @cipher_params end |
Class Method Details
.cipher_type(options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The Cipher algorithm string such as AES-128-CBC
64 65 66 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 64 def self.cipher_type() Ably::Models::CipherParams.cipher_type() end |
.generate_random_key(key_length = DEFAULTS.fetch(:key_length)) ⇒ Object
Generate a random encryption key from the supplied keylength (or the default key_length of 256 if none supplied)
57 58 59 60 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 57 def self.generate_random_key(key_length = DEFAULTS.fetch(:key_length)) params = DEFAULTS.merge(key_length: key_length) OpenSSL::Cipher.new(cipher_type(params)).random_key end |
.get_default_params(params = {}) ⇒ Ably::Models::CipherParams
Returns Configured cipher params with :key, :algorithm, :mode, :key_length attributes.
47 48 49 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 47 def self.get_default_params(params = {}) Ably::Models::CipherParams(params) end |
Instance Method Details
#decrypt(encrypted_payload_with_iv) ⇒ String
Decrypt payload using configured Cipher
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 92 def decrypt(encrypted_payload_with_iv) raise Ably::Exceptions::CipherError, 'iv is missing or not long enough' unless encrypted_payload_with_iv.length >= BLOCK_LENGTH*2 iv = encrypted_payload_with_iv.slice(0...BLOCK_LENGTH) encrypted_payload = encrypted_payload_with_iv.slice(BLOCK_LENGTH..-1) decipher = openssl_cipher decipher.decrypt decipher.key = key decipher.iv = iv decipher.update(encrypted_payload) << decipher.final end |
#encrypt(payload, encrypt_options = {}) ⇒ String
Encrypt payload using configured Cipher
76 77 78 79 80 81 82 83 84 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 76 def encrypt(payload, = {}) cipher = openssl_cipher cipher.encrypt cipher.key = key iv = [:iv] || fixed_iv || cipher.random_iv cipher.iv = iv iv << cipher.update(payload) << cipher.final end |
#random_iv ⇒ String
Generate a random IV
108 109 110 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 108 def random_iv openssl_cipher.random_iv end |