Skip to content

Commit 2a270ce

Browse files
committed
Generate correct VCIDs
Reference: #1579 Reported-by: tdruez <[email protected]> Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 65d0e17 commit 2a270ce

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

aboutcode/hashid/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@
4545
def build_vcid(prefix="VCID"):
4646
"""
4747
Return a new Vulnerable Code ID (aka. VCID) which is a strongly unique vulnerability
48-
identifierstring using the provided ``prefix``. A VCID is composed of a four letter prefix, and
48+
identifier string using the provided ``prefix``. A VCID is composed of a four letter prefix, and
4949
three segments composed of four letters and dihits each separated by a dash.
50-
5150
For example::
5251
>>> import re
5352
>>> vcid = build_vcid()
5453
>>> assert re.match('VCID(-[a-hjkm-z1-9]{4}){3}', vcid), vcid
54+
55+
We were mistakenly not using enough bits. The symptom was that the last
56+
segment of the VCID was always strting with "aaa" This ensure we are now OK:
57+
>>> vcids = [build_vcid() for _ in range(50)]
58+
>>> assert not any(vid.split("-")[-1].startswith("aaa") for vid in vcids)
5559
"""
56-
# we keep only 64 bits (e.g. 8 bytes)
57-
uid = sha256(uuid4().bytes).digest()[:8]
58-
# we keep only 12 encoded bytes (which corresponds to 60 bits)
60+
uid = uuid4().bytes
61+
# we keep three segments of 4 base32-encodee bytes, 3*4=12
62+
# which corresponds to 60 bits
63+
# becausee each base32 byte can store 5 bits (2**5 = 32)
5964
uid = base32_custom(uid)[:12].decode("utf-8").lower()
6065
return f"{prefix}-{uid[:4]}-{uid[4:8]}-{uid[8:12]}"
6166

0 commit comments

Comments
 (0)