Content · Web Development · PHP

PHP sodium_crypto_shorthash() Function: Usage and Examples

sodium_crypto_shorthash calculates a short keyed SipHash fingerprint meant for hash-table collision resistance, not cryptographic.

2 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

sodium_crypto_shorthash calculates a short keyed SipHash fingerprint meant for hash-table collision resistance, not cryptographic signatures.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: sodium_crypto_shorthash calculates a short keyed SipHash fingerprint meant for hash-table collision resistance, not cryptographic signatures.

The sodium extension must be enabled.

Syntax and Return Value


sodium_crypto_shorthash(string $message, string $key): string
ParameterPurpose
$message (string)The plaintext or input message bytes; content is not automatically character-normalized.
$key (string)Raw key bytes in the required algorithm-specific length; protect them from disclosure.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: The short output is not collision resistant enough for document integrity, signatures or password hashing.

Example 1: Basic Operation


SipHash is meant for short hash-table fingerprints, not collision-resistant document integrity.

<?php
$message = "Account update";
$key = sodium_crypto_shorthash_keygen();
echo bin2hex(sodium_crypto_shorthash($message, $key));

Example 2: A Different Use Case


SipHash is meant for short hash-table fingerprints, not collision-resistant document integrity.

<?php
// Check the alternative case.
$message = "New order: 42";
$key = sodium_crypto_shorthash_keygen();
echo bin2hex(sodium_crypto_shorthash($message, $key));

Practical Example: Checking the Result


SipHash is meant for short hash-table fingerprints, not collision-resistant document integrity.

<?php
// Verify the expected behavior before using results.
$message = "Export completed";
$key = sodium_crypto_shorthash_keygen();
echo bin2hex(sodium_crypto_shorthash($message, $key));

Important Behavior and Edge Cases


The short output is not collision resistant enough for document integrity, signatures or password hashing.

The short output is not collision resistant enough for document integrity, signatures or password hashing.

Comparison with Related Functions


ApproachKey distinction
sodium_crypto_shorthashsodium_crypto_shorthash calculates a short keyed SipHash fingerprint meant for hash-table collision resistance, not cryptographic signatures.
Related techniquesodium_crypto_generichash supports larger digests for document hashing.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismsodium_crypto_generichash supports larger digests for document hashing.
Ignoring a security prerequisite or failure caseThe short output is not collision resistant enough for document integrity, signatures or password hashing.

Key Takeaways


  • sodium_crypto_shorthash calculates a short keyed SipHash fingerprint meant for hash-table collision resistance, not cryptographic signatures.
  • The short output is not collision resistant enough for document integrity, signatures or password hashing.
  • sodium_crypto_generichash supports larger digests for document hashing.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.