scatter_swap 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +48 -22
- data/lib/scatter_swap/version.rb +1 -1
- data/scatter_swap.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,37 +1,68 @@
|
|
1
1
|
# ScatterSwap
|
2
2
|
|
3
|
-
This is the hashing function behind
|
4
|
-
https://github.com/namick/obfuscate_id
|
3
|
+
This is the integer hashing function behind [obfuscate_id](https://github.com/namick/obfuscate_id).
|
5
4
|
|
6
|
-
Designing a hash function is a bit of a black art and
|
7
|
-
being that I don't have math background, I must resort
|
8
|
-
to this simplistic swaping and scattering of array elements.
|
5
|
+
> Designing a hash function is a bit of a black art and
|
6
|
+
> being that I don't have math background, I must resort
|
7
|
+
> to this simplistic swaping and scattering of array elements.
|
9
8
|
|
10
|
-
After writing this and reading/learning some elemental hashing techniques,
|
11
|
-
I
|
12
|
-
http://en.wikipedia.org/wiki/Perfect_hash_function#Minimal_perfect_hash_function
|
9
|
+
> After writing this and reading/learning some elemental hashing techniques,
|
10
|
+
> I realized that this library is an example of what is known as a [minimal perfect hash function](http://en.wikipedia.org/wiki/Perfect_hash_function#Minimal_perfect_hash_function).
|
13
11
|
|
14
|
-
I welcome all improvements :-)
|
12
|
+
> I welcome all improvements via pull-requests :-)
|
15
13
|
|
16
|
-
If you have some comments or suggestions, please contact me
|
17
|
-
https://github.com/namick
|
14
|
+
> If you have some comments or suggestions, please contact me at `github@nathanamick.com` - nathan amick
|
18
15
|
|
19
|
-
|
16
|
+
## Goals
|
20
17
|
|
18
|
+
We want to transform an integer into another random looking integer and then reliably tranform it back.
|
21
19
|
|
22
|
-
|
23
|
-
It zero pads smaller numbers... so the number 1 is expressed with:
|
24
|
-
0000000001
|
20
|
+
It will turn the number `3` into `2356513904`, and it can then reliably reverse that scrambled `2356513904` number back into `3`
|
25
21
|
|
26
|
-
|
27
|
-
|
22
|
+
We also want sequential integers to become non-sequential.
|
23
|
+
|
24
|
+
So for example it will turn `7001, 7002, 7003` into `5270192353, 7107163820, 3296163828`, and back again.
|
25
|
+
|
26
|
+
Please note, this is not encryption or related to security in any way. It lightly obfuscates an integer in a reversable way.
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Pass a number (as an integer or string) to the 'hash' method and it will return an obfuscated version of it.
|
31
|
+
|
32
|
+
ScatterSwap.hash(1).to_i
|
33
|
+
#=> 4517239960
|
34
|
+
|
35
|
+
Pass that obfuscated version in and it will return the original (as a zero padded string).
|
36
|
+
|
37
|
+
ScatterSwap.reverse_hash(4517239960).to_i
|
38
|
+
#=> 1
|
39
|
+
|
40
|
+
|
41
|
+
*Because this was originally built for urls like this `example.com/users/00000000001` it outputs strings. This is why the examples above have `to_i` tacked on to them. Since extracting it to its own library, that may not make sense anymore. I'm considering output the same type as it is input. Thoughts?*
|
42
|
+
|
43
|
+
## How it works
|
44
|
+
|
45
|
+
This library is built for integers that can be expressed with 10 digits.
|
46
|
+
It zero pads smaller numbers.
|
47
|
+
|
48
|
+
The number 1 is expressed with:
|
49
|
+
|
50
|
+
0000000001
|
51
|
+
|
52
|
+
The biggest number it can deal with is 10 billion - 1:
|
53
|
+
|
54
|
+
9999999999
|
28
55
|
|
29
56
|
Since we are working with a limited sequential set of input integers, 10 billion,
|
30
57
|
this algorithm will suffice for simple id obfuscation for many web apps.
|
58
|
+
|
31
59
|
The largest value that Ruby on Rails default id, Mysql INT type, is just over 2 billion (2147483647)
|
32
60
|
which is the same as 2 to the power of 31 minus 1, but considerably less than 10 billion.
|
33
61
|
|
62
|
+
## Strategies
|
63
|
+
|
34
64
|
ScatterSwap is an integer hash function designed to have:
|
65
|
+
|
35
66
|
- zero collisions ( http://en.wikipedia.org/wiki/Perfect_hash_function )
|
36
67
|
- achieve avalanche ( http://en.wikipedia.org/wiki/Avalanche_effect )
|
37
68
|
- reversable
|
@@ -50,7 +81,6 @@ to swap out each of those zeros for something else.. something different
|
|
50
81
|
for each place in the 10 digit array; for this, we need a map so that we
|
51
82
|
can reverse it.
|
52
83
|
|
53
|
-
TODO: Write a gem description
|
54
84
|
|
55
85
|
## Installation
|
56
86
|
|
@@ -66,10 +96,6 @@ Or install it yourself as:
|
|
66
96
|
|
67
97
|
$ gem install scatter_swap
|
68
98
|
|
69
|
-
## Usage
|
70
|
-
|
71
|
-
TODO: Write usage instructions here
|
72
|
-
|
73
99
|
## Contributing
|
74
100
|
|
75
101
|
1. Fork it
|
data/lib/scatter_swap/version.rb
CHANGED
data/scatter_swap.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ["github@nathanamick.com"]
|
11
11
|
gem.description = %q{ScatterSwap is an integer hash function designed to have zero collisions, achieve avalanche, and be reversible.}
|
12
12
|
gem.summary = %q{Minimal perfect hash function for 10 digit integers}
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/namick/scatter_swap"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scatter_swap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -32,7 +32,7 @@ files:
|
|
32
32
|
- scatter_swap.gemspec
|
33
33
|
- spec/scatter_swap_spec.rb
|
34
34
|
- spec/spec_helper.rb
|
35
|
-
homepage:
|
35
|
+
homepage: https://github.com/namick/scatter_swap
|
36
36
|
licenses: []
|
37
37
|
post_install_message:
|
38
38
|
rdoc_options: []
|