signature.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2014-2015 UAVCAN Development Team <uavcan.org>
3 #
4 # This software is distributed under the terms of the MIT License.
5 #
6 # Author: Pavel Kirienko <pavel.kirienko@zubax.com>
7 # Ben Dyer <ben_dyer@mac.com>
8 #
9 
10 from __future__ import division, absolute_import, print_function, unicode_literals
11 
12 #
13 # CRC-64-WE
14 # Description: http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat-bits.64
15 # Initial value: 0xFFFFFFFFFFFFFFFF
16 # Poly: 0x42F0E1EBA9EA3693
17 # Reverse: no
18 # Output xor: 0xFFFFFFFFFFFFFFFF
19 # Check: 0x62EC59E3F1A4F00A
20 #
21 
22 
23 class Signature:
24  '''
25  This class implements the UAVCAN DSDL signature hash function. Please refer to the specification for details.
26  '''
27  MASK64 = 0xFFFFFFFFFFFFFFFF
28  POLY = 0x42F0E1EBA9EA3693
29 
30  def __init__(self, extend_from=None):
31  '''
32  extend_from Initial value (optional)
33  '''
34  if extend_from is not None:
35  self._crc = (int(extend_from) & Signature.MASK64) ^ Signature.MASK64
36  else:
37  self._crc = Signature.MASK64
38 
39  def add(self, data_bytes):
40  '''Feed ASCII string or bytes to the signature function'''
41  try:
42  if isinstance(data_bytes, basestring): # Python 2.7 compatibility
43  data_bytes = map(ord, data_bytes)
44  except NameError:
45  if isinstance(data_bytes, str): # This branch will be taken on Python 3
46  data_bytes = map(ord, data_bytes)
47 
48  for b in data_bytes:
49  self._crc ^= (b << 56) & Signature.MASK64
50  for _ in range(8):
51  if self._crc & (1 << 63):
52  self._crc = ((self._crc << 1) & Signature.MASK64) ^ Signature.POLY
53  else:
54  self._crc <<= 1
55 
56  def get_value(self):
57  '''Returns integer signature value'''
58  return (self._crc & Signature.MASK64) ^ Signature.MASK64
59 
60 
62  '''
63  One-shot signature computation for ASCII string or bytes.
64  Returns integer signture value.
65  '''
66  s = Signature()
67  s.add(data)
68  return s.get_value()
69 
70 
71 # if __name__ == '__main__':
72 if 1:
73  s = Signature()
74  s.add(b'123')
75  s.add('456789')
76  assert s.get_value() == 0x62EC59E3F1A4F00A
pyuavcan_v0.dsdl.signature.Signature.add
def add(self, data_bytes)
Definition: signature.py:39
pyuavcan_v0.dsdl.signature.Signature._crc
_crc
Definition: signature.py:35
pyuavcan_v0.dsdl.signature.compute_signature
def compute_signature(data)
Definition: signature.py:61
int
int
Definition: libstubs.cpp:120
pyuavcan_v0.dsdl.signature.Signature.get_value
def get_value(self)
Definition: signature.py:56
pyuavcan_v0.dsdl.signature.Signature.__init__
def __init__(self, extend_from=None)
Definition: signature.py:30
pyuavcan_v0.dsdl.signature.Signature
Definition: signature.py:23


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03