@@ -2,95 +2,95 @@ module EncryptedStrings
22 # Indicates no password was specified for the symmetric cipher
33 class NoPasswordError < StandardError
44 end
5-
5+
66 # Symmetric encryption uses a specific algorithm and password to encrypt
77 # the string. As long as the algorithm and password are known, the string
88 # can be decrypted.
9- #
9+ #
1010 # Source: http://support.microsoft.com/kb/246071
11- #
12- # == Encrypting
13- #
11+ #
12+ # == Encrypting
13+ #
1414 # To encrypt a string using a symmetric cipher, the algorithm and password
1515 # must be specified. You can define the defaults for these values like so:
16- #
16+ #
1717 # EncryptedStrings::SymmetricCipher.default_algorithm = 'des-ecb'
1818 # EncryptedStrings::SymmetricCipher.default_password = 'secret'
19- #
19+ #
2020 # If these configuration options are not passed in to #encrypt, then the
2121 # default values will be used. You can override the default values like so:
22- #
22+ #
2323 # password = 'shhhh'
2424 # password.encrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "S/sEkViX3v4=\n"
25- #
25+ #
2626 # An exception will be raised if no password is specified.
27- #
27+ #
2828 # == Decrypting
29- #
29+ #
3030 # To decrypt a string using an symmetric cipher, the algorithm and password
3131 # must be specified. Defaults for these values can be defined as show above.
32- #
32+ #
3333 # If these configuration options are not passed in to #decrypt, then the
3434 # default values will be used. You can override the default values like so:
35- #
35+ #
3636 # password = "S/sEkViX3v4=\n"
3737 # password.decrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "shhhh"
38- #
38+ #
3939 # An exception will be raised if no password is specified.
4040 class SymmetricCipher < Cipher
4141 class << self
4242 # The default algorithm to use for encryption. Default is DES-EDE3-CBC.
4343 attr_accessor :default_algorithm
44-
44+
4545 # The default password to use for generating the key and initialization
4646 # vector. Default is nil.
4747 attr_accessor :default_password
4848 end
49-
49+
5050 # Set default values
5151 @default_algorithm = 'DES-EDE3-CBC'
52-
52+
5353 # The algorithm to use for encryption/decryption
5454 attr_accessor :algorithm
55-
55+
5656 # The password that generates the key/initialization vector for the
5757 # algorithm
5858 attr_accessor :password
59-
59+
6060 # Creates a new cipher that uses a symmetric encryption strategy.
61- #
61+ #
6262 # Configuration options:
6363 # * <tt>:algorithm</tt> - The algorithm to use for generating the encrypted string
6464 # * <tt>:password</tt> - The secret value to use for generating the
6565 # key/initialization vector for the algorithm
6666 def initialize ( options = { } )
6767 invalid_options = options . keys - [ :algorithm , :password ]
6868 raise ArgumentError , "Unknown key(s): #{ invalid_options . join ( ", " ) } " unless invalid_options . empty?
69-
69+
7070 options = {
7171 :algorithm => SymmetricCipher . default_algorithm ,
7272 :password => SymmetricCipher . default_password
7373 } . merge ( options )
74-
74+
7575 self . algorithm = options [ :algorithm ]
7676 self . password = options [ :password ]
7777 raise NoPasswordError if password . nil?
78-
78+
7979 super ( )
8080 end
81-
81+
8282 # Decrypts the current string using the current key and algorithm specified
8383 def decrypt ( data )
8484 cipher = build_cipher ( :decrypt )
8585 cipher . update ( data . unpack ( 'm' ) [ 0 ] ) + cipher . final
8686 end
87-
87+
8888 # Encrypts the current string using the current key and algorithm specified
8989 def encrypt ( data )
9090 cipher = build_cipher ( :encrypt )
9191 [ cipher . update ( data ) + cipher . final ] . pack ( 'm' )
9292 end
93-
93+
9494 private
9595 def build_cipher ( type ) #:nodoc:
9696 cipher = OpenSSL ::Cipher ::Cipher . new ( algorithm ) . send ( type )
0 commit comments