[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Replace remaining _digest_for_content() usage



commit 6beaaf49df9c0960f12e443b717cbd1c46ce765c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Nov 15 12:06:27 2018 -0800

    Replace remaining _digest_for_content() usage
    
    Swapping out our remmaining _digest_for_content() usage so we can drop that
    helper.
---
 docs/change_log.rst                          |  1 +
 stem/descriptor/__init__.py                  | 25 ++++---------------------
 stem/descriptor/extrainfo_descriptor.py      |  2 +-
 stem/descriptor/hidden_service_descriptor.py |  3 ++-
 stem/descriptor/networkstatus.py             |  4 +++-
 5 files changed, 11 insertions(+), 24 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index f8db0b52..a0d89db7 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -52,6 +52,7 @@ The following are only available within Stem's `git repository
  * **Descriptors**
 
   * Added :func:`~stem.descriptor.Descriptor.type_annotation` method (:trac:`28397`)
+  * Added the **hash_type** and **encoding** arguments to `ServerDescriptor <api/descriptor/server_descriptor.html#stem.descriptor.server_descriptor.ServerDescriptor.digest>`_ and `ExtraInfo's <api/descriptor/extrainfo_descriptor.html#stem.descriptor.extrainfo_descriptor.ExtraInfoDescriptor.digest>`_ digest methods (:trac:`28398`)
   * DescriptorDownloader crashed if **use_mirrors** is set (:trac:`28393`)
   * Don't download from Serge, a bridge authority that frequently timeout
 
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 07ed5b1b..66a34f6e 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -87,7 +87,6 @@ import base64
 import codecs
 import collections
 import copy
-import hashlib
 import os
 import random
 import re
@@ -674,9 +673,9 @@ def _encode_digest(hash_value, encoding):
   if encoding == DigestEncoding.RAW:
     return hash_value
   elif encoding == DigestEncoding.HEX:
-    return hash_value.hexdigest().upper()
+    return stem.util.str_tools._to_unicode(hash_value.hexdigest().upper())
   elif encoding == DigestEncoding.BASE64:
-    return base64.b64encode(hash_value.digest()).rstrip('=')
+    return stem.util.str_tools._to_unicode(base64.b64encode(hash_value.digest()).rstrip(b'='))
   else:
     raise NotImplementedError('BUG: stem.descriptor._encode_digest should recognize all DigestEncoding, lacked %s' % encoding)
 
@@ -917,22 +916,6 @@ class Descriptor(object):
     digest_hex = codecs.encode(decrypted_bytes[seperator_index + 1:], 'hex_codec')
     return stem.util.str_tools._to_unicode(digest_hex.upper())
 
-  def _digest_for_content(self, start, end):
-    """
-    Provides the digest of our descriptor's content in a given range.
-
-    :param bytes start: start of the range to generate a digest for
-    :param bytes end: end of the range to generate a digest for
-
-    :returns: the digest string encoded in uppercase hex
-
-    :raises: ValueError if the digest canot be calculated
-    """
-
-    digest_content = self._content_range(start, end)
-    digest_hash = hashlib.sha1(stem.util.str_tools._to_bytes(digest_content))
-    return stem.util.str_tools._to_unicode(digest_hash.hexdigest().upper())
-
   def _content_range(self, start = None, end = None):
     """
     Provides the descriptor content inclusively between two substrings.
@@ -947,13 +930,13 @@ class Descriptor(object):
     start_index, end_index = None, None
 
     if start is not None:
-      start_index = content.find(start)
+      start_index = content.find(stem.util.str_tools._to_bytes(start))
 
       if start_index == -1:
         raise ValueError("'%s' is not present within our descriptor content" % start)
 
     if end is not None:
-      end_index = content.find(end, start_index)
+      end_index = content.find(stem.util.str_tools._to_bytes(end), start_index)
 
       if end_index == -1:
         raise ValueError("'%s' is not present within our descriptor content" % end)
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py
index 401b9643..ea74ea52 100644
--- a/stem/descriptor/extrainfo_descriptor.py
+++ b/stem/descriptor/extrainfo_descriptor.py
@@ -974,7 +974,7 @@ class RelayExtraInfoDescriptor(ExtraInfoDescriptor):
       #
       #   https://trac.torproject.org/projects/tor/ticket/28415
 
-      return stem.descriptor._encode_digest(hashlib.sha256(str(self)), encoding)
+      return stem.descriptor._encode_digest(hashlib.sha256(self.get_bytes()), encoding)
     else:
       raise NotImplementedError('Extrainfo descriptor digests are only available in sha1 and sha256, not %s' % hash_type)
 
diff --git a/stem/descriptor/hidden_service_descriptor.py b/stem/descriptor/hidden_service_descriptor.py
index a7cc0e3d..ad68c349 100644
--- a/stem/descriptor/hidden_service_descriptor.py
+++ b/stem/descriptor/hidden_service_descriptor.py
@@ -280,7 +280,8 @@ class HiddenServiceDescriptor(Descriptor):
 
       if not skip_crypto_validation and stem.prereq.is_crypto_available():
         signed_digest = self._digest_for_signature(self.permanent_key, self.signature)
-        content_digest = self._digest_for_content(b'rendezvous-service-descriptor ', b'\nsignature\n')
+        digest_content = self._content_range('rendezvous-service-descriptor ', '\nsignature\n')
+        content_digest = hashlib.sha1(digest_content).hexdigest().upper()
 
         if signed_digest != content_digest:
           raise ValueError('Decrypted digest does not match local digest (calculated: %s, local: %s)' % (signed_digest, content_digest))
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 63483c94..02dd55fb 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -55,6 +55,7 @@ For more information see :func:`~stem.descriptor.__init__.DocumentHandler`...
 """
 
 import collections
+import hashlib
 import io
 
 import stem.descriptor.router_status_entry
@@ -1115,7 +1116,8 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
 
     # sha1 hash of the body and header
 
-    local_digest = self._digest_for_content(b'network-status-version', b'directory-signature ')
+    digest_content = self._content_range('network-status-version', 'directory-signature ')
+    local_digest = hashlib.sha1(digest_content).hexdigest().upper()
 
     valid_digests, total_digests = 0, 0
     required_digests = len(self.signatures) / 2.0



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits