]> git.proxmox.com Git - mirror_frr.git/blame - python/clippy/uidhash.py
python: apply black formatting
[mirror_frr.git] / python / clippy / uidhash.py
CommitLineData
36a8fdfd
DL
1# xref unique ID hash calculation
2#
3# Copyright (C) 2020 David Lamparter for NetDEF, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; either version 2 of the License, or (at your option)
8# any later version.
9#
10# This program is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13# more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; see the file COPYING; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19import struct
20from hashlib import sha256
21
00f0c399
DL
22
23def bititer(data, bits, startbit=True):
24 """
36a8fdfd
DL
25 just iterate the individual bits out from a bytes object
26
27 if startbit is True, an '1' bit is inserted at the very beginning
28 goes <bits> at a time, starts at LSB.
00f0c399 29 """
36a8fdfd
DL
30 bitavail, v = 0, 0
31 if startbit and len(data) > 0:
32 v = data.pop(0)
33 yield (v & ((1 << bits) - 1)) | (1 << (bits - 1))
34 bitavail = 9 - bits
35 v >>= bits - 1
36
37 while len(data) > 0:
38 while bitavail < bits:
39 v |= data.pop(0) << bitavail
40 bitavail += 8
41 yield v & ((1 << bits) - 1)
42 bitavail -= bits
43 v >>= bits
44
00f0c399 45
36a8fdfd 46def base32c(data):
00f0c399 47 """
36a8fdfd 48 Crockford base32 with extra dashes
00f0c399 49 """
36a8fdfd 50 chs = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
00f0c399 51 o = ""
36a8fdfd
DL
52 if type(data) == str:
53 data = [ord(v) for v in data]
54 else:
55 data = list(data)
56 for i, bits in enumerate(bititer(data, 5)):
57 if i == 5:
00f0c399 58 o = o + "-"
36a8fdfd
DL
59 elif i == 10:
60 break
61 o = o + chs[bits]
62 return o
63
00f0c399 64
36a8fdfd 65def uidhash(filename, hashstr, hashu32a, hashu32b):
00f0c399 66 """
36a8fdfd 67 xref Unique ID hash used in FRRouting
00f0c399
DL
68 """
69 filename = "/".join(filename.rsplit("/")[-2:])
36a8fdfd 70
00f0c399
DL
71 hdata = filename.encode("UTF-8") + hashstr.encode("UTF-8")
72 hdata += struct.pack(">II", hashu32a, hashu32b)
36a8fdfd
DL
73 i = sha256(hdata).digest()
74 return base32c(i)