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: 128, }
- BLOCK_LENGTH =
16
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Configured options for this Crypto object, see #initialize for a list of configured options.
Instance Method Summary collapse
-
#cipher_type ⇒ String
The Cipher algorithm string such as AES-128-CBC.
-
#decrypt(encrypted_payload_with_iv) ⇒ String
Decrypt payload using configured Cipher.
-
#encrypt(payload, encrypt_options = {}) ⇒ String
Encrypt payload using configured Cipher.
-
#initialize(options) ⇒ Ably::Util::Crypto
constructor
Creates a Crypto object.
-
#random_iv ⇒ String
Generate a random IV.
-
#random_key ⇒ String
Generate a random key.
Constructor Details
#initialize(options) ⇒ Ably::Util::Crypto
Creates a Ably::Util::Crypto object
35 36 37 38 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 35 def initialize() raise ArgumentError, ':key is required' unless .has_key?(:key) @options = DEFAULTS.merge().freeze end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Configured options for this Crypto object, see #initialize for a list of configured options
17 18 19 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 17 def @options end |
Instance Method Details
#cipher_type ⇒ String
The Cipher algorithm string such as AES-128-CBC
92 93 94 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 92 def cipher_type ([:combined] || "#{[:algorithm]}-#{[:key_length]}-#{[:mode]}").to_s.upcase end |
#decrypt(encrypted_payload_with_iv) ⇒ String
Decrypt payload using configured Cipher
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 64 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 48 def encrypt(payload, = {}) cipher = openssl_cipher cipher.encrypt cipher.key = key iv = [:iv] || [:iv] || cipher.random_iv cipher.iv = iv iv << cipher.update(payload) << cipher.final end |
#random_iv ⇒ String
Generate a random IV
86 87 88 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 86 def random_iv openssl_cipher.random_iv end |
#random_key ⇒ String
Generate a random key
80 81 82 |
# File 'lib/submodules/ably-ruby/lib/ably/util/crypto.rb', line 80 def random_key openssl_cipher.random_key end |