Download the PHP package m-wardany/hash-ids without Composer
On this page you can find all versions of the php package m-wardany/hash-ids. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download m-wardany/hash-ids
More information about m-wardany/hash-ids
Files in m-wardany/hash-ids
Package hash-ids
Short Description This package is designed to hash sequential keys so that they appear as random characters, making them difficult to predict. There is no requirement to check if the key already exists in the database before saving, as each key is guaranteed to be unique
License MIT
Informations about the package hash-ids
Hash Id
A Laravel package designed to hash sequential keys into random-appearing characters, making them difficult to predict. Each hashed key is guaranteed to be unique without requiring database existence checks before saving.
Features
- 🔐 Secure hashing - Transform sequential IDs into unpredictable hashes
- 🎯 Multiple hash types - Support for mixed, text, and integer alphabets
- 🔧 Flexible configuration - Customize prefixes, suffixes, and minimum lengths
- 🔑 Custom encryption keys - Use different encryption keys per attribute
- 📦 Multiple hashes - Generate multiple hash variants for a single attribute
- ✨ Laravel integration - Seamless integration with Eloquent models
Installation
Step 1: Install via Composer
Step 2: Publish Configuration
This will create a config/hashid.php file where you can customize the default settings.
Configuration
Update config/hashid.php to set your global defaults:
Usage
Basic Setup
To use hash IDs in your model, implement the Hashable interface and use the HasHashId trait:
Hash Builder Methods
The HashBuilder class provides three static methods for creating hash configurations:
1. Mixed Alphabet (Alphanumeric)
Generates hashes using both letters and numbers (e.g., aB3xK9)
2. Text Only
Generates hashes using only letters (e.g., aBxKmP)
3. Integer Only
Generates hashes using only numbers (e.g., 839201)
Customization Options
Minimum Length
Sets the minimum length of the generated hash.
Prefix
Adds a static prefix to the hash (e.g., POST-aB3xK9).
Suffix (Static)
Adds a static suffix to the hash (e.g., aB3xK9-ACTIVE).
Suffix (Dynamic)
Adds a dynamic suffix based on model data.
Custom Encryption Key
Uses a custom encryption key instead of the global default.
Advanced Examples
Single Hash with Dynamic Suffix
Multiple Hashes for One Attribute
Complex Multi-Attribute Setup
How It Works
- Implement Interface: Add the
Hashableinterface to your model - Use Trait: Include the
HasHashIdtrait - Define Attributes: Specify which attributes to hash in
getHashAttributes() - Automatic Hashing: Hashes are generated automatically when the model is saved
- Access Hashed Values: Access via the configured attribute name (e.g.,
$post->post_key)
Database Schema
Ensure your database has columns for the hashed attributes:
Best Practices
- ✅ Use descriptive names for hashed attributes (e.g.,
public_id,share_token) - ✅ Add database indexes to hashed columns for better query performance
- ✅ Use different encryption keys for different security contexts
- ✅ Set appropriate minimum lengths based on your uniqueness requirements
- ✅ Add prefixes to make hash types easily identifiable
- ⚠️ Never expose the original sequential ID in public APIs
Security Considerations
- The package uses secure hashing algorithms to prevent prediction attacks
- Each encryption key creates a unique hash set - keep your keys secure
- Hashed values are deterministic (same input + key = same output)
- Consider using different encryption keys for public vs internal hashes
License
This package is open-source software licensed under the MIT license.