1 # -*- coding: utf-8 ; test-case-name: bridgedb.test.test_parse_fingerprint ; -*-
2 #_____________________________________________________________________________
4 # This file is part of BridgeDB, a Tor bridge distribution system.
6 # :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@torproject.org>
7 # please also see AUTHORS file
8 # :copyright: (c) 2007-2017, The Tor Project, Inc.
9 # (c) 2014-2017, Isis Lovecruft
10 # :license: see LICENSE for licensing information
11 #_____________________________________________________________________________
14 .. py:module:: bridgedb.parse.fingerprint
15 :synopsis: Parsers for Tor Bridge fingerprints.
17 .. todo: This module is very small; it could possibly be combined with another
18 module, e.g. :mod:`bridgedb.parse.descriptors`.
20 bridgedb.parse.fingerprint
21 ============================
23 Utility functions for converting between various relay fingerprint formats,
24 and checking their validity.
28 toHex - Convert a fingerprint from its binary representation to hexadecimal.
29 fromHex - Convert a fingerprint from hexadecimal to binary.
30 isValidFingerprint - Validate a fingerprint.
38 #: The required length for hexidecimal representations of hash digest of a
39 #: Tor relay's public identity key (a.k.a. its fingerprint).
40 HEX_FINGERPRINT_LEN
= 40
43 #: (callable) Convert a value from binary to hexidecimal representation.
44 toHex
= binascii
.b2a_hex
46 #: (callable) Convert a value from hexidecimal to binary representation.
47 fromHex
= binascii
.a2b_hex
49 def isValidFingerprint(fingerprint
):
50 """Determine if a Tor relay fingerprint is valid.
52 :param str fingerprint: The hex-encoded hash digest of the relay's
53 public identity key, a.k.a. its fingerprint.
55 :returns: ``True`` if the **fingerprint** was valid, ``False`` otherwise.
58 if len(fingerprint
) != HEX_FINGERPRINT_LEN
:
59 raise ValueError("Fingerprint has incorrect length: %r"
62 except (TypeError, ValueError):
63 logging
.debug("Invalid hex fingerprint: %r" % repr(fingerprint
))