1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import base64
17 import io
18 import struct
19
20 import dns.exception
21 import dns.rdata
22 import dns.util
23
25 """NSEC3PARAM record
26
27 @ivar algorithm: the hash algorithm number
28 @type algorithm: int
29 @ivar flags: the flags
30 @type flags: int
31 @ivar iterations: the number of iterations
32 @type iterations: int
33 @ivar salt: the salt
34 @type salt: string"""
35
36 __slots__ = ['algorithm', 'flags', 'iterations', 'salt']
37
38 - def __init__(self, rdclass, rdtype, algorithm, flags, iterations, salt):
39 super(NSEC3PARAM, self).__init__(rdclass, rdtype)
40 self.algorithm = algorithm
41 self.flags = flags
42 self.iterations = iterations
43 self.salt = salt
44
45 - def to_text(self, origin=None, relativize=True, **kw):
46 if self.salt == b'':
47 salt = '-'
48 else:
49 salt = base64.b16encode(self.salt).decode('ascii').lower()
50 return '%u %u %u %s' % (self.algorithm, self.flags, self.iterations, salt)
51
52 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
53 algorithm = tok.get_uint8()
54 flags = tok.get_uint8()
55 iterations = tok.get_uint16()
56 salt = tok.get_string()
57 if salt == '-':
58 salt = b''
59 else:
60 salt = bytes.fromhex(salt)
61 return cls(rdclass, rdtype, algorithm, flags, iterations, salt)
62
63 from_text = classmethod(from_text)
64
65 - def to_wire(self, file, compress = None, origin = None):
66 l = len(self.salt)
67 file.write(struct.pack("!BBHB", self.algorithm, self.flags,
68 self.iterations, l))
69 file.write(self.salt)
70
71 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
72 (algorithm, flags, iterations, slen) = struct.unpack('!BBHB',
73 wire[current : current + 5])
74 current += 5
75 rdlen -= 5
76 salt = wire[current : current + slen]
77 current += slen
78 rdlen -= slen
79 if rdlen != 0:
80 raise dns.exception.FormError
81 return cls(rdclass, rdtype, algorithm, flags, iterations, salt)
82
83 from_wire = classmethod(from_wire)
84
85 - def _cmp(self, other):
86 b1 = io.BytesIO()
87 self.to_wire(b1)
88 b2 = io.BytesIO()
89 other.to_wire(b2)
90 return dns.util.cmp(b1.getvalue(), b2.getvalue())
91