[tor-commits] [stem/master] Lazy load microdescriptor digest

atagar at torproject.org atagar at torproject.org
Sun Jan 25 22:37:34 UTC 2015


commit b93ea3928b9e4f032f5f8d5c7400bc3932f39b8d
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 25 11:17:47 2015 -0800

    Lazy load microdescriptor digest
    
    Not truly a line attribute, but we can treat it in a similar fashion.
---
 stem/descriptor/microdescriptor.py |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py
index 5b8d0a3..c9389e4 100644
--- a/stem/descriptor/microdescriptor.py
+++ b/stem/descriptor/microdescriptor.py
@@ -169,6 +169,7 @@ def _parse_id_line(descriptor, entries):
     raise ValueError("'id' lines should contain both the key type and digest: id %s" % value)
 
 
+_parse_digest = lambda descriptor, entries: setattr(descriptor, 'digest', hashlib.sha256(descriptor.get_bytes()).hexdigest().upper())
 _parse_onion_key_line = _parse_key_block('onion-key', 'onion_key', 'RSA PUBLIC KEY')
 _parse_ntor_onion_key_line = _parse_simple_line('ntor-onion-key', 'ntor_onion_key')
 _parse_family_line = lambda descriptor, entries: setattr(descriptor, 'family', _value('family', entries).split(' '))
@@ -206,6 +207,7 @@ class Microdescriptor(Descriptor):
     'exit_policy_v6': (None, _parse_p6_line),
     'identifier_type': (None, _parse_id_line),
     'identifier': (None, _parse_id_line),
+    'digest': (None, _parse_digest),
   }
 
   PARSER_FOR_LINE = {
@@ -220,13 +222,11 @@ class Microdescriptor(Descriptor):
 
   def __init__(self, raw_contents, validate = True, annotations = None):
     super(Microdescriptor, self).__init__(raw_contents, lazy_load = not validate)
-
-    self.digest = hashlib.sha256(self.get_bytes()).hexdigest().upper()
     self._annotation_lines = annotations if annotations else []
-
     entries = _get_descriptor_components(raw_contents, validate)
 
     if validate:
+      self.digest = hashlib.sha256(self.get_bytes()).hexdigest().upper()
       self._parse(entries, validate)
       self._check_constraints(entries)
     else:





More information about the tor-commits mailing list