Advanced Topics
Table of contents
Performance Considerations
In general, at the moment, Sqids are slower to encode than Hashids (especially if using the blocklist feature). However, they are faster to decode than Hashids. With YJIT enabled, the differences in speeds are smaller.
To get the most out of Sqids encode performance, consider a small (or no) blocklist (set the blocklist: option). The default Sqids blocklist is very costly on encode time, but extensive.
Security Considerations
It’s important to understand the security implications of using encoded IDs.
Not for Sensitive Data
Encoded IDs are not secure. They are meant to be used for obfuscation, not encryption. It may be possible to reverse them via brute-force, especially for simple or sequential IDs.
Don’t use encoded IDs as the sole protection for sensitive resources. Always implement proper authorization checks.
Read more about the security implications: Hashids expose salt value
Salts
Changing the salt: If you change your salt, all previously encoded IDs will no longer decode correctly. Have a migration plan if you need to change the salt.
Hex Encoding Features (Experimental)
EncodedId includes experimental support for encoding hex strings, which can be useful for UUIDs and other hex-based identifiers.
Encoding UUIDs
coder = EncodedId::ReversibleId.hashid(salt: "my-salt")
# Encode a UUID (hyphens are automatically stripped from input)
uuid = "9a566b8b-8618-42ab-8db7-a5a0276401fd"
encoded = coder.encode_hex(uuid)
# => "q66d-1429-0v59-qug7-35fv-9mys-kx58-ujvr-mfq6-av"
# Decode back to UUID (output does not include hyphens)
decoded = coder.decode_hex(encoded)
# => ["9a566b8b861842ab8db7a5a0276401fd"]
Optimizing Hex Encoding Length
For long hex strings like UUIDs, you can customize the hex_digit_encoding_group_size to get shorter encoded strings:
# Default hex_digit_encoding_group_size (4)
coder = EncodedId::ReversibleId.hashid(salt: "my-salt")
encoded = coder.encode_hex("9a566b8b861842ab8db7a5a0276401fd")
# => "q66d-1429-0v59-qug7-35fv-9mys-kx58-ujvr-mfq6-av"
# Larger group size for shorter output
coder = EncodedId::ReversibleId.hashid(
salt: "my-salt",
hex_digit_encoding_group_size: 32
)
encoded = coder.encode_hex("9a566b8b861842ab8db7a5a0276401fd")
# => "3352-63wk-2mx8-vj7g-m363-6zze-7rzw-m9"