PHP

Bob Jenkins' One-At-A-Time hashing algorithm in PHP

CK CK 3788 
Created at  
54 0 0

Bob Jenkins' One-At-A-Time hashing algorithm is simply known as jenkins_hash() providing 8 bytes long hash value.

<?php
 
// Bob Jenkins' One-At-A-Time hashing algorithm.
function jenkins_hash($key)
{
    $key = (string)$key;
    $len = strlen($key);
    for($hash = $i = 0; $i < $len; ++$i)
    {
        $hash += ord($key[$i]);
        $hash += ($hash << 10);
        $hash ^= ($hash >> 6);
    }
    $hash += ($hash << 3);
    $hash ^= ($hash >> 11);
    $hash += ($hash << 15);
    return str_pad(dechex($hash),16,0,STR_PAD_LEFT);
}
 
?>

For better security, I prefer to add salt as following:

<?php
 
// Bob Jenkins' One-At-A-Time hashing algorithm with Salt
function jenkins_hash($key, $salt=337)
{
    $key = (string)$key;
    $len = strlen($key);
    for($hash = $salt, $i = 0; $i < $len; ++$i)
    {
        $hash += ord($key[$i]);
        $hash += ($hash << 10);
        $hash ^= ($hash >> 6);
    }
    $hash += ($hash << 3);
    $hash ^= ($hash >> 11);
    $hash += ($hash << 15);
    return str_pad(dechex($hash),16,0,STR_PAD_LEFT);
}
 
?>

 

Tags Hash jenkins_hash Facebook X
Comments 0
Similar posts
  1. 32bits simple hash function in PHP
    40
  2. HashTable Tutorial In C#
    1,788
  1. Google BigQuery Example to converted Nested FIelds in JSON
    459
  2. Verifying Apple In-App Purchase validation receipt with PHP
    1,787
  3. Useful code for file-based Unit Test in PHP
    500
  4. Function Overloading Implementation in PHP with Magic Mathods
    1,757
  5. PHP Magic - the Null Coalescing Operator (??) - return the value if the give variable is NULL
    1,672
  6. How do I find php.ini on Mac?
    493
  7. Mastering Excel Data Importation in PHP
    555
  8. Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file
    976
  9. How do I determine the client IP type (IPv4/IPv6) in PHP
    834
  10. What is 302 Found Redirection in HTTP 1.1?
    491
  11. Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records
    620
  12. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    739
  13. CentOS 7 PHP Apache Kafka: Installation, Examples, and System Diagram
    307
  14. Apache Kafka Explained: Powering Netflix, Building with PHP
    492
  15. Array of arguments passed to script in PHP
    90
  16. PHP Code Compression Tool by removing whitespace, strips comments - PHP Minifier/Compressor
    1,415
  17. Setup Apache+PHP+MySQL on Centos 6.7
    605
  18. mcrypt missing when using phpMyAdmin on Centos 6.7
    550
  19. PHP Code can know the current OS is windows or not
    584
  20. Checking similarity between two strings in PHP
    675
  21. Simple PHP calendar
    478
  22. Simple PHP code performance measuring example
    792
  23. Convert plain HTML to XHTML
    453
  24. GD library version in PHP
    505
  25. JSON format control in PHP
    693
  26. mysql optimization – if you are using Word Press, do not turn off persistent connection
    593
  27. File downloading function through PHP code
    532
  28. Image file resizing in PHP
    562
  29. Apache server unstable detection and the related server(mysql, apache) restart automatically
    538
  30. How to set AUTO_INCREMENT ID in mySQL ?
    607
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,000
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Invalid Auction Storms in Ad Tech
    98
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  9. The Two Clocks of Ad Server Architecture
    70
  10. Frequency Cap Architecture Overview
    515
  11. Collecting Proof-of-Play from 100,000 Screens
    146
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  13. What is RedisVL?
    109
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    231
  15. TPU & GPU Cluster Management Tools
    245
  16. The Concept of Scaffolding in AI Agents
    234
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  21. Machine Learning: Training vs. Ranking
    222
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  24. Designing a Data Curation System Around a Vector Database
    221
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  29. Understanding Data Lineage
    262
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    167