Algorithmcore
Context-triggered piecewise hashing
Also called CTPH, fuzzy hashing.
Cuts a file where a rolling hash of the last 7 bytes hits a trigger value, hashes each piece to one character, and compares files by the edit distance of these strings.
- Time
- O(n log n) worstO(n) average · best
- Space
- O(s) extra
- Where
- n = the number of bytes of the file; s = the maximum signature length (64)
- Model
- Word RAM
- Properties
- not cryptographicdeterministiclocality preservingofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Andrew Tridgell, Jesse Kornblum, 2006
Context-triggered piecewise hashing (CTPH) gives a file a short signature, such that two files that differ by a few local edits get similar signatures. A rolling hash over the last 7 bytes decides where a piece ends, as in Rabin fingerprinting. Each piece is hashed to one character, and the signature is the string of these characters. Two signatures are compared by an edit distance and give a score from 0 to 100. Kornblum (2006) published the method as ssdeep, based on Andrew Tridgell's spamsum. Forensic examiners and malware analysts use it to find files that are almost the same, for example two builds of one program, and it is one of the inputs of the malware detection problem. The examples and the code of this page use random byte strings and short sentences only.
In plain words
A cryptographic hash changes completely when one byte of a file changes. That is right for a password check, and wrong when you want to find files that are almost the same.
CTPH cuts the file into pieces and hashes each piece. The trick is where the cuts go. A small window slides over the file, and a cut goes where the hash of the window has a special value. The cut points depend only on the bytes near them. So after an edit, the cuts soon fall on the same bytes as before, and only the pieces near the edit change.
Each piece gives one character. Two files that share most pieces share most characters, in the same order. The score counts how much of the two strings is common.
The problem
The input is a byte string of length . The output is a signature : a block size , a string of at most characters for block size , and a string of at most 32 characters for block size . A second function takes two signatures and returns a score from 0 (no evidence of similarity) to 100 (the strings are equal). The aim is that a local edit of changes only a few characters of the signature, while two unrelated files almost never share a run of 7 characters. This is a form of near-duplicate detection for byte strings, where the MinHash method of web pages is the analogue for sets of words.
The algorithm
The constants are a window of 7 bytes, , , a Base64 alphabet B64 of 64 characters, and the FNV prime with the start value for the piece hash. The rolling hash keeps three 32-bit values: , the sum of the window; , the sum weighted 7, 6, ..., 1 from the newest byte; and , a shift-and-XOR value. Its output is . The piece hash step is .
CTPH-SIGNATURE(x) ▷ x has n bytes- 1
b ← B_min; while b·S < n do b ← 2b - 2
loop - 3
s1 ← "", s2 ← "", h1 ← h2 ← INIT, reset the rolling hash - 4
for each byte c of x do - 5
r ← ROLL(c); h1 ← h1·p xor c; h2 ← h2·p xor c - 6
if r mod b = b - 1 and |s1| < S - 1 then append B64[h1 mod 64] to s1; h1 ← INIT - 7
if r mod 2b = 2b - 1 and |s2| < S/2 - 1 then append B64[h2 mod 64] to s2; h2 ← INIT - 8
if bytes were read since the last cut of s1 (of s2) then append B64[h1 mod 64] to s1 (B64[h2 mod 64] to s2) - 9
if b > B_min and |s1| < S/2 then b ← b/2; continue - 10
return (b, s1, s2) CTPH-COMPARE((b, s1, s2), (b', t1, t2))- 11
if b = b' then return max(SCORE(s1, t1), SCORE(s2, t2)) - 12
if b' = 2b then return SCORE(s2, t1) - 13
if b = 2b' then return SCORE(s1, t2) - 14
return 0 ▷ no strings of a common block size SCORE(u, v)- 15
cut each run of more than 3 equal characters in u and in v to 3 - 16
if u and v have no common substring of length 7 then return 0 - 17
d ← |u| + |v| - 2·LCS(u, v) ▷ the insert and delete distance - 18
return ⌊100·(|u| + |v| - d) / (|u| + |v|)⌋
Line L1 picks the smallest block size with , so that about 64 triggers are expected. Lines L4 to L7 make one pass. A trigger for fires on average once in bytes, and line L6 closes the current piece there. Line L7 does the same for , so has about half as many characters. The caps keep the strings short: after cuts, the rest of the file goes into the last piece, and line L8 emits it. If the pass gave fewer than 32 characters, the guess of was too large for this file, and line L9 halves and runs the pass again.
Two signatures can only be compared on strings of one block size, because a character means "a piece of about bytes". Lines L11 to L13 find the common block size. If the sizes differ by a factor of 4 or more, the score is 0. SCORE removes long runs, which come from repeated content such as padding, and then requires a common substring of 7 characters. This rule removes most chance matches between unrelated files. The score is , where LCS is the length of a longest common subsequence.
ssdeep uses a weighted edit distance and a different scale, and it caps the score of small block sizes. This page uses the plain form above. The code follows the structure of spamsum and ssdeep, but it does not claim output that is identical to ssdeep.
Example
Take = the quick brown fox (19 bytes). Line L1 gives , because . The rolling values after each byte give 3-triggers () at positions 2, 6, 11 and 12, and one 6-trigger () at position 2.
| piece | bytes | ends at | at the end | character | |
|---|---|---|---|---|---|
| 1 | the |
p = 2 | 124445 | 2 | U |
| 2 | qui |
p = 6 | 3594639422 | 2 | k |
| 3 | ck br |
p = 11 | 2483362040 | 2 | L |
| 4 | o |
p = 12 | 2158072664 | 2 | K |
| 5 | wn fox |
end of file (line L8) | 1497864676 | 1 | X |
So = UkLKX. For the only cut is after the, so = UV: the first character is U again, because the piece the is the same. The signature is . Line L9 does not halve, because .
The string the quick brown cat has the signature . The first four pieces are the same and only the last character changed. But the score is 0, because neither string has 7 characters (line L16). Even the identical signature UkLKX scores 0 against itself. CTPH is a method for files of some size, not for short strings.
A bigger example, from the test file: a random file of 20000 bytes gets (the smallest with ) and strings of 61 and 26 characters. Flip one byte at offset 5000, two bytes at offset 12000, and insert 13 bytes at offset 16000. Each of the three edits changes one character of and one of . For , LCS = 58, so the score is . For , LCS = 23 and the score is . Line L11 returns 95. Two independent random files of 8000 bytes score 0 in all the tests.
Correctness
The key fact is that the rolling hash after byte is a function of only: , , and is the XOR of shifted by places, cut to 32 bits. So the cut decision at depends only on the 7 bytes that end at . If an edit replaces , every cut before and every cut after is still a cut, shifted by the change in length. Pieces that lie fully outside this zone keep their bytes and their character. So the two strings have the form and , with and about as long as the number of cuts near the edit plus one, as long as no cap is reached. The loop stops because halves only while , and the score is symmetric, lies in , and is 100 only for equal strings. The full proof is in the proofs folder.
Proof: CTPH cuts a file only on local context, so a local edit changes only a few signature charactersfull
We write for the input bytes and set for . All arithmetic on the hash values is modulo . The block size is fixed inside one pass of lines L3 to L8. A position is a -trigger of if the value that line L5 computes after byte satisfies .
Lemma 1 (the rolling hash is a function of the last 7 bytes). After line L5 has read , the three parts of the rolling hash are
so depends only on .
Proof. By induction on . Before the first byte all parts are 0 and the window holds zeros, which is the formula for with the convention for . Assume the formulas after . The window slot holds , the byte that leaves. The update of uses the old :
where the last step puts and drops the term , whose weight is 0. The update of adds and subtracts , which gives the sum of . The update of shifts every term left by 5 places and XORs in . A byte that was read 7 or more steps ago has been shifted by at least 35 places, so the mask removes it.
Lemma 2 (cuts are context triggered). Whether is a -trigger depends only on and .
Proof. Immediate from Lemma 1.
Lemma 3 (what a pass computes). Let be the -triggers of and let be its -triggers. When line L8 ends, is the string of the characters for the pieces , where the first pieces end at triggers and one last piece holds the rest of if the rest is not empty. Here is the piece hash started from HASH_INIT. The same holds for with the -triggers and the cap .
Proof. Line L5 updates with every byte, line L6 resets it right after it emits a character, and line L3 sets it to HASH_INIT at the start. So when line L6 emits at a trigger , is the piece hash of the bytes since the last emission. Line L6 emits at every -trigger until and never after. Line L8 emits the hash of the bytes after the last emission if there are any; the flag open1 of the code is true exactly then.
So and , which is the postcondition of the code.
Theorem 4 (a local edit is local in the signature). Let be with the part replaced by a string , so the bytes before are equal and the bytes after in are the bytes after in . Fix a block size and assume that and both have fewer than -triggers. Then the strings of and of for this have the form and , where and are common, is at most one more than the number of -triggers of in , and is at most one more than the number of -triggers of in .
Proof. Let . For the 7 bytes ending at are the same in and , so by Lemma 2 the -triggers below are the same. For the 7 bytes ending at in are the 7 bytes ending at in , so is a trigger of exactly when is a trigger of . By Lemma 3, with no cap reached, the pieces are the intervals between consecutive triggers, and the last piece runs to the end.
A piece of that ends at a trigger below is a piece of with the same bytes, so it gives the same character; these pieces form . A piece of whose previous trigger is above (so it starts after ) has the same bytes as the piece of that starts later, and it also ends at the matching trigger or at the end of the file; these pieces form . Every other piece of contains a byte of or starts right after a trigger in that range, and the number of these pieces is at most the number of triggers in plus one. The same count holds in .
The same statement holds for with the -triggers. So and the distance of line L17 on the raw strings is at most . Line L15 removes characters only from runs of more than 3 equal characters, and it does this in the same way inside and ; the bound is a statement about the raw signatures, and the run cut can change it only near the borders of and .
Lemma 5 (termination and cost). CTPH-SIGNATURE stops, and it makes at most passes, where is the block size of line L1.
Proof. Line L1 ends because doubles and is finite. In the loop of line L2, each repeat comes from line L9, which needs and halves . The block size is always times a power of 2, so it reaches after at most halvings, and then line L9 does not repeat. Each pass reads each byte once and does work per byte.
Lemma 6 (the score). For strings and after line L15, SCORE returns 0 if they share no substring of length 7, and otherwise . This value is symmetric in and , lies in , and equals 100 exactly when .
Proof. Line L17 gives , which turns line L18 into the formula. LCS is symmetric. Since , the value is at most 100, with equality exactly when , that is when . If line L16 passes, both strings have length at least 7, so the division is defined.
Theorem 7 (correctness). CTPH-SIGNATURE stops and returns as described by Lemma 3 for the block size the loop ends with, and CTPH-COMPARE returns a score in that compares only strings made with the same block size.
Proof. Termination and the form of the output are Lemmas 5 and 3. Line L11 compares with (both of block size ) and with (both ). Line L12 handles , where of the first signature and of the second both have block size ; line L13 is the mirror case. Any other pair of block sizes has no string of a common block size, and line L14 returns 0. Each score is in by Lemma 6.
Complexity
One pass of lines L4 to L8 reads each byte once and does word operations per byte, so it takes time. Line L1 makes at most doublings, and line L9 can halve as many times, so the worst case is . The halving is rare in practice: with chosen by line L1, about triggers are expected and is between 32 and 64, so one or two passes are typical. This is an observation on random-like data, not a bound. The extra space is the window of 7 bytes and the two strings, .
CTPH-COMPARE computes at most two LCS tables of at most cells, which is time, a constant. A search of one signature against stored signatures is , and an index of the 7-character substrings of the stored strings cuts this to the candidates that can score above 0.
The model is the word RAM with 32-bit words; all hash arithmetic is modulo .
Properties
- Local. A local edit changes only the pieces next to it (the theorem of the proof). This is the property that a cryptographic hash does not have on purpose.
- Self-synchronizing. After an insertion or a deletion, the cuts move with the bytes, so the rest of the signature is the same. A fixed block size would shift every later block.
- Deterministic, not random. There is no seed. Two runs on the same file give the same signature.
- Not a proof of relation. A score of 0 says that the method found no evidence of similarity. It does not say the files are unrelated. A high score is strong evidence for honest edits only.
- Not robust against an adversary. The trigger rule is public, so a person who changes a file on purpose can make the signature differ. Research on the security of similarity digests is a separate subject.
Variants and improvements
- Block size choice. Kornblum's paper gives , with a floor. Breitinger and Baier observe that the implementation uses the ceiling, which is what line L1 computes. For a random file of 24000 bytes line L1 gives .
- Two passes in all cases. Breitinger and Baier (2011) count the triggers of the four block sizes , , and in one pass and store their offsets. A second pass then hashes the pieces of the largest block size that gives at least 32 characters. They report a gain of about 55% in run time on their test image. The same counts show a file with far more triggers than expected, which can be a sign of a file changed on purpose to fill the cap of 64 characters.
- Bloom filter signatures. Roussev and coauthors replaced the rolling hash by the polynomial hash djb2 and the piece hash by MD5, and put 11 bits of each MD5 value into a Bloom filter. A comparison then counts common bits instead of a string distance. djb2 must be computed again for each window, so this is slower.
- Set-based digests. MinHash over the piece hashes, or over byte n-grams, estimates the Jaccard similarity of the two sets and works with locality-sensitive hashing for large collections. SimHash gives a fixed-length bit string instead.
When to use it
Use CTPH to find almost identical files in a large collection: versions of one document, two builds of one program, a sample and its repacked copy if the packer kept most bytes. It is cheap, it needs one or two passes, and the signature is less than 100 characters. Do not use it on small files, which have fewer than 7 pieces and never match. Do not use it as evidence against a careful adversary. For files whose content moves around in large blocks, a set-based digest finds more matches than a string distance.
Pitfalls
- Comparing strings of different block sizes. Writing
return SCORE(s1, t1)in place of lines L11 to L14 compares pieces of different average length, and two copies of one file whose sizes are on two sides of a doubling point get a low score. Compare only strings of a common block size, as lines L12 and L13 do. - Not resetting the piece hash. Without
h1 ← INITin line L6, each character is the hash of the whole prefix, and one edit changes every later character. Reset the piece hash at each cut. - A trigger that depends on the whole prefix. Line L5 must use a rolling hash of the window. A hash such as
r ← r·31 + cwithout removing the old byte makes each cut depend on all earlier bytes, and the signature no longer synchronizes after an edit. - Forgetting the last piece. Without line L8, two files that differ only after the last cut get equal signatures.
- Reading 0 as "different". Line L16 returns 0 for any string shorter than 7 characters, so two identical small files score 0. Check the signature length before you use the score.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/context-triggered-piecewise-hashing/. The authors are listed in prov/PW1KEF.jsonl.
"""Context-triggered piecewise hashing (CTPH), after Kornblum (2006) and spamsum.
A rolling hash over the last 7 bytes decides where a piece ends. Each piece
gets a small FNV-style hash, and the low 6 bits of that hash become one
Base64 character of the signature. Two signatures are compared with a score
from 0 to 100 that comes from the longest common subsequence of the two
character strings. The code follows the structure of spamsum and ssdeep but
does not claim output that is identical to ssdeep; the score of SCORE is the
simpler one that the page defines. The code works on byte strings that the
caller gives; it never opens a file.
"""
WINDOW = 7
B_MIN = 3
S = 64
HASH_INIT = 0x28021967
FNV_PRIME = 0x01000193
MASK = 0xFFFFFFFF
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Signature = tuple[int, str, str]
class RollingHash:
"""The rolling hash of spamsum over a window of WINDOW bytes, mod 2^32."""
def __init__(self) -> None:
self.h1 = 0 # sum of the bytes in the window
self.h2 = 0 # sum of the bytes times their weight 7, 6, ..., 1
self.h3 = 0 # shift and xor of the recent bytes
self.window = [0] * WINDOW
self.n = 0
def roll(self, c: int) -> int:
"""Add byte c, drop the byte that leaves the window, return the hash. Cost: O(1)."""
self.h2 = (self.h2 - self.h1 + WINDOW * c) & MASK
self.h1 = (self.h1 + c - self.window[self.n % WINDOW]) & MASK
self.window[self.n % WINDOW] = c
self.n += 1
self.h3 = ((self.h3 << 5) & MASK) ^ c
return (self.h1 + self.h2 + self.h3) & MASK
def piece_hash(h: int, c: int) -> int:
"""One step of the FNV-style piece hash of spamsum. Cost: O(1)."""
return ((h * FNV_PRIME) & MASK) ^ c
def ctph_signature(x: bytes) -> Signature:
"""Return (b, s1, s2): the block size and the signatures for b and 2b.
Precondition: x is a byte string of length n. Postcondition: len(s1) <= S
and len(s2) <= S // 2. Cost: O(n) per pass and at most
O(log(n / (B_MIN S)) + 1) passes, so O(n log n) in the worst case.
"""
n = len(x)
b = B_MIN # @line L1
while b * S < n:
b *= 2
while True: # @line L2
s1: list[str] = [] # @line L3
s2: list[str] = []
h1 = h2 = HASH_INIT
r = RollingHash()
open1 = open2 = False
for c in x: # @line L4
v = r.roll(c) # @line L5
h1 = piece_hash(h1, c)
h2 = piece_hash(h2, c)
open1 = open2 = True
if v % b == b - 1 and len(s1) < S - 1: # @line L6
s1.append(B64[h1 % 64])
h1, open1 = HASH_INIT, False
if v % (2 * b) == 2 * b - 1 and len(s2) < S // 2 - 1: # @line L7
s2.append(B64[h2 % 64])
h2, open2 = HASH_INIT, False
if open1: # @line L8
s1.append(B64[h1 % 64])
if open2:
s2.append(B64[h2 % 64])
if b > B_MIN and len(s1) < S // 2: # @line L9
b //= 2
continue
return b, "".join(s1), "".join(s2) # @line L10
def ctph_compare(sig_a: Signature, sig_b: Signature) -> int:
"""Similarity score from 0 to 100 of two signatures. Cost: O(S^2), a constant."""
b, s1, s2 = sig_a
bb, t1, t2 = sig_b
if b == bb: # @line L11
return max(score(s1, t1), score(s2, t2))
if bb == 2 * b: # @line L12
return score(s2, t1)
if b == 2 * bb: # @line L13
return score(s1, t2)
return 0 # @line L14
def cut_runs(u: str, k: int = 3) -> str:
"""Cut each run of more than k equal characters down to k. Cost: O(len(u))."""
out: list[str] = []
for ch in u:
if len(out) >= k and all(o == ch for o in out[-k:]):
continue
out.append(ch)
return "".join(out)
def has_common_substring(u: str, v: str, k: int = WINDOW) -> bool:
"""True if u and v share a substring of length k. Cost: O(k (len(u) + len(v)))."""
subs = {u[i : i + k] for i in range(len(u) - k + 1)}
return any(v[i : i + k] in subs for i in range(len(v) - k + 1))
def lcs_length(u: str, v: str) -> int:
"""Length of a longest common subsequence, by dynamic programming. Cost: O(len(u) len(v))."""
prev = [0] * (len(v) + 1)
for a in u:
cur = [0]
for j, c in enumerate(v):
cur.append(prev[j] + 1 if a == c else max(prev[j + 1], cur[j]))
prev = cur
return prev[-1]
def score(u: str, v: str) -> int:
"""Score of two signature strings of one block size, from 0 to 100."""
u, v = cut_runs(u), cut_runs(v) # @line L15
if not has_common_substring(u, v): # @line L16
return 0
d = len(u) + len(v) - 2 * lcs_length(u, v) # @line L17
return (100 * (len(u) + len(v) - d)) // (len(u) + len(v)) # @line L18
test_context_triggered_piecewise_hashing.py 159 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/context-triggered-piecewise-hashing/. The authors are listed in prov/PW1KEF.jsonl.
import itertools
import random
from functools import lru_cache
from context_triggered_piecewise_hashing import (
B64,
B_MIN,
FNV_PRIME,
HASH_INIT,
MASK,
S,
RollingHash,
ctph_compare,
ctph_signature,
score,
)
def rolling_closed_form(x: bytes, p: int) -> int:
"""The rolling hash after byte p, from the last 7 bytes only."""
last = [x[p - j] if p - j >= 0 else 0 for j in range(7)]
h1 = sum(last)
h2 = sum((7 - j) * last[j] for j in range(7)) & MASK
h3 = 0
for j in range(7):
h3 ^= (last[j] << (5 * j)) & MASK
return (h1 + h2 + h3) & MASK
def fnv_from_scratch(piece: bytes) -> int:
h = HASH_INIT
for c in piece:
h = ((h * FNV_PRIME) & MASK) ^ c
return h
def signature_for(x: bytes, b: int, cap: int) -> str:
"""Cut x after each trigger position of block size b, up to cap pieces that end at a trigger."""
values = [rolling_closed_form(x, p) for p in range(len(x))]
cuts = [p for p, v in enumerate(values) if v % b == b - 1][:cap]
out, start = [], 0
for p in cuts:
out.append(B64[fnv_from_scratch(x[start : p + 1]) % 64])
start = p + 1
if start < len(x):
out.append(B64[fnv_from_scratch(x[start:]) % 64])
return "".join(out)
def oracle_signature(x: bytes) -> tuple[int, str, str]:
b = B_MIN
while b * S < len(x):
b *= 2
while True:
s1 = signature_for(x, b, S - 1)
s2 = signature_for(x, 2 * b, S // 2 - 1)
if b > B_MIN and len(s1) < S // 2:
b //= 2
continue
return b, s1, s2
def oracle_score(u: str, v: str) -> int:
def cut(w: str) -> str:
return "".join(ch * min(3, len(list(g))) for ch, g in itertools.groupby(w))
u, v = cut(u), cut(v)
if not any(u[i : i + 7] == v[j : j + 7] for i in range(len(u) - 6) for j in range(len(v) - 6)):
return 0
@lru_cache(maxsize=None)
def lcs(i: int, j: int) -> int:
if i == len(u) or j == len(v):
return 0
if u[i] == v[j]:
return 1 + lcs(i + 1, j + 1)
return max(lcs(i + 1, j), lcs(i, j + 1))
return (200 * lcs(0, 0)) // (len(u) + len(v))
def random_bytes(rng: random.Random, n: int, alphabet: int = 256) -> bytes:
return bytes(rng.randrange(alphabet) for _ in range(n))
def test_rolling_hash_depends_on_last_seven_bytes() -> None:
rng = random.Random(1)
for _ in range(500):
x = random_bytes(rng, rng.randrange(1, 40))
r = RollingHash()
for p, c in enumerate(x):
assert r.roll(c) == rolling_closed_form(x, p)
def test_signature_matches_oracle() -> None:
rng = random.Random(2)
for t in range(500):
n = rng.randrange(0, 300) if t < 480 else rng.randrange(300, 2500)
x = random_bytes(rng, n, rng.choice([2, 4, 256]))
assert ctph_signature(x) == oracle_signature(x)
def test_signature_lengths_and_block_size() -> None:
rng = random.Random(3)
for n in (0, 1, 100, 191, 192, 193, 5000, 24000):
b, s1, s2 = ctph_signature(random_bytes(rng, n))
assert len(s1) <= S and len(s2) <= S // 2
assert b >= B_MIN and (b // B_MIN) & (b // B_MIN - 1) == 0
assert ctph_signature(random_bytes(random.Random(4), 24000))[0] == 384
def test_score_matches_oracle() -> None:
rng = random.Random(5)
for _ in range(500):
u = "".join(rng.choice("ABCD") for _ in range(rng.randrange(0, 14)))
v = "".join(rng.choice("ABCD") for _ in range(rng.randrange(0, 14)))
if rng.random() < 0.5 and len(u) >= 7:
v = u[: rng.randrange(7, len(u) + 1)] + v
assert score(u, v) == oracle_score(u, v)
def test_identical_and_short_inputs() -> None:
x = random_bytes(random.Random(6), 3000)
sig = ctph_signature(x)
assert ctph_compare(sig, sig) == 100
tiny = b"abcde"
small = ctph_signature(tiny)
assert len(small[1]) < 7
assert ctph_compare(small, small) == 0
def test_small_edit_keeps_a_high_score() -> None:
rng = random.Random(7)
x = random_bytes(rng, 20000)
y = bytearray(x)
for p in (5000, 12000, 12001):
y[p] ^= 0xFF
y[16000:16000] = b"inserted text"
assert ctph_compare(ctph_signature(x), ctph_signature(bytes(y))) >= 80
def test_unrelated_inputs_score_zero() -> None:
rng = random.Random(8)
for _ in range(5):
x, y = random_bytes(rng, 8000), random_bytes(rng, 8000)
assert ctph_compare(ctph_signature(x), ctph_signature(y)) == 0
def test_neighbouring_block_sizes_are_compared() -> None:
rng = random.Random(9)
x = random_bytes(rng, 10000)
y = x + random_bytes(rng, 4000)
a, b = ctph_signature(x), ctph_signature(y)
assert b[0] == 2 * a[0]
assert ctph_compare(a, b) == score(a[2], b[1]) > 0
assert ctph_compare(a, (8 * a[0], a[1], a[2])) == 0
History
Andrew Tridgell wrote the rolling checksum of rsync, and then spamsum, which cut e-mail text at points chosen by a rolling hash and compared the strings of piece hashes to find similar spam. Jesse Kornblum adapted spamsum to files of any size and published it as context triggered piecewise hashing at DFRWS 2006, with the tool ssdeep. Breitinger and Baier (2011) described the algorithm in detail, made it faster, and discussed how an active adversary can change a signature. Later work in digital forensics compared ssdeep with other similarity digests.
References
- Jesse Kornblum, Identifying almost identical files using context triggered piecewise hashing, Digital Investigation 3S (DFRWS 2006), pages S91 to S97, the whole paper (the rolling hash, the trigger rule, the block size, the two signatures and the comparison)
- Frank Breitinger and Harald Baier, Performance issues about context-triggered piecewise hashing, ICDF2C 2011, Springer LNICST, 2012, Section 3.2 (Algorithm 1, the rolling hash; the trigger rule; the block size formula and the halving rule) and Section 4
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag PW1KEF Edit this page History Report an error Markdown