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

Commit

Permalink
Simply gem to obfuscate
Browse files Browse the repository at this point in the history
  • Loading branch information
mguymon committed Feb 1, 2013
1 parent 9dab9ba commit 471fa70
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ test/tmp
test/version_tmp
tmp

#idea files
*.iml
.idea

# YARD artifacts
.yardoc
_yardoc
Expand Down
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'http://rubygems.org'

gemspec
70 changes: 70 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
PATH
remote: .
specs:
obfuscate (0.0.1)
crypt19 (~> 1.2.0)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.11)
activesupport (= 3.2.11)
builder (~> 3.0.0)
activerecord (3.2.11)
activemodel (= 3.2.11)
activesupport (= 3.2.11)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.11)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
coderay (1.0.8)
crypt19 (1.2.1)
diff-lcs (1.1.3)
ffi (1.3.1)
guard (1.6.2)
listen (>= 0.6.0)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
terminal-table (>= 1.4.3)
thor (>= 0.14.6)
guard-rspec (2.4.0)
guard (>= 1.1)
rspec (~> 2.11)
i18n (0.6.1)
listen (0.7.2)
lumberjack (1.0.2)
method_source (0.8.1)
multi_json (1.5.0)
pry (0.9.11.4)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rb-inotify (0.8.8)
ffi (>= 0.5.0)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)
slop (3.4.3)
sqlite3 (1.3.7)
terminal-table (1.4.5)
thor (0.17.0)
tzinfo (0.3.35)

PLATFORMS
ruby

DEPENDENCIES
activerecord (> 3.0.0)
guard-rspec (~> 2.4.0)
obfuscate!
rb-inotify (~> 0.8.8)
rspec (~> 2.12.0)
sqlite3 (~> 1.3.0)
9 changes: 9 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

12 changes: 12 additions & 0 deletions lib/obfuscate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

require 'obfuscate/version'

module Obfuscate

class << self
attr_accessor :salt
attr_accessor :options
end

self.options = {}
end
53 changes: 53 additions & 0 deletions lib/obfuscate/crypt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'obfuscate'
require 'crypt/blowfish'
require "base64"

class Obfuscate::Crypt
def initialize( salt, opts = {} )
@opts = { :remove_trailing_equal => true, :encode => true, :mode => :string }.merge( opts )
@crypt = Crypt::Blowfish.new( salt )

@mode = @opts[:mode].to_sym
end

def obfuscate( text, mode = nil )

mode = mode || @mode

obfuscated = nil
if mode == :string
obfuscated = @crypt.encrypt_string(text)
elsif mode == :block
obfuscated = @crypt.encrypt_block(text.to_s.ljust(8))
else
raise "Unsupport Mode"
end

if @opts[:encode]
obfuscated = URI.escape(Base64.strict_encode64(obfuscated).strip)
obfuscated = obfuscated.chomp("=") if mode == :block && @opts[:remove_trailing_equal]
end

obfuscated
end

def clarify( text, mode = nil )

mode = mode || @mode
obfuscated = text.to_s


if @opts[:encode]
obfuscated << "=" if mode == :block && @opts[:remove_trailing_equal]
obfuscated = Base64.strict_decode64( URI.unescape(obfuscated) )
end

if mode == :string
@crypt.decrypt_string( obfuscated )
elsif mode == :block
@crypt.decrypt_block( obfuscated ).strip
else
raise "Unsupport Mode"
end
end
end
45 changes: 45 additions & 0 deletions lib/obfuscate/obfuscatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'obfuscate/crypt'

module Obfuscate
module Obfuscatable
extend ActiveSupport::Concern

included do
end

module ClassMethods
def obfuscatable(options = {})
opts = Obfuscate.options.merge(options)

cattr_accessor :obfuscatable_salt
self.obfuscatable_salt = (opts.delete(:salt) || Obfuscate.salt ).to_s

cattr_accessor :obfuscator
self.obfuscator = Obfuscate::Crypt.new( self.obfuscatable_salt, opts )
end

def find_by_obfuscated_id( text )
find_by_id( clarify_id( text ) )
end

def clarify_id( text )
self.obfuscator.clarify( text, :block )
end

def clarify( text, mode = :string)
self.obfuscator.clarify(text, mode)
end

def obfuscate( text, mode = :string)
self.obfuscator.obfuscate(text, mode)
end
end

def obfuscate_id
self.obfuscator.obfuscate( self.id, :block )
end

end
end

ActiveRecord::Base.send :include, Obfuscate::Obfuscatable
3 changes: 3 additions & 0 deletions lib/obfuscate/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Obfuscate
VERSION = "0.0.1"
end
27 changes: 27 additions & 0 deletions obfuscate.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "obfuscate/version"

Gem::Specification.new do |s|
s.name = %q{obfuscate}
s.license = "MIT"
s.version = Obfuscate::VERSION
s.platform = Gem::Platform::RUBY
s.homepage = %q{https://github.com/mguymon/obfuscate}
s.authors = ["Michael Guymon"]
s.email = ["[email protected]"]
s.description = %q{Obfuscate}
s.summary = %q{Obfuscate}

s.files = `git ls-files`.split("\n").sort
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_runtime_dependency(%q<crypt19>, ["~> 1.2.0"])
s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
s.add_development_dependency(%q<guard-rspec>, ["~> 2.4.0"])
s.add_development_dependency(%q<rb-inotify>, ["~> 0.8.8"])
s.add_development_dependency(%q<activerecord>, ["> 3.0.0"])
s.add_development_dependency(%q<sqlite3>, ["~> 1.3.0"])
end
75 changes: 75 additions & 0 deletions spec/lib/obfuscate/crypt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'spec_helper'
require 'obfuscate/crypt'
require 'crypt/blowfish'
require "base64"

describe Obfuscate::Crypt do

describe "obfuscate block mode" do
it "should obfuscate" do
crypt = Obfuscate::Crypt.new( "salt-salt", :mode => :block, :mode => :block)
crypt.obfuscate("test123").should eql "3NINgAbOhPc"
end

it "should obfuscate including the equality" do
crypt = Obfuscate::Crypt.new( "salt-salt", :mode => :block, :remove_trailing_equal => false)
crypt.obfuscate("test123").should eql "3NINgAbOhPc="
end

it "should obfuscate without encoding" do
crypt = Obfuscate::Crypt.new( "salt-salt", :mode => :block, :encode => false)
crypt.obfuscate("test123").should eql "\xDC\xD2\r\x80\x06\xCE\x84\xF7"
end

it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( "salt-salt", :mode => :string )
crypt.obfuscate("test123", :block).should eql "3NINgAbOhPc"
end
end

describe "obfuscate string mode" do
it "should obfuscate" do
crypt = Obfuscate::Crypt.new( "salt-salt-salt")
#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( "salt-salt-salt")
crypt.clarify( "j65H1jCTYh2uw/lsHweLXMw50aaENXYI" ).should eql "test12345678"
crypt.clarify( "4bxm/ijHwq1ekPBYEGFsr+LuJ8EZVa57" ).should eql "test12345678"
crypt.clarify( "y2D0gT8x3llEToou6PPbKRagVYX1NeVc" ).should eql "test12345678"
crypt.clarify( "T/graYvZiWE585V9fkJ5HAj1wyOothyo" ).should eql "test12345678"
end


it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( "salt-salt", :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

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

it "should be able to override mode" do
crypt = Obfuscate::Crypt.new( "salt-salt", :mode => :string )
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( "salt-salt", :mode => :block)
enc = crypt.obfuscate("1234567890")
crypt.clarify(enc).should eql "12345678"
end
end
end
end
38 changes: 38 additions & 0 deletions spec/lib/obfuscate/ofuscatable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'obfuscate/obfuscatable'
require 'message'

describe Obfuscate::Obfuscatable do

it "should have salt" do
Message.obfuscatable_salt.should eql "default salt"
end

it "should have obfuscator" do
Message.obfuscator.should_not be_nil
end

it "should obfuscate text" do
obfuscated = Message.obfuscate( "blah blah blah blah blah blah blah" )
obfuscated.clarify( obfuscate ).should eql "blah blah blah blah blah blah blah"
end

describe "instance" do

let(:model) do
model = Message.new
model.text = "Test"
model.save

model
end

it "should obfuscate_id" do
model.obfuscate_id.should_not be nil
end

it "should find_by_obfuscated_id" do
Message.find_by_obfuscated_id( model.obfuscate_id ).should eql model
end
end
end
7 changes: 7 additions & 0 deletions spec/lib/obfuscate/version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe Obfuscate do
it "should have VERSION" do
Obfuscate::VERSION.should_not be_nil
end
end
12 changes: 12 additions & 0 deletions spec/lib/obfuscate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

require 'obfuscate'

describe Obfuscate do

it "should set salt" do
Obfuscate.salt = "test"
Obfuscate.salt.should eql "test"
end

end
7 changes: 7 additions & 0 deletions spec/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'obfuscate/obfuscatable'

Obfuscate.salt = "default salt"

class Message < ActiveRecord::Base
obfuscatable
end
Loading

0 comments on commit 471fa70

Please sign in to comment.