hashids 0.0.3 → 0.0.5
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.
- checksums.yaml +7 -0
- data/.travis.yml +1 -1
- data/Gemfile +0 -1
- data/README.md +9 -9
- data/Rakefile +10 -0
- data/hashids.gemspec +1 -1
- data/lib/hashids.rb +14 -19
- data/spec/hashids_spec.rb +5 -1
- metadata +9 -13
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4aca5b2a67715e91ddbfc9b5c68cfd794a6c986a
|
4
|
+
data.tar.gz: 23974c9688a19b0da1215bbdc9065071234cd903
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8e7f1e742554373ce8fbb3128c59ccb5e4992a95624b5115649ccd7cd9e1cd90078bdd04d611f2e0f71e8d6d0be2cd659bba453140483fa54b263250e7137243
|
7
|
+
data.tar.gz: 62132383ecd62f42c0d9ecd8a2684a746bf1fd6e8dd99f2c5e5ad06c72941359a1aec729f00f443dab42b7a5b62d5a29c23ab7e3151c3299ecdcd956fb86dbf3
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -45,7 +45,7 @@ Or install it yourself as:
|
|
45
45
|
|
46
46
|
## Usage
|
47
47
|
|
48
|
-
|
48
|
+
### Encrypting one number
|
49
49
|
|
50
50
|
You can pass a unique salt value so your hashes differ from everyone else's. I use "**this is my salt**" as an example.
|
51
51
|
|
@@ -58,7 +58,7 @@ hash = hashids.encrypt(12345)
|
|
58
58
|
|
59
59
|
ryBo
|
60
60
|
|
61
|
-
|
61
|
+
### Decrypting
|
62
62
|
|
63
63
|
Notice during decryption, same salt value is used:
|
64
64
|
|
@@ -71,7 +71,7 @@ numbers = hashids.decrypt("ryBo")
|
|
71
71
|
|
72
72
|
[ 12345 ]
|
73
73
|
|
74
|
-
|
74
|
+
### Decrypting with different salt
|
75
75
|
|
76
76
|
Decryption will not work if salt is changed:
|
77
77
|
|
@@ -84,7 +84,7 @@ numbers = hashids.decrypt("ryBo")
|
|
84
84
|
|
85
85
|
[]
|
86
86
|
|
87
|
-
|
87
|
+
### Encrypting several numbers
|
88
88
|
|
89
89
|
```ruby
|
90
90
|
hashids = Hashids.new("this is my salt")
|
@@ -95,7 +95,7 @@ hash = hashids.encrypt(683, 94108, 123, 5)
|
|
95
95
|
|
96
96
|
zBphL54nuMyu5
|
97
97
|
|
98
|
-
|
98
|
+
### Decrypting is done the same way
|
99
99
|
|
100
100
|
```ruby
|
101
101
|
hashids = Hashids.new("this is my salt")
|
@@ -106,7 +106,7 @@ numbers = hashids.decrypt("zBphL54nuMyu5")
|
|
106
106
|
|
107
107
|
[ 683, 94108, 123, 5 ]
|
108
108
|
|
109
|
-
|
109
|
+
### Encrypting and specifying minimum hash length
|
110
110
|
|
111
111
|
Here we encrypt integer 1, and set the minimum hash length to **8** (by default it's **0** -- meaning hashes will be the shortest possible length).
|
112
112
|
|
@@ -119,7 +119,7 @@ hash = hashids.encrypt(1)
|
|
119
119
|
|
120
120
|
b9iLXiAa
|
121
121
|
|
122
|
-
|
122
|
+
### Decrypting
|
123
123
|
|
124
124
|
```ruby
|
125
125
|
hashids = Hashids.new("this is my salt", 8)
|
@@ -130,7 +130,7 @@ numbers = hashids.decrypt("b9iLXiAa")
|
|
130
130
|
|
131
131
|
[ 1 ]
|
132
132
|
|
133
|
-
|
133
|
+
### Specifying custom hash alphabet
|
134
134
|
|
135
135
|
Here we set the alphabet to consist of only four letters: "abcd"
|
136
136
|
|
@@ -148,7 +148,7 @@ hash = hashids.encrypt(1, 2, 3, 4, 5)
|
|
148
148
|
The primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression.
|
149
149
|
Having said that, this algorithm does try to make these hashes unguessable and unpredictable:
|
150
150
|
|
151
|
-
|
151
|
+
### Repeating numbers
|
152
152
|
|
153
153
|
```ruby
|
154
154
|
hashids = Hashids.new("this is my salt")
|
data/Rakefile
CHANGED
@@ -6,3 +6,13 @@ task :default => :spec
|
|
6
6
|
Rake::TestTask.new(:spec) do |t|
|
7
7
|
t.test_files = FileList['spec/**/*_spec.rb']
|
8
8
|
end
|
9
|
+
|
10
|
+
task :profile do
|
11
|
+
Bundler.with_clean_env do
|
12
|
+
exec 'CPUPROFILE=/tmp/hashids_profile ' +
|
13
|
+
'RUBYOPT="-r`gem which \'perftools.rb\' | tail -1`" ' +
|
14
|
+
'ruby spec/hashids_profile.rb && ' +
|
15
|
+
'pprof.rb --gif /tmp/hashids_profile > /tmp/hashids_profile.gif && ' +
|
16
|
+
'open /tmp/hashids_profile.gif'
|
17
|
+
end
|
18
|
+
end
|
data/hashids.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.description = %q{Use hashids when you do not want to expose your database ids to the user.}
|
13
13
|
gem.homepage = "https://github.com/peterhellberg/hashids.rb"
|
14
14
|
|
15
|
-
gem.required_ruby_version = '>= 1.9.
|
15
|
+
gem.required_ruby_version = '>= 1.9.3'
|
16
16
|
|
17
17
|
gem.files = `git ls-files`.split($/)
|
18
18
|
gem.test_files = gem.files.grep(%r{^(spec)/})
|
data/lib/hashids.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
class Hashids
|
4
|
-
VERSION = "0.0.
|
4
|
+
VERSION = "0.0.5"
|
5
5
|
DEFAULT_ALPHABET = "xcS4F6h89aUbideAI7tkynuopqrXCgTE5GBKHLMjfRsz"
|
6
6
|
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
SaltError = Class.new(ArgumentError)
|
9
|
+
MinLengthError = Class.new(ArgumentError)
|
10
|
+
AlphabetError = Class.new(ArgumentError)
|
11
11
|
|
12
12
|
def initialize(salt = "", min_length = 0, alphabet = DEFAULT_ALPHABET)
|
13
13
|
@salt = salt
|
@@ -21,6 +21,8 @@ class Hashids
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def encrypt(*numbers)
|
24
|
+
numbers.flatten! if numbers.length == 1
|
25
|
+
|
24
26
|
if numbers.empty? || numbers.reject { |n| Integer(n) && n > 0 }.any?
|
25
27
|
""
|
26
28
|
else
|
@@ -138,7 +140,7 @@ class Hashids
|
|
138
140
|
ret = "#{pad_left}#{ret}#{pad_right}"
|
139
141
|
excess = ret.length - min_length
|
140
142
|
|
141
|
-
ret = ret[(excess
|
143
|
+
ret = ret[(excess.div(2)), min_length] if excess > 0
|
142
144
|
alphabet = consistent_shuffle(alphabet, salt + ret)
|
143
145
|
end
|
144
146
|
|
@@ -150,20 +152,13 @@ class Hashids
|
|
150
152
|
|
151
153
|
if hash.length > 0
|
152
154
|
original_hash = hash
|
153
|
-
alphabet
|
154
|
-
lottery_char
|
155
|
-
|
156
|
-
@guards.each do |guard|
|
157
|
-
hash = hash.tr(guard, ' ')
|
158
|
-
end
|
159
|
-
|
160
|
-
hash_split = hash.split(' ')
|
155
|
+
alphabet = ""
|
156
|
+
lottery_char = ""
|
161
157
|
|
158
|
+
hash_split = hash.tr(@guards.join(''), ' ').split(' ')
|
162
159
|
hash = hash_split[[3,2].include?(hash_split.length) ? 1 : 0]
|
163
160
|
|
164
|
-
@seps.
|
165
|
-
hash = hash.tr(sep, ' ')
|
166
|
-
end
|
161
|
+
hash.tr!(@seps.join(''), ' ')
|
167
162
|
|
168
163
|
hash_array = hash.split(' ')
|
169
164
|
|
@@ -198,8 +193,8 @@ class Hashids
|
|
198
193
|
salt_array = salt.scan(@chars_regex)
|
199
194
|
sorting_array = []
|
200
195
|
|
201
|
-
salt_array <<
|
202
|
-
salt_array.each { |char| sorting_array <<
|
196
|
+
salt_array << 0 if salt_array.empty?
|
197
|
+
salt_array.each { |char| sorting_array << char.ord }
|
203
198
|
|
204
199
|
sorting_array.each_with_index do |int,i|
|
205
200
|
add = true
|
@@ -240,7 +235,7 @@ class Hashids
|
|
240
235
|
|
241
236
|
while number > 0
|
242
237
|
hash = "#{alphabet[number % alphabet.length]}#{hash}"
|
243
|
-
number = number
|
238
|
+
number = number.div(alphabet.length)
|
244
239
|
end
|
245
240
|
|
246
241
|
hash
|
data/spec/hashids_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require "minitest/autorun"
|
3
4
|
require "minitest/spec"
|
4
5
|
require "minitest/pride"
|
5
|
-
require "minitest/autorun"
|
6
6
|
|
7
7
|
require_relative "../lib/hashids"
|
8
8
|
|
@@ -74,6 +74,10 @@ describe Hashids do
|
|
74
74
|
}
|
75
75
|
end
|
76
76
|
|
77
|
+
it "can encrypt a list of numbers passed in as an array" do
|
78
|
+
hashids.encrypt([1,2,3]).must_equal 'eGtrS8'
|
79
|
+
end
|
80
|
+
|
77
81
|
it "returns an empty string if no numbers" do
|
78
82
|
hashids.encrypt.must_equal ""
|
79
83
|
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Peter Hellberg
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: Use hashids when you do not want to expose your database ids to the user.
|
15
14
|
email:
|
@@ -30,31 +29,28 @@ files:
|
|
30
29
|
- spec/hashids_spec.rb
|
31
30
|
homepage: https://github.com/peterhellberg/hashids.rb
|
32
31
|
licenses: []
|
32
|
+
metadata: {}
|
33
33
|
post_install_message:
|
34
34
|
rdoc_options: []
|
35
35
|
require_paths:
|
36
36
|
- lib
|
37
37
|
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
38
|
requirements:
|
40
|
-
- -
|
39
|
+
- - '>='
|
41
40
|
- !ruby/object:Gem::Version
|
42
|
-
version: 1.9.
|
41
|
+
version: 1.9.3
|
43
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
-
none: false
|
45
43
|
requirements:
|
46
|
-
- -
|
44
|
+
- - '>='
|
47
45
|
- !ruby/object:Gem::Version
|
48
46
|
version: '0'
|
49
|
-
segments:
|
50
|
-
- 0
|
51
|
-
hash: -2635611476242162703
|
52
47
|
requirements: []
|
53
48
|
rubyforge_project:
|
54
|
-
rubygems_version:
|
49
|
+
rubygems_version: 2.0.3
|
55
50
|
signing_key:
|
56
|
-
specification_version:
|
51
|
+
specification_version: 4
|
57
52
|
summary: Generate YouTube-like hashes from one or many numbers.
|
58
53
|
test_files:
|
59
54
|
- spec/hashids_profile.rb
|
60
55
|
- spec/hashids_spec.rb
|
56
|
+
has_rdoc:
|