lib/submodules/ably-ruby/lib/ably/util/crypto.rb in ably-rest-0.8.3 vs lib/submodules/ably-ruby/lib/ably/util/crypto.rb in ably-rest-0.8.5
- old
+ new
@@ -35,9 +35,24 @@
def initialize(options)
raise ArgumentError, ':key is required' unless options.has_key?(:key)
@options = DEFAULTS.merge(options).freeze
end
+ # Obtain a default CipherParams. This uses default algorithm, mode and
+ # padding and key length. A key and IV are generated using the default
+ # system SecureRandom; the key may be obtained from the returned CipherParams
+ # for out-of-band distribution to other clients.
+ #
+ # @return [Hash] CipherParam options Hash with attributes :key, :algorithn, :mode, :key_length
+ #
+ def self.get_default_params(key = nil)
+ params = DEFAULTS.merge(key: key)
+ params[:key_length] = key.unpack('b*').first.length if params[:key]
+ cipher_type = "#{params[:algorithm]}-#{params[:key_length]}-#{params[:mode]}"
+ params[:key] = OpenSSL::Cipher.new(cipher_type.upcase).random_key unless params[:key]
+ params
+ end
+
# Encrypt payload using configured Cipher
#
# @param [String] payload the payload to be encrypted
# @param [Hash] encrypt_options an options Hash to configure the encrypt action
# @option encrypt_options [String] :iv optionally use the provided Initialization Vector instead of a randomly generated IV