obfuscate 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Obfucate Changelog
2
2
 
3
+ ### 0.0.7 (May 16, 2013)
4
+
5
+ * Add :append_salt option to obfuscateable
6
+
3
7
  ### 0.0.6 (May 11, 2013)
4
8
 
5
- * Implement a new method called find_obfuscated [thanks minostro]
9
+ * Implement a new method called find_obfuscated (<a href="https://github.com/mguymon/obfuscate/pull/2">Pull #2</a>) [thanks <a href="https://github.com/minostro">minostro</a>]
data/README.md CHANGED
@@ -3,12 +3,13 @@
3
3
  A simple way to obfuscate ids and text. Useful when you have to make ids visible
4
4
  to users. Integrates directly with Rails 3.
5
5
 
6
- The goal is to make simple obfuscated ids that are not huge. This is achieved by using
7
- Blowfish and encrypting a single block. This produces a nice id of 11 characters (11
8
- since the trailing _=_ is removed by default), for example `3NINgAbOhPc`. The caveat is,
9
- the id must be within _99,999,999_, e.g. a max length of _8_.
6
+ The goal is to make simple obfuscated ids that are not huge. This is achieved by using an
7
+ <a href="https://github.com/otherinbox/crypt">implementation</a> of <a href="http://www.schneier.com/blowfish.html">Blowfish</a>
8
+ and encrypting a single block. This produces a nice id of 11 characters (11 since the
9
+ trailing _=_ is removed by default), for example `3NINgAbOhPc`. The caveat is, the id
10
+ must be within _99,999,999_, e.g. a max length of _8_.
10
11
 
11
- Text can be obfuscated using Blowfishes string encryption as well, but than it produces
12
+ Text can be obfuscated using Blowfish's string encryption as well, but than it produces
12
13
  output that is larger than the elegant 11 character from single block encryption.
13
14
 
14
15
  https://github.com/mguymon/obfuscate
@@ -24,8 +25,8 @@ https://github.com/mguymon/obfuscate
24
25
  A simple example
25
26
 
26
27
  Obfuscate.setup do |config|
27
- config.salt = "A weak salt ..."
28
- config.mode = :string # defaults to :string
28
+ config.salt = "A weak salt ..." # Length must be between 1-56
29
+ config.mode = :string # defaults to :string
29
30
  end
30
31
 
31
32
  obfuscated = Obfuscate.obfuscate( "test" ) # "HoDruKtafqyLxZxu9s-kYQ=="
@@ -37,10 +38,10 @@ Create an initializer in `config/initializers` with:
37
38
 
38
39
  require 'obfuscate/obfuscatable'
39
40
  Obfuscate.setup do |config|
40
- config.salt = "A weak salt ..."
41
+ config.salt = "A weak salt ..." # Length must be between 1-56
41
42
  end
42
43
 
43
- Now add to models that you want to be `Obfuscatable`:
44
+ Now add to models that you want to be `Obfuscatable`, with <a href="http://rubydoc.info/gems/obfuscate/Obfuscate/Obfuscatable/ClassMethods">possible config options</a>:
44
45
 
45
46
  class Message < ActiveRecord::Base
46
47
  obfuscatable # a hash of config overrides can be passed.
@@ -51,8 +52,9 @@ To get the 11 character `obfuscated_id`, which uses `mode :block` for the Blowfi
51
52
  message = Message.find(1)
52
53
  obfuscated = message.obfuscated_id # "NuwhZTtHnko"
53
54
  clarified = message.clarify_id( obfuscated ) # "1"
54
-
55
- Message.find_by_obfuscated_id( obfuscated )
55
+
56
+ Message.find_obfuscated( obfuscated ) # raises an ActiveRecord::RecordNotFound if the found record does not exist
57
+ Message.find_by_obfuscated_id( obfuscated ) # returns nil if the found record does not exist
56
58
 
57
59
  Or `obfuscate` a block of text, defaults to mode :string which uses Blowfish string encryption, allowing longer
58
60
  blocks of text to be obfuscated.
data/lib/obfuscate.rb CHANGED
@@ -13,7 +13,6 @@
13
13
  # License for the specific language governing permissions and limitations under
14
14
  # the License.
15
15
 
16
-
17
16
  require 'obfuscate/version'
18
17
  require 'obfuscate/crypt'
19
18
  require 'obfuscate/config'
@@ -27,6 +27,14 @@ class Obfuscate::Config
27
27
  def mode=(mode)
28
28
  @mode = mode.to_sym
29
29
  end
30
+
31
+ def salt=(salt)
32
+ if salt.length == 0 || salt.length > 56
33
+ raise "Obfuscate salt length must be between 1-56"
34
+ else
35
+ @salt = salt
36
+ end
37
+ end
30
38
 
31
39
  # Check if mode is :block and remove_trailing_equal is true
32
40
  # @return [Boolean]
@@ -34,7 +42,8 @@ class Obfuscate::Config
34
42
  self.mode == :block && self.remove_trailing_equal == true
35
43
  end
36
44
 
37
- # Creates a new instance of Config with applied changes
45
+ # Creates a new instance of Config with applied changes. Does not change
46
+ # the original Config.
38
47
  #
39
48
  # @param [Hash] options of configurations
40
49
  # @option options [Symbol] :salt A Model specific salt
@@ -25,11 +25,19 @@ module Obfuscate
25
25
  # Cavaet: Only supports id lengths up to 8 (e.g. 99,999,999) due to use of Blowfish block encryption.
26
26
  #
27
27
  # @params [Hash] options to override the default config
28
- # @option options [Symbol] :salt A Model specific salt
28
+ # @option options [Symbol] :salt A Model specific salt, length must be between 1-56
29
+ # @option options [Symbol] :append_salt Append string to default salt and use for this Model. Overwrites the salt option.
29
30
  # @option options [Symbol] :encode Enable Base64 and URL encoding for this Model. Enabled by default.
30
31
  # @option options [Symbol] :remove_trailing_equal When in :block mode, removes the trailing = from the
31
32
  # obfuscated text.
32
33
  def obfuscatable(options = {})
34
+ # :append_salt will append the string to the default salt
35
+ append_salt = options.with_indifferent_access.delete(:append_salt)
36
+ if append_salt
37
+ options[:salt] = "#{Obfuscate.config.salt}#{append_salt}"
38
+ end
39
+
40
+
33
41
  config = Obfuscate.config.apply(options)
34
42
 
35
43
  cattr_accessor :obfuscatable_config
@@ -14,5 +14,5 @@
14
14
  # the License.
15
15
 
16
16
  module Obfuscate
17
- VERSION = "0.0.6"
17
+ VERSION = "0.0.7"
18
18
  end
@@ -14,10 +14,23 @@
14
14
  # the License.
15
15
 
16
16
  require 'spec_helper'
17
+ require 'obfuscate'
17
18
  require 'obfuscate/config'
18
19
 
19
20
  describe Obfuscate::Config do
20
21
 
22
+ it "should raise error if salt is to large" do
23
+ expect do
24
+ Obfuscate::Config.new.salt = "a79094729c586621afe294eab90ce4c21749d54be80ef08425d372a281b8"
25
+ end.to raise_error(RuntimeError)
26
+ end
27
+
28
+ it "should raise error if salt is empty" do
29
+ expect do
30
+ Obfuscate::Config.new.salt = ""
31
+ end.to raise_error(RuntimeError)
32
+ end
33
+
21
34
  it "should have default mode" do
22
35
  Obfuscate::Config.new.mode.should eql :string
23
36
  end
@@ -20,17 +20,14 @@ describe Obfuscate::Obfuscatable do
20
20
 
21
21
  it "should give a hoot and not pollute" do
22
22
  ActiveRecord::Base.method_defined?( :find_by_obfuscated_id ).should be_false
23
- end
24
-
25
- it "should give a hoot and not pollute" do
26
23
  ActiveRecord::Base.method_defined?( :find_obfuscated ).should be_false
27
24
  end
28
25
 
29
- it "should have config" do
30
- Message.obfuscatable_config.salt.should eql "message salt"
26
+ it "should have appended salt" do
27
+ Message.obfuscatable_config.salt.should eql "Test Salt--Message--"
31
28
  end
32
29
 
33
- it "should have obfuscator" do
30
+ it "should have crypt" do
34
31
  Message.obfuscatable_crypt.is_a?( Obfuscate::Crypt ).should be_true
35
32
  end
36
33
 
@@ -76,6 +73,7 @@ describe Obfuscate::Obfuscatable do
76
73
  end
77
74
  end
78
75
 
76
+ Obfuscate.config.salt = "Test Salt"
79
77
  class Message < ActiveRecord::Base
80
- obfuscatable :salt => 'message salt'
78
+ obfuscatable :append_salt => "--Message--"
81
79
  end
@@ -58,4 +58,14 @@ describe Obfuscate do
58
58
  Obfuscate.clarify(obfuscated).should eql "test obfuscation"
59
59
  end
60
60
 
61
+ it "should clarify existing obfuscated values" do
62
+ Obfuscate.setup :salt => 'obfuscate-salt', :mode => :string
63
+ vals = ["7a2b9e943dc32807285f4cc5e14dfb9f", "cda78d5b2ff68c1af37ed567940eaf47", "c4a175a114b3505761ffb7647e9ab5fd", "6fb25b22c552e9e0fcaa6608418de636", "f7fcbd31a844639edf16c349ce5f51f5", "a8f5535b956fea220a8cfa1127f1746f", "fef14ce49f1027ffcae3f0139e44b3f6", "17ce584c4beb2b05630970588aa296c0", "fd81080b3f5ee8b92f97c5a4ec8bba68", "fbea19fa974d59b3d3d2e8b76ee073ba"]
64
+ obfuscated = ["1UcIzpT8OYg1DRp3U2KJhFc4G0MFlFgMQUy93DHsz2JpjHmqTGANUUt0G8tbao_a", "1UcIzpT8OYhDPRmTdlV55HIb6bqZcTYDdyaGqTLplNhTxmkEaO_5iDzjoDYt0l_O", "1UcIzpT8OYhGb9w6jFBCJsJHCyv94Dk4DNLIDRJ4A0mxpkBs_BqAPU2MgVNI5g1k", "1UcIzpT8OYheLeZhqOq4G5b4FAjmksim0z5oSuhroRq8AM-iEjiQ1-wS7Bk8layP", "1UcIzpT8OYj0bVYbRaDtV3BD56w9rBoxQK_Rqf-gPjZqh0t3mhSoxTQIjxPEsT9K", "1UcIzpT8OYhreXIwqINLguTO1fEWY23RbbaT3RRmLqTreZTIFBuOl0lFqijoDaUM", "1UcIzpT8OYisoIwsozOMRh4W7mvhWE_Mf5yUi2TO0OLn1MGxXnB5qR2B9sgIoKsk", "1UcIzpT8OYhX9QBNegug7SzXYbutpemeij-GonXITIHSGgO8yV1QkATuqegIkJZp", "1UcIzpT8OYjJkj8IJyYAm0bUsHDHt4xn3qSpPeVm_G2u9Ow9ZNkmUjHnegJOlOyZ", "yJKFWD2DdQIg7pZmVjrMZdcplGmqbU7iwmuY54X4nn-9fnGuEOg_9hjyqR7gPce8"]
65
+
66
+ obfuscated.size.times.each do |x|
67
+ Obfuscate.clarify(obfuscated[x]).should eql vals[x]
68
+ end
69
+ end
70
+
61
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obfuscate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-11 00:00:00.000000000 Z
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: otherinbox-crypt19
@@ -150,7 +150,6 @@ files:
150
150
  - spec/lib/obfuscate/ofuscatable_spec.rb
151
151
  - spec/lib/obfuscate/version_spec.rb
152
152
  - spec/lib/obfuscate_spec.rb
153
- - spec/message.rb
154
153
  - spec/spec_helper.rb
155
154
  homepage: https://github.com/mguymon/obfuscate
156
155
  licenses:
data/spec/message.rb DELETED
@@ -1,22 +0,0 @@
1
- # Licensed to the Apache Software Foundation (ASF) under one or more
2
- # contributor license agreements. See the NOTICE file distributed with this
3
- # work for additional information regarding copyright ownership. The ASF
4
- # licenses this file to you under the Apache License, Version 2.0 (the
5
- # "License"); you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
- # License for the specific language governing permissions and limitations under
14
- # the License.
15
-
16
- require 'obfuscate/obfuscatable'
17
-
18
- Obfuscate.salt = "default salt"
19
-
20
- class Message < ActiveRecord::Base
21
- obfuscatable
22
- end