Skip to main content

DIGEST

Introduction

The DIGEST command returns a hash digest for the value stored at a specified key. It uses the XXH3 hashing algorithm to compute a 64-bit hash and returns it as a 16-character hexadecimal string. This command is useful for comparing values without transferring the full content, implementing checksums, or detecting changes.

Syntax

DIGEST key

Time complexity: O(N) where N is the length of the string value

ACL categories: @read, @string, @fast

Parameter Explanations

  • key: The key whose value should be hashed. Must be a string type.

Return Values

  • Returns a 16-character hexadecimal string representing the XXH3 64-bit hash of the value.
  • Returns nil if the key does not exist.
  • Returns an error if the key exists but holds a non-string value type.

Code Examples

Basic Example

Compute digest of a string value:

dragonfly$> SET mykey "Hello, Dragonfly!"
OK
dragonfly$> DIGEST mykey
"d4f3c8b2a1e6f7d9"

Non-Existent Key

If the key does not exist, DIGEST returns nil:

dragonfly$> DIGEST non_existent_key
(nil)

Digest Consistency

The same value always produces the same digest:

dragonfly$> SET key1 "test"
OK
dragonfly$> SET key2 "test"
OK
dragonfly$> DIGEST key1
"a1b2c3d4e5f67890"
dragonfly$> DIGEST key2
"a1b2c3d4e5f67890"

Different Values Produce Different Digests

dragonfly$> SET key1 "hello"
OK
dragonfly$> SET key2 "world"
OK
dragonfly$> DIGEST key1
"1234567890abcdef"
dragonfly$> DIGEST key2
"fedcba0987654321"

Error on Wrong Type

dragonfly$> LPUSH mylist "item"
(integer) 1
dragonfly$> DIGEST mylist
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Best Practices

  • Use DIGEST to compare values efficiently without transferring full content over the network.
  • Implement change detection mechanisms by storing and comparing digests.
  • Use with DELEX IFDEQ/IFDNE for conditional deletions based on digest matching.

Common Mistakes

  • Attempting to use DIGEST on non-string keys will result in a WRONGTYPE error.
  • Assuming different values might produce the same digest (hash collisions are extremely rare with XXH3).

FAQs

What hashing algorithm does DIGEST use?

DIGEST uses the XXH3 algorithm, which is a fast, non-cryptographic hash function that produces a 64-bit hash value.

Is DIGEST suitable for cryptographic purposes?

No, DIGEST uses XXH3 which is not a cryptographic hash function. For cryptographic purposes, use dedicated cryptographic hash functions.

Can DIGEST work with compressed or integer-encoded strings?

Yes, DIGEST handles all string encodings including raw strings, integer-encoded strings, and compressed strings.

Is DIGEST compatible with Redis?

Yes, DIGEST is compatible with Redis 8.4.0 and later versions.