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

[tor-commits] [metrics-lib/master] Calculate descriptor digests.



commit 8a1dce943a3b5ae791cba66dc3081fa633cee20f
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Thu Mar 22 15:43:12 2012 +0100

    Calculate descriptor digests.
---
 .../torproject/descriptor/ExtraInfoDescriptor.java |    4 ++
 .../torproject/descriptor/ServerDescriptor.java    |    4 ++
 .../descriptor/impl/ExtraInfoDescriptorImpl.java   |   30 ++++++++++++++++++++
 .../descriptor/impl/ServerDescriptorImpl.java      |   30 ++++++++++++++++++++
 4 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/src/org/torproject/descriptor/ExtraInfoDescriptor.java b/src/org/torproject/descriptor/ExtraInfoDescriptor.java
index 18a5b97..229b075 100644
--- a/src/org/torproject/descriptor/ExtraInfoDescriptor.java
+++ b/src/org/torproject/descriptor/ExtraInfoDescriptor.java
@@ -7,6 +7,10 @@ import java.util.SortedMap;
 
 public interface ExtraInfoDescriptor extends Descriptor {
 
+  /* Return the descriptor digest that is used to reference this
+   * extra-info descriptor in a server descriptor. */
+  public String getExtraInfoDigest();
+
   /* Return the relay's nickname. */
   public String getNickname();
 
diff --git a/src/org/torproject/descriptor/ServerDescriptor.java b/src/org/torproject/descriptor/ServerDescriptor.java
index 1fe1f21..d641d49 100644
--- a/src/org/torproject/descriptor/ServerDescriptor.java
+++ b/src/org/torproject/descriptor/ServerDescriptor.java
@@ -7,6 +7,10 @@ import java.util.List;
 /* Contains a relay server descriptor. */
 public interface ServerDescriptor extends Descriptor {
 
+  /* Return the descriptor digest that is used to reference this server
+   * descriptor in a network status. */
+  public String getServerDescriptorDigest();
+
   /* Return the relay's nickname. */
   public String getNickname();
 
diff --git a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
index 7f2194e..a198d70 100644
--- a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
@@ -5,6 +5,7 @@ package org.torproject.descriptor.impl;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -13,6 +14,7 @@ import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import org.apache.commons.codec.digest.DigestUtils;
 import org.torproject.descriptor.BandwidthHistory;
 import org.torproject.descriptor.ExtraInfoDescriptor;
 
@@ -41,6 +43,7 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl
       throws DescriptorParseException {
     super(descriptorBytes, failUnrecognizedDescriptorLines);
     this.parseDescriptorBytes();
+    this.calculateDigest();
     Set<String> exactlyOnceKeywords = new HashSet<String>(Arrays.asList((
         "extra-info,published").split(",")));
     this.checkExactlyOnceKeywords(exactlyOnceKeywords);
@@ -492,6 +495,33 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl
     /* Not parsing crypto parts (yet). */
   }
 
+  private void calculateDigest() throws DescriptorParseException {
+    try {
+      String ascii = new String(this.getRawDescriptorBytes(), "US-ASCII");
+      String startToken = "router ";
+      String sigToken = "\nrouter-signature\n";
+      int start = ascii.indexOf(startToken);
+      int sig = ascii.indexOf(sigToken) + sigToken.length();
+      if (start >= 0 || sig >= 0 || sig > start) {
+        byte[] forDigest = new byte[sig - start];
+        System.arraycopy(this.getRawDescriptorBytes(), start,
+            forDigest, 0, sig - start);
+        this.extraInfoDigest = DigestUtils.shaHex(forDigest);
+      }
+    } catch (UnsupportedEncodingException e) {
+      /* Handle below. */
+    }
+    if (this.extraInfoDigest == null) {
+      throw new DescriptorParseException("Could not calculate extra-info "
+          + "descriptor digest.");
+    }
+  }
+
+  private String extraInfoDigest;
+  public String getExtraInfoDigest() {
+    return this.extraInfoDigest;
+  }
+
   private String nickname;
   public String getNickname() {
     return this.nickname;
diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index 67b4d09..200b8b5 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -5,12 +5,14 @@ package org.torproject.descriptor.impl;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.commons.codec.digest.DigestUtils;
 import org.torproject.descriptor.BandwidthHistory;
 import org.torproject.descriptor.ServerDescriptor;
 
@@ -45,6 +47,7 @@ public class ServerDescriptorImpl extends DescriptorImpl
       throws DescriptorParseException {
     super(descriptorBytes, failUnrecognizedDescriptorLines);
     this.parseDescriptorBytes();
+    this.calculateDigest();
     Set<String> exactlyOnceKeywords = new HashSet<String>(Arrays.asList(
         "router,bandwidth,published".split(",")));
     this.checkExactlyOnceKeywords(exactlyOnceKeywords);
@@ -424,6 +427,33 @@ public class ServerDescriptorImpl extends DescriptorImpl
     this.allowSingleHopExits = true;
   }
 
+  private void calculateDigest() throws DescriptorParseException {
+    try {
+      String ascii = new String(this.getRawDescriptorBytes(), "US-ASCII");
+      String startToken = "router ";
+      String sigToken = "\nrouter-signature\n";
+      int start = ascii.indexOf(startToken);
+      int sig = ascii.indexOf(sigToken) + sigToken.length();
+      if (start >= 0 || sig >= 0 || sig > start) {
+        byte[] forDigest = new byte[sig - start];
+        System.arraycopy(this.getRawDescriptorBytes(), start,
+            forDigest, 0, sig - start);
+        this.serverDescriptorDigest = DigestUtils.shaHex(forDigest);
+      }
+    } catch (UnsupportedEncodingException e) {
+      /* Handle below. */
+    }
+    if (this.serverDescriptorDigest == null) {
+      throw new DescriptorParseException("Could not calculate server "
+          + "descriptor digest.");
+    }
+  }
+
+  private String serverDescriptorDigest;
+  public String getServerDescriptorDigest() {
+    return this.serverDescriptorDigest;
+  }
+
   private String nickname;
   public String getNickname() {
     return this.nickname;

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