Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Fix crypt_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
mguymon committed Dec 12, 2018
1 parent 418bb0f commit 288c2c2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
6 changes: 2 additions & 4 deletions lib/obfuscate/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize
def mode=(mode)
@mode = mode.to_sym
end

def salt=(salt)
if salt.length == 0 || salt.length > 56
raise "Obfuscate salt length must be between 1-56"
Expand Down Expand Up @@ -55,14 +55,12 @@ def remove_trailing_equal?
def apply(options = {}, &blk)
config = self.class.new

changes = self.to_hash.merge( options )

changes = self.to_hash.merge(options)
config.salt = changes[:salt] unless changes[:salt].nil?
config.mode = changes[:mode] unless changes[:mode].nil?
config.encode = changes[:encode] unless changes[:encode].nil?
config.remove_trailing_equal = changes[:remove_trailing_equal] unless changes[:remove_trailing_equal].nil?


yield(config) if blk

config
Expand Down
9 changes: 4 additions & 5 deletions lib/obfuscate/crypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
require 'base64'

class Obfuscate::Crypt

attr_reader :config
attr_reader :exec_config
attr_reader :exec_config

# New instance of Obfuscate::Crypt
#
# @param [Obfuscate::Config]
def initialize( config )
def initialize(config)
@crypt = Crypt::Blowfish.new( config.salt )
@config = config
end
Expand All @@ -34,9 +33,9 @@ def initialize( config )
#
# @param [Symbol] override_mode to explicit set obfuscate mode to :string or :block
# @return [String]
def obfuscate( text, override_mode = nil )
def obfuscate(text, override_mode = nil)

@exec_config = @config.apply( :mode => (override_mode || @config.mode) )
@exec_config = @config.apply(:mode => (override_mode || @config.mode) )

obfuscated = nil
if @exec_config.mode == :string
Expand Down
38 changes: 18 additions & 20 deletions spec/lib/obfuscate/crypt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,50 @@
describe Obfuscate::Crypt do

describe "obfuscate block mode" do
before(:each) do
@config = Obfuscate::Config.new.tap do |config|
let(:config) do
Obfuscate::Config.new.tap do |config|
config.salt = "salt-salt-salt"
config.mode = :block
end
end

it "should obfuscate" do
crypt = Obfuscate::Crypt.new( @config )
crypt = Obfuscate::Crypt.new(config)
crypt.obfuscate("test123").should eql "R35tG3YxSQk"
end

it "should obfuscate including the equality" do
crypt = Obfuscate::Crypt.new( @config.apply(:remove_trailing_equal => false) )
crypt = Obfuscate::Crypt.new(config.apply(:remove_trailing_equal => false))
crypt.obfuscate("test123").should eql "R35tG3YxSQk="
end

it "should obfuscate without encoding" do
@config.encode.should be_true
crypt = Obfuscate::Crypt.new( @config.apply(:encode => false ) )
@config.encode.should be_true
crypt.obfuscate("without encoding").should eql "\x8F\xC7\xE4\n,2\xA2\xA7"
crypt = Obfuscate::Crypt.new(config.apply(encode: false))
crypt.obfuscate("without encoding").unpack('H*').first.should eql "8fc7e40a2c32a2a7"
end

it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( @config.apply( :mode => :string ) )
crypt = Obfuscate::Crypt.new(config.apply( :mode => :string ))
crypt.obfuscate("test123", :block).should eql "R35tG3YxSQk"
end
end

describe "obfuscate string mode" do
before(:each) do
@config = Obfuscate::Config.new.tap do |config|
let(:config) do
Obfuscate::Config.new.tap do |config|
config.salt = "salt-salt-salt"
config.mode = :string
end
end

it "should obfuscate" do
crypt = Obfuscate::Crypt.new( @config )
crypt = Obfuscate::Crypt.new(config)
#crypt.obfuscate("test12345678").should eql ""
crypt.clarify( crypt.obfuscate("test12345678") ).should eql "test12345678"
end

it "should clarify previously obfuscated text" do
crypt = Obfuscate::Crypt.new( @config )
crypt = Obfuscate::Crypt.new(config)
crypt.clarify( "j65H1jCTYh2uw/lsHweLXMw50aaENXYI" ).should eql "test12345678"
crypt.clarify( "4bxm/ijHwq1ekPBYEGFsr+LuJ8EZVa57" ).should eql "test12345678"
crypt.clarify( "y2D0gT8x3llEToou6PPbKRagVYX1NeVc" ).should eql "test12345678"
Expand All @@ -75,35 +73,35 @@


it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( @config.apply( :mode => :block ) )
crypt = Obfuscate::Crypt.new(config.apply( :mode => :block ))

# such a weak spec, but the encrypted string will not be the same every time
crypt.obfuscate("test123", :string).should_not eql "3NINgAbOhPc"
end
end

describe "clarify" do
before(:each) do
@config = Obfuscate.setup do |config|
describe "#clarify" do
let(:config) do
Obfuscate.setup do |config|
config.salt = "salt-salt-salt"
config.mode = :string
end
end

it "should clarify" do
crypt = Obfuscate::Crypt.new( @config )
crypt = Obfuscate::Crypt.new(config)
enc = crypt.obfuscate("test123456test123456test123456test123456")
crypt.clarify(enc).should eql "test123456test123456test123456test123456"
end

it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( @config )
crypt = Obfuscate::Crypt.new(config)
crypt.clarify( crypt.obfuscate("test123", :block), :block ).should eql "test123"
end

describe "block mode" do
it "will trim text larger than 8 characters" do
crypt = Obfuscate::Crypt.new( @config.apply( :mode => :block ) )
crypt = Obfuscate::Crypt.new(config.apply(mode: :block))
enc = crypt.obfuscate("1234567890")
crypt.exec_config.mode.should eql :block

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

require 'rubygems'
require 'active_record'
require 'pry'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
Expand Down

0 comments on commit 288c2c2

Please sign in to comment.