Skip to content

Commit

Permalink
remove trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 7, 2017
1 parent 196ce64 commit e36e8d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion encrypted_strings.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- test/*`.split("\n")
s.rdoc_options = %w(--line-numbers --inline-source --title encrypted_strings --main README.rdoc)
s.extra_rdoc_files = %w(README.rdoc CHANGELOG.rdoc LICENSE)

s.add_development_dependency("maxitest")
s.add_development_dependency("rake")
end
52 changes: 26 additions & 26 deletions lib/encrypted_strings/symmetric_cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,95 @@ module EncryptedStrings
# Indicates no password was specified for the symmetric cipher
class NoPasswordError < StandardError
end

# Symmetric encryption uses a specific algorithm and password to encrypt
# the string. As long as the algorithm and password are known, the string
# can be decrypted.
#
#
# Source: http://support.microsoft.com/kb/246071
#
# == Encrypting
#
#
# == Encrypting
#
# To encrypt a string using a symmetric cipher, the algorithm and password
# must be specified. You can define the defaults for these values like so:
#
#
# EncryptedStrings::SymmetricCipher.default_algorithm = 'des-ecb'
# EncryptedStrings::SymmetricCipher.default_password = 'secret'
#
#
# If these configuration options are not passed in to #encrypt, then the
# default values will be used. You can override the default values like so:
#
#
# password = 'shhhh'
# password.encrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "S/sEkViX3v4=\n"
#
#
# An exception will be raised if no password is specified.
#
#
# == Decrypting
#
#
# To decrypt a string using an symmetric cipher, the algorithm and password
# must be specified. Defaults for these values can be defined as show above.
#
#
# If these configuration options are not passed in to #decrypt, then the
# default values will be used. You can override the default values like so:
#
#
# password = "S/sEkViX3v4=\n"
# password.decrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "shhhh"
#
#
# An exception will be raised if no password is specified.
class SymmetricCipher < Cipher
class << self
# The default algorithm to use for encryption. Default is DES-EDE3-CBC.
attr_accessor :default_algorithm

# The default password to use for generating the key and initialization
# vector. Default is nil.
attr_accessor :default_password
end

# Set default values
@default_algorithm = 'DES-EDE3-CBC'

# The algorithm to use for encryption/decryption
attr_accessor :algorithm

# The password that generates the key/initialization vector for the
# algorithm
attr_accessor :password

# Creates a new cipher that uses a symmetric encryption strategy.
#
#
# Configuration options:
# * <tt>:algorithm</tt> - The algorithm to use for generating the encrypted string
# * <tt>:password</tt> - The secret value to use for generating the
# key/initialization vector for the algorithm
def initialize(options = {})
invalid_options = options.keys - [:algorithm, :password]
raise ArgumentError, "Unknown key(s): #{invalid_options.join(", ")}" unless invalid_options.empty?

options = {
:algorithm => SymmetricCipher.default_algorithm,
:password => SymmetricCipher.default_password
}.merge(options)

self.algorithm = options[:algorithm]
self.password = options[:password]
raise NoPasswordError if password.nil?

super()
end

# Decrypts the current string using the current key and algorithm specified
def decrypt(data)
cipher = build_cipher(:decrypt)
cipher.update(data.unpack('m')[0]) + cipher.final
end

# Encrypts the current string using the current key and algorithm specified
def encrypt(data)
cipher = build_cipher(:encrypt)
[cipher.update(data) + cipher.final].pack('m')
end

private
def build_cipher(type) #:nodoc:
cipher = OpenSSL::Cipher::Cipher.new(algorithm).send(type)
Expand Down
30 changes: 15 additions & 15 deletions test/symmetric_cipher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ class SymmetricCipherByDefaultTest < Minitest::Test
def setup
@symmetric_cipher = EncryptedStrings::SymmetricCipher.new(:password => 'secret')
end

def test_should_use_default_algorithm
assert_equal 'DES-EDE3-CBC', @symmetric_cipher.algorithm
end

def test_should_raise_exception
assert_raises(EncryptedStrings::NoPasswordError) {EncryptedStrings::SymmetricCipher.new}
end

def test_should_encrypt_using_default_configuration
assert_equal "oTxJd67ElLY=\n", @symmetric_cipher.encrypt('test')
end

def test_should_decrypt_encrypted_string_using_default_configuration
assert_equal 'test', @symmetric_cipher.decrypt("oTxJd67ElLY=\n")
end
Expand All @@ -32,28 +32,28 @@ class SymmetricCipherWithCustomDefaultsTest < Minitest::Test
def setup
@original_default_algorithm = EncryptedStrings::SymmetricCipher.default_algorithm
@original_default_password = EncryptedStrings::SymmetricCipher.default_password

EncryptedStrings::SymmetricCipher.default_algorithm = 'DES-EDE3-CFB'
EncryptedStrings::SymmetricCipher.default_password = 'secret'
@symmetric_cipher = EncryptedStrings::SymmetricCipher.new
end

def test_should_use_custom_default_algorithm
assert_equal 'DES-EDE3-CFB', @symmetric_cipher.algorithm
end

def test_should_use_custom_default_password
assert_equal 'secret', @symmetric_cipher.password
end

def test_should_encrypt_using_custom_default_configuration
assert_equal "QWz/eQ==\n", @symmetric_cipher.encrypt('test')
end

def test_should_decrypt_encrypted_string_using_custom_default_configuration
assert_equal 'test', @symmetric_cipher.decrypt("QWz/eQ==\n")
end

def teardown
EncryptedStrings::SymmetricCipher.default_algorithm = @original_default_algorithm
EncryptedStrings::SymmetricCipher.default_password = @original_default_password
Expand All @@ -70,7 +70,7 @@ class SymmetricCipherTest < Minitest::Test
def setup
@symmetric_cipher = EncryptedStrings::SymmetricCipher.new(:password => 'secret')
end

def test_should_be_able_to_decrypt
assert @symmetric_cipher.can_decrypt?
end
Expand All @@ -80,19 +80,19 @@ class SymmetricCipherWithCustomOptionsTest < Minitest::Test
def setup
@symmetric_cipher = EncryptedStrings::SymmetricCipher.new(:algorithm => 'DES-EDE3-CFB', :password => 'secret')
end

def test_should_use_custom_algorithm
assert_equal 'DES-EDE3-CFB', @symmetric_cipher.algorithm
end

def test_should_use_custom_password
assert_equal 'secret', @symmetric_cipher.password
end

def test_should_encrypt_using_custom_options
assert_equal "QWz/eQ==\n", @symmetric_cipher.encrypt('test')
end

def test_should_decrypt_using_custom_options
assert_equal 'test', @symmetric_cipher.decrypt("QWz/eQ==\n")
end
Expand Down

0 comments on commit e36e8d0

Please sign in to comment.