]>
git.proxmox.com Git - mirror_frr.git/blob - python/clippy/uidhash.py
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 # xref unique ID hash calculation
4 # Copyright (C) 2020 David Lamparter for NetDEF, Inc.
7 from hashlib
import sha256
10 def bititer(data
, bits
, startbit
=True):
12 just iterate the individual bits out from a bytes object
14 if startbit is True, an '1' bit is inserted at the very beginning
15 goes <bits> at a time, starts at LSB.
18 if startbit
and len(data
) > 0:
20 yield (v
& ((1 << bits
) - 1)) |
(1 << (bits
- 1))
25 while bitavail
< bits
:
26 v |
= data
.pop(0) << bitavail
28 yield v
& ((1 << bits
) - 1)
35 Crockford base32 with extra dashes
37 chs
= "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
40 data
= [ord(v
) for v
in data
]
43 for i
, bits
in enumerate(bititer(data
, 5)):
52 def uidhash(filename
, hashstr
, hashu32a
, hashu32b
):
54 xref Unique ID hash used in FRRouting
56 filename
= "/".join(filename
.rsplit("/")[-2:])
58 hdata
= filename
.encode("UTF-8") + hashstr
.encode("UTF-8")
59 hdata
+= struct
.pack(">II", hashu32a
, hashu32b
)
60 i
= sha256(hdata
).digest()