opaque_id 1.2.0 → 1.4.0

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/docs/alphabets.md ADDED
@@ -0,0 +1,524 @@
1
+ ---
2
+ layout: default
3
+ title: Alphabets
4
+ nav_order: 6
5
+ description: "Complete guide to built-in alphabets and custom alphabet creation"
6
+ permalink: /alphabets/
7
+ ---
8
+
9
+ # Alphabets
10
+
11
+ OpaqueId provides flexible alphabet configuration for generating IDs with different character sets. This guide covers built-in alphabets, custom alphabet creation, and best practices for alphabet selection.
12
+
13
+ - TOC
14
+ {:toc}
15
+
16
+ ## Built-in Alphabets
17
+
18
+ OpaqueId comes with two pre-configured alphabets optimized for different use cases.
19
+
20
+ ### ALPHANUMERIC_ALPHABET (Default)
21
+
22
+ The default alphabet provides a good balance of security, readability, and URL safety.
23
+
24
+ ```ruby
25
+ # Characters: A-Z, a-z, 0-9 (62 characters)
26
+ # Use case: General purpose, URL-safe
27
+ # Example output: "V1StGXR8_Z5jdHi6B-myT"
28
+
29
+ class User < ApplicationRecord
30
+ include OpaqueId::Model
31
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
32
+ end
33
+ ```
34
+
35
+ **Characteristics:**
36
+
37
+ - **Length**: 62 characters
38
+ - **Characters**: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`
39
+ - **URL-safe**: Yes (no special characters)
40
+ - **Human-readable**: Yes (no confusing characters)
41
+ - **Performance**: Good (62 characters)
42
+
43
+ **Use cases:**
44
+
45
+ - General purpose ID generation
46
+ - Public URLs and user-facing identifiers
47
+ - APIs and web services
48
+ - Database primary keys
49
+
50
+ ### STANDARD_ALPHABET
51
+
52
+ The standard alphabet provides the fastest generation speed with 64 characters.
53
+
54
+ ```ruby
55
+ # Characters: A-Z, a-z, 0-9, -, _ (64 characters)
56
+ # Use case: Fastest generation, URL-safe
57
+ # Example output: "V1StGXR8_Z5jdHi6B-myT"
58
+
59
+ class User < ApplicationRecord
60
+ include OpaqueId::Model
61
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
62
+ end
63
+ ```
64
+
65
+ **Characteristics:**
66
+
67
+ - **Length**: 64 characters
68
+ - **Characters**: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`
69
+ - **URL-safe**: Yes (includes hyphens and underscores)
70
+ - **Human-readable**: Yes (minimal special characters)
71
+ - **Performance**: Best (64 characters, optimized path)
72
+
73
+ **Use cases:**
74
+
75
+ - High-performance applications
76
+ - Internal system identifiers
77
+ - APIs with high throughput
78
+ - Background job systems
79
+
80
+ ## Alphabet Comparison
81
+
82
+ | Alphabet | Characters | Length | URL-Safe | Performance | Use Case |
83
+ | ----------------------- | -------------------- | ------ | -------- | ----------- | ---------------- |
84
+ | `ALPHANUMERIC_ALPHABET` | A-Z, a-z, 0-9 | 62 | ✅ | Good | General purpose |
85
+ | `STANDARD_ALPHABET` | A-Z, a-z, 0-9, -, \_ | 64 | ✅ | Best | High performance |
86
+
87
+ ## Custom Alphabets
88
+
89
+ Create custom alphabets for specific requirements and use cases.
90
+
91
+ ### Numeric Only
92
+
93
+ Generate numeric-only IDs for order numbers, invoice numbers, etc.
94
+
95
+ ```ruby
96
+ class Order < ApplicationRecord
97
+ include OpaqueId::Model
98
+
99
+ # Generate numeric-only IDs
100
+ self.opaque_id_alphabet = "0123456789"
101
+ self.opaque_id_length = 10
102
+ end
103
+
104
+ # Example output: "1234567890"
105
+ ```
106
+
107
+ **Use cases:**
108
+
109
+ - Order numbers
110
+ - Invoice numbers
111
+ - Reference numbers
112
+ - Sequential-looking IDs
113
+
114
+ ### Hexadecimal
115
+
116
+ Generate hexadecimal IDs for API keys, tokens, etc.
117
+
118
+ ```ruby
119
+ class ApiKey < ApplicationRecord
120
+ include OpaqueId::Model
121
+
122
+ # Generate hexadecimal IDs
123
+ self.opaque_id_alphabet = "0123456789abcdef"
124
+ self.opaque_id_length = 16
125
+ end
126
+
127
+ # Example output: "a1b2c3d4e5f67890"
128
+ ```
129
+
130
+ **Use cases:**
131
+
132
+ - API keys
133
+ - Authentication tokens
134
+ - Session identifiers
135
+ - Cryptographic keys
136
+
137
+ ### URL-Safe Characters
138
+
139
+ Generate IDs with maximum URL safety.
140
+
141
+ ```ruby
142
+ class Article < ApplicationRecord
143
+ include OpaqueId::Model
144
+
145
+ # Generate URL-safe IDs
146
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
147
+ self.opaque_id_length = 8
148
+ end
149
+
150
+ # Example output: "V1StGXR8"
151
+ ```
152
+
153
+ **Use cases:**
154
+
155
+ - URL slugs
156
+ - Public identifiers
157
+ - Social media sharing
158
+ - Email-friendly IDs
159
+
160
+ ### Base64-Style
161
+
162
+ Generate Base64-style IDs for maximum character diversity.
163
+
164
+ ```ruby
165
+ class Document < ApplicationRecord
166
+ include OpaqueId::Model
167
+
168
+ # Generate Base64-style IDs
169
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
170
+ self.opaque_id_length = 12
171
+ end
172
+
173
+ # Example output: "V1StGXR8_Z5j"
174
+ ```
175
+
176
+ **Use cases:**
177
+
178
+ - Document identifiers
179
+ - File references
180
+ - Binary data representation
181
+ - High-entropy requirements
182
+
183
+ ### Uppercase Only
184
+
185
+ Generate uppercase-only IDs for better readability.
186
+
187
+ ```ruby
188
+ class Product < ApplicationRecord
189
+ include OpaqueId::Model
190
+
191
+ # Generate uppercase-only IDs
192
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
193
+ self.opaque_id_length = 10
194
+ end
195
+
196
+ # Example output: "V1STGXR8Z5"
197
+ ```
198
+
199
+ **Use cases:**
200
+
201
+ - Product codes
202
+ - SKUs
203
+ - License keys
204
+ - Human-readable identifiers
205
+
206
+ ### Lowercase Only
207
+
208
+ Generate lowercase-only IDs for consistent formatting.
209
+
210
+ ```ruby
211
+ class User < ApplicationRecord
212
+ include OpaqueId::Model
213
+
214
+ # Generate lowercase-only IDs
215
+ self.opaque_id_alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
216
+ self.opaque_id_length = 12
217
+ end
218
+
219
+ # Example output: "v1stgxr8z5jd"
220
+ ```
221
+
222
+ **Use cases:**
223
+
224
+ - Usernames
225
+ - Subdomains
226
+ - File names
227
+ - Consistent formatting
228
+
229
+ ### No Confusing Characters
230
+
231
+ Generate IDs without characters that can be easily confused.
232
+
233
+ ```ruby
234
+ class User < ApplicationRecord
235
+ include OpaqueId::Model
236
+
237
+ # Exclude confusing characters: 0, O, l, I, 1
238
+ self.opaque_id_alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"
239
+ self.opaque_id_length = 15
240
+ end
241
+
242
+ # Example output: "V2StGXR8Z5jdHi6B"
243
+ ```
244
+
245
+ **Use cases:**
246
+
247
+ - Human-readable IDs
248
+ - Voice communication
249
+ - Hand-written transcription
250
+ - Accessibility
251
+
252
+ ### Specialized Characters
253
+
254
+ Generate IDs with specific character sets for unique requirements.
255
+
256
+ ```ruby
257
+ class Game < ApplicationRecord
258
+ include OpaqueId::Model
259
+
260
+ # Game-friendly characters (no vowels to avoid words)
261
+ self.opaque_id_alphabet = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz23456789"
262
+ self.opaque_id_length = 8
263
+ end
264
+
265
+ # Example output: "B2StGXR8"
266
+ ```
267
+
268
+ **Use cases:**
269
+
270
+ - Game codes
271
+ - Room identifiers
272
+ - Tournament codes
273
+ - Content filtering
274
+
275
+ ## Alphabet Selection Guide
276
+
277
+ ### Choose by Use Case
278
+
279
+ #### Public URLs
280
+
281
+ ```ruby
282
+ # URL-safe, human-readable
283
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
284
+ ```
285
+
286
+ #### High Performance
287
+
288
+ ```ruby
289
+ # Fastest generation
290
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
291
+ ```
292
+
293
+ #### Human Readable
294
+
295
+ ```ruby
296
+ # No confusing characters
297
+ self.opaque_id_alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"
298
+ ```
299
+
300
+ #### Maximum Security
301
+
302
+ ```ruby
303
+ # High entropy, many characters
304
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
305
+ ```
306
+
307
+ #### Numeric Only
308
+
309
+ ```ruby
310
+ # Numbers only
311
+ self.opaque_id_alphabet = "0123456789"
312
+ ```
313
+
314
+ ### Choose by Length
315
+
316
+ #### Short IDs (8-12 characters)
317
+
318
+ ```ruby
319
+ # Use larger alphabets for shorter IDs
320
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
321
+ self.opaque_id_length = 8
322
+ ```
323
+
324
+ #### Medium IDs (15-21 characters)
325
+
326
+ ```ruby
327
+ # Standard alphabets work well
328
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
329
+ self.opaque_id_length = 21
330
+ ```
331
+
332
+ #### Long IDs (32+ characters)
333
+
334
+ ```ruby
335
+ # Any alphabet works, consider performance
336
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
337
+ self.opaque_id_length = 32
338
+ ```
339
+
340
+ ## Performance Considerations
341
+
342
+ ### Alphabet Size Impact
343
+
344
+ Larger alphabets generally provide better performance due to optimized algorithms:
345
+
346
+ ```ruby
347
+ # 64 characters - fastest (optimized path)
348
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
349
+
350
+ # 62 characters - good performance
351
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
352
+
353
+ # 16 characters - slower (rejection sampling)
354
+ self.opaque_id_alphabet = "0123456789abcdef"
355
+
356
+ # 10 characters - slowest (rejection sampling)
357
+ self.opaque_id_alphabet = "0123456789"
358
+ ```
359
+
360
+ ### Character Set Optimization
361
+
362
+ ```ruby
363
+ # Optimized for 64 characters
364
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
365
+
366
+ # Good performance with 62 characters
367
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
368
+
369
+ # Slower with smaller sets
370
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
371
+ ```
372
+
373
+ ## Security Considerations
374
+
375
+ ### Entropy and Security
376
+
377
+ Larger alphabets provide more entropy per character:
378
+
379
+ ```ruby
380
+ # High entropy (64 characters)
381
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
382
+ self.opaque_id_length = 21
383
+ # Entropy: 21 * log2(64) = 126 bits
384
+
385
+ # Medium entropy (62 characters)
386
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
387
+ self.opaque_id_length = 21
388
+ # Entropy: 21 * log2(62) = 125 bits
389
+
390
+ # Lower entropy (16 characters)
391
+ self.opaque_id_alphabet = "0123456789abcdef"
392
+ self.opaque_id_length = 21
393
+ # Entropy: 21 * log2(16) = 84 bits
394
+ ```
395
+
396
+ ### Character Predictability
397
+
398
+ Avoid predictable character patterns:
399
+
400
+ ```ruby
401
+ # Good - random character distribution
402
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
403
+
404
+ # Avoid - sequential patterns
405
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
406
+
407
+ # Avoid - common patterns
408
+ self.opaque_id_alphabet = "0123456789"
409
+ ```
410
+
411
+ ## Best Practices
412
+
413
+ ### 1. Choose Appropriate Alphabet Size
414
+
415
+ - **64 characters**: Best performance, good security
416
+ - **62 characters**: Good balance, URL-safe
417
+ - **32 characters**: Moderate performance, specific use cases
418
+ - **16 characters**: Slower, specific requirements
419
+ - **10 characters**: Slowest, numeric-only needs
420
+
421
+ ### 2. Consider URL Safety
422
+
423
+ ```ruby
424
+ # URL-safe characters
425
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
426
+
427
+ # Avoid URL-unsafe characters
428
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
429
+ ```
430
+
431
+ ### 3. Avoid Confusing Characters
432
+
433
+ ```ruby
434
+ # Exclude confusing characters
435
+ self.opaque_id_alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"
436
+
437
+ # Or use purge_chars option
438
+ self.opaque_id_purge_chars = ['0', 'O', 'l', 'I', '1']
439
+ ```
440
+
441
+ ### 4. Test Your Alphabet
442
+
443
+ ```ruby
444
+ # Test alphabet generation
445
+ class User < ApplicationRecord
446
+ include OpaqueId::Model
447
+
448
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
449
+ self.opaque_id_length = 10
450
+ end
451
+
452
+ # Generate test IDs
453
+ 5.times { puts User.new.opaque_id }
454
+ # => "V1STGXR8Z5"
455
+ # => "K8JH2MN9PL"
456
+ # => "Q3RT7S1V4X"
457
+ # => "B6NF9C2M8Y"
458
+ # => "D4HG7K1P5W"
459
+ ```
460
+
461
+ ## Common Alphabet Patterns
462
+
463
+ ### E-commerce
464
+
465
+ ```ruby
466
+ # Order numbers - numeric
467
+ class Order < ApplicationRecord
468
+ include OpaqueId::Model
469
+ self.opaque_id_alphabet = "0123456789"
470
+ self.opaque_id_length = 8
471
+ end
472
+
473
+ # Product codes - alphanumeric
474
+ class Product < ApplicationRecord
475
+ include OpaqueId::Model
476
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
477
+ self.opaque_id_length = 12
478
+ end
479
+ ```
480
+
481
+ ### API Development
482
+
483
+ ```ruby
484
+ # API keys - hexadecimal
485
+ class ApiKey < ApplicationRecord
486
+ include OpaqueId::Model
487
+ self.opaque_id_alphabet = "0123456789abcdef"
488
+ self.opaque_id_length = 32
489
+ end
490
+
491
+ # API tokens - standard
492
+ class ApiToken < ApplicationRecord
493
+ include OpaqueId::Model
494
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
495
+ self.opaque_id_length = 24
496
+ end
497
+ ```
498
+
499
+ ### Content Management
500
+
501
+ ```ruby
502
+ # Article slugs - URL-safe
503
+ class Article < ApplicationRecord
504
+ include OpaqueId::Model
505
+ self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
506
+ self.opaque_id_length = 8
507
+ end
508
+
509
+ # Comments - standard
510
+ class Comment < ApplicationRecord
511
+ include OpaqueId::Model
512
+ self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
513
+ self.opaque_id_length = 15
514
+ end
515
+ ```
516
+
517
+ ## Next Steps
518
+
519
+ Now that you understand alphabet configuration:
520
+
521
+ 1. **Explore [Configuration](configuration.md)** for complete configuration options
522
+ 2. **Check out [Use Cases](use-cases.md)** for real-world alphabet usage
523
+ 3. **Review [Performance](performance.md)** for alphabet optimization tips
524
+ 4. **Read [API Reference](api-reference.md)** for complete alphabet documentation