1#-- 2# 3# $RCSfile$ 4# 5# = Ruby-space predefined Cipher subclasses 6# 7# = Info 8# 'OpenSSL for Ruby 2' project 9# Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz> 10# All rights reserved. 11# 12# = Licence 13# This program is licenced under the same licence as Ruby. 14# (See the file 'LICENCE'.) 15# 16# = Version 17# $Id: cipher.rb 36895 2012-09-04 00:57:31Z nobu $ 18# 19#++ 20 21module OpenSSL 22 class Cipher 23 %w(AES CAST5 BF DES IDEA RC2 RC4 RC5).each{|name| 24 klass = Class.new(Cipher){ 25 define_method(:initialize){|*args| 26 cipher_name = args.inject(name){|n, arg| "#{n}-#{arg}" } 27 super(cipher_name) 28 } 29 } 30 const_set(name, klass) 31 } 32 33 %w(128 192 256).each{|keylen| 34 klass = Class.new(Cipher){ 35 define_method(:initialize){|mode| 36 mode ||= "CBC" 37 cipher_name = "AES-#{keylen}-#{mode}" 38 super(cipher_name) 39 } 40 } 41 const_set("AES#{keylen}", klass) 42 } 43 44 # Generate, set, and return a random key. 45 # You must call cipher.encrypt or cipher.decrypt before calling this method. 46 def random_key 47 str = OpenSSL::Random.random_bytes(self.key_len) 48 self.key = str 49 return str 50 end 51 52 # Generate, set, and return a random iv. 53 # You must call cipher.encrypt or cipher.decrypt before calling this method. 54 def random_iv 55 str = OpenSSL::Random.random_bytes(self.iv_len) 56 self.iv = str 57 return str 58 end 59 60 # This class is only provided for backwards compatibility. Use OpenSSL::Cipher in the future. 61 class Cipher < Cipher 62 # add warning 63 end 64 end # Cipher 65end # OpenSSL 66