[tor-commits] [stem/master] Constants for padding attributes

atagar at torproject.org atagar at torproject.org
Tue Jun 20 16:17:12 UTC 2017


commit 2ae8ea381830c466b54b7b383f8fe232406094e8
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jun 19 12:10:58 2017 -0700

    Constants for padding attributes
    
    Bit more readable this way. Also, we can drop the lower() from the hexdigest
    since it already is.
---
 stem/descriptor/server_descriptor.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 724684d..03008ac 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -76,6 +76,10 @@ except ImportError:
 
 SigningKey = collections.namedtuple('SigningKey', ['public', 'private', 'descriptor_signing_key'])
 
+DIGEST_TYPE_INFO = b'\x00\x01'
+DIGEST_PADDING = b'\xFF'
+DIGEST_SEPARATOR = b'\x00'
+
 # relay descriptors must have exactly one of the following
 REQUIRED_FIELDS = (
   'router',
@@ -249,8 +253,8 @@ def _generate_signature(content, signing_key):
 
   # generate the digest with required PKCS1 padding so it's 128 bytes
 
-  digest = hashlib.sha1(content).hexdigest().lower().decode('hex_codec')
-  digest = b'\x00\x01' + (b'\xFF' * (125 - len(digest))) + b'\x00' + digest
+  digest = hashlib.sha1(content).hexdigest().decode('hex_codec')
+  digest = DIGEST_TYPE_INFO + (DIGEST_PADDING * (125 - len(digest))) + DIGEST_SEPARATOR + digest
 
   padding = padding.PSS(mgf = padding.MGF1(hashes.SHA256()), salt_length = padding.PSS.MAX_LENGTH)
   return base64.b64encode(signing_key.private.sign(digest, padding, hashes.SHA256()))





More information about the tor-commits mailing list