scatter_swap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scatter_swap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nathan and David Amick
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ScatterSwap
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'scatter_swap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install scatter_swap
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module ScatterSwap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,140 @@
1
+ require "scatter_swap/version"
2
+
3
+ class ScatterSwap
4
+ # This is the hashing function behind ObfuscateId.
5
+ # https://github.com/namick/obfuscate_id
6
+ #
7
+ # Designing a hash function is a bit of a black art and
8
+ # being that I don't have math background, I must resort
9
+ # to this simplistic swaping and scattering of array elements.
10
+ #
11
+ # After writing this and reading/learning some elemental hashing techniques,
12
+ # I realize this library is what is known as a Minimal perfect hash function:
13
+ # http://en.wikipedia.org/wiki/Perfect_hash_function#Minimal_perfect_hash_function
14
+ #
15
+ # I welcome all improvements :-)
16
+ #
17
+ # If you have some comments or suggestions, please contact me on github
18
+ # https://github.com/namick
19
+ #
20
+ # - nathan amick
21
+ #
22
+ #
23
+ # This library is built for integers that can be expressed with 10 digits:
24
+ # It zero pads smaller numbers... so the number one is expressed with:
25
+ # 0000000001
26
+ # The biggest number it can deal with is:
27
+ # 9999999999
28
+ #
29
+ # Since we are working with a limited sequential set of input integers, 10 billion,
30
+ # this algorithm will suffice for simple id obfuscation for many web apps.
31
+ # The largest value that Ruby on Rails default id, Mysql INT type, is just over 2 billion (2147483647)
32
+ # which is the same as 2 to the power of 31 minus 1, but considerably less than 10 billion.
33
+ #
34
+ # ScatterSwap is an integer hash function designed to have:
35
+ # - zero collisions ( http://en.wikipedia.org/wiki/Perfect_hash_function )
36
+ # - achieve avalanche ( http://en.wikipedia.org/wiki/Avalanche_effect )
37
+ # - reversable
38
+ #
39
+ # We do that by combining two distinct strategies.
40
+ #
41
+ # 1. Scattering - whereby we take the whole number, slice it up into 10 digits
42
+ # and rearange their places, yet retaining the same 10 digits. This allows
43
+ # us to preserve the sum of all the digits, regardless of order. This sum acts
44
+ # as a key to reverse this scattering effect.
45
+ #
46
+ # 2. Swapping - when dealing with many sequential numbers that we don't want
47
+ # to look similar, scattering wont do us much good because so many of the
48
+ # digits are the same; it deoesn't help to scatter 9 zeros around, so we need
49
+ # to swap out each of those zeros for something else.. something different
50
+ # for each place in the 10 digit array; for this, we need a map so that we
51
+ # can reverse it.
52
+
53
+ # Convience class method pointing to the instance method
54
+ def self.hash(plain_integer, spin = 0)
55
+ new(plain_integer, spin).hash
56
+ end
57
+
58
+ # Convience class method pointing to the instance method
59
+ def self.reverse_hash(hashed_integer, spin = 0)
60
+ new(hashed_integer, spin).reverse_hash
61
+ end
62
+
63
+ def initialize(original_integer, spin = 0)
64
+ @original_integer = original_integer
65
+ @spin = spin
66
+ zero_pad = original_integer.to_s.rjust(10, '0')
67
+ @working_array = zero_pad.split("").collect {|d| d.to_i}
68
+ end
69
+
70
+ attr_accessor :working_array
71
+
72
+ # obfuscates an integer up to 10 digits in length
73
+ def hash
74
+ swap
75
+ scatter
76
+ completed_string
77
+ end
78
+
79
+ # de-obfuscates an integer
80
+ def reverse_hash
81
+ unscatter
82
+ unswap
83
+ completed_string
84
+ end
85
+
86
+ def completed_string
87
+ @working_array.join
88
+ end
89
+
90
+ # We want a unique map for each place in the original number
91
+ def swapper_map(index)
92
+ array = (0..9).to_a
93
+ 10.times.collect.with_index do |i|
94
+ array.rotate!(index + i ^ spin).pop
95
+ end
96
+ end
97
+
98
+ # Using a unique map for each of the ten places,
99
+ # we swap out one number for another
100
+ def swap
101
+ @working_array = @working_array.collect.with_index do |digit, index|
102
+ swapper_map(index)[digit]
103
+ end
104
+ end
105
+
106
+ # Reverse swap
107
+ def unswap
108
+ @working_array = @working_array.collect.with_index do |digit, index|
109
+ swapper_map(index).rindex(digit)
110
+ end
111
+ end
112
+
113
+ # Rearrange the order of each digit in a reversable way by using the
114
+ # sum of the digits (which doesn't change regardless of order)
115
+ # as a key to record how they were scattered
116
+ def scatter
117
+ sum_of_digits = @working_array.inject(:+).to_i
118
+ @working_array = 10.times.collect do
119
+ @working_array.rotate!(spin ^ sum_of_digits).pop
120
+ end
121
+ end
122
+
123
+ # Reverse the scatter
124
+ def unscatter
125
+ scattered_array = @working_array
126
+ sum_of_digits = scattered_array.inject(:+).to_i
127
+ @working_array = []
128
+ @working_array.tap do |unscatter|
129
+ 10.times do
130
+ unscatter.push scattered_array.pop
131
+ unscatter.rotate! (sum_of_digits ^ spin) * -1
132
+ end
133
+ end
134
+ end
135
+
136
+ # Add some spice so that different apps can have differently mapped hashes
137
+ def spin
138
+ @spin || 0
139
+ end
140
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'scatter_swap/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "scatter_swap"
8
+ gem.version = ScatterSwap::VERSION
9
+ gem.authors = ["Nathan Amick"]
10
+ gem.email = ["github@nathanamick.com"]
11
+ gem.description = %q{ScatterSwap is an integer hash function designed to have zero collisions, achieve avalanche, and be reversible.}
12
+ gem.summary = %q{Minimal perfect hash function for 10 digit integers}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scatter_swap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Amick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ScatterSwap is an integer hash function designed to have zero collisions,
15
+ achieve avalanche, and be reversible.
16
+ email:
17
+ - github@nathanamick.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/scatter_swap.rb
28
+ - lib/scatter_swap/version.rb
29
+ - scatter_swap.gemspec
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Minimal perfect hash function for 10 digit integers
54
+ test_files: []