You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* supports multiple IDs encoded in one `EncodedId` (eg '7aq6-0zqw' decodes to `[78, 45]`)
36
-
* supports encoding of hex strings (eg UUIDs), including mutliple IDs encoded in one `EncodedId`
37
-
* uses a reduced character set (Crockford alphabet) & ids split into groups of letters, ie 'human-readability'
38
-
* profanity limitation
48
+
### Rails support `encoded_id-rails`
39
49
40
-
To use with **Rails** check out the `encoded_id-rails` gem.
50
+
To use with **Rails** check out the [`encoded_id-rails`](https://github.com/stevegeek/encoded_id-rails) gem.
41
51
42
-
## Note on security of encoded IDs (hashids)
52
+
```ruby
53
+
classUser < ApplicationRecord
54
+
includeEncodedId::WithEncodedId
55
+
end
56
+
57
+
User.find_by_encoded_id("p5w9-z27j")
58
+
# => #<User id: 78>
59
+
```
60
+
61
+
### Note on security of encoded IDs (hashids)
43
62
44
63
**Encoded IDs are not secure**. It maybe possible to reverse them via brute-force. They are meant to be used in URLs as
45
64
an obfuscation. The algorithm is not an encryption.
46
65
47
66
Please read more on https://hashids.org/
48
67
49
-
50
-
## Compared to alternate Gems
68
+
## Compare to alternate Gems
51
69
52
70
-https://github.com/excid3/prefixed_ids
53
71
-https://github.com/namick/obfuscate_id
54
72
-https://github.com/norman/friendly_id
55
73
-https://github.com/SPBTV/with_uid
56
74
57
-
## See also
58
-
59
-
-https://hashids.org
60
-
-https://www.crockford.com/wrmg/base32.html
61
-
62
-
63
75
## Installation
64
76
65
77
Install the gem and add to the application's Gemfile by executing:
@@ -70,20 +82,189 @@ If bundler is not being used to manage dependencies, install the gem by executin
70
82
71
83
$ gem install encoded_id
72
84
73
-
## Usage
85
+
## `EncodedId::ReversibleId.new`
86
+
87
+
To create an instance of the encoder/decoder use `.new` with the `salt` option:
88
+
89
+
```ruby
90
+
coder =EncodedId::ReversibleId.new(
91
+
# The salt is required
92
+
salt: ...,
93
+
# And then the following options are optional
94
+
length:8,
95
+
split_at:4,
96
+
split_with:"-",
97
+
alphabet:EncodedId::Alphabet.modified_crockford,
98
+
hex_digit_encoding_group_size:4# Experimental
99
+
)
100
+
```
101
+
102
+
Note the `salt` value is required and should be a string of some length (greater than 3 characters). This is used to generate the encoded string.
103
+
104
+
It will need to be the same value when decoding the string back into the original ID. If the salt is changed, the encoded
105
+
strings will be different and possibly decode to different IDs.
106
+
107
+
### Options
74
108
75
-
TODO: Write usage instructions here
109
+
The encoded ID is configurable. The following can be changed:
76
110
77
-
### Rails
111
+
- the length, eg 8 characters for `p5w9-z27j`
112
+
- the alphabet used in it (min 16 characters)
113
+
- and the number of characters to split the output into and the separator
78
114
79
-
To use with rails try the `encoded_id-rails` gem.
115
+
### `length`
116
+
117
+
`length`: the minimum length of the encoded string. The default is 8 characters.
118
+
119
+
The actual length of the encoded string can be longer if the inputs cannot be represented in the minimum length.
120
+
121
+
### `alphabet`
122
+
123
+
`alphabet`: the alphabet used in the encoded string. By default it uses a variation of the Crockford reduced character set (https://www.crockford.com/base32.html).
124
+
125
+
`alphabet` must be an instance of `EncodedId::Alphabet`.
126
+
127
+
The default alphabet is `EncodedId::Alphabet.modified_crockford`.
128
+
129
+
To create a new alphabet, use `EncodedId::Alphabet.new`:
0 commit comments