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

[tor-commits] [metrics-lib/master] Provide microdescriptor digest in hex encoding.



commit 664921eb50491a774a5281bf1c12836ab7dedd94
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Fri Dec 11 10:22:39 2020 +0100

    Provide microdescriptor digest in hex encoding.
---
 CHANGELOG.md                                             |  3 +++
 .../java/org/torproject/descriptor/Microdescriptor.java  |  9 +++++++++
 .../torproject/descriptor/impl/MicrodescriptorImpl.java  | 16 ++++++++++++++++
 .../descriptor/impl/MicrodescriptorImplTest.java         |  3 +++
 4 files changed, 31 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 828718d..bee22c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@
  * Medium changes
    - Optimize parsing of large files containing many descriptors.
 
+ * Minor changes
+   - Provide microdescriptor SHA-256 digest in hexadecimal encoding.
+
 
 # Changes in version 2.14.0 - 2020-08-07
 
diff --git a/src/main/java/org/torproject/descriptor/Microdescriptor.java b/src/main/java/org/torproject/descriptor/Microdescriptor.java
index feaf00b..0d329b7 100644
--- a/src/main/java/org/torproject/descriptor/Microdescriptor.java
+++ b/src/main/java/org/torproject/descriptor/Microdescriptor.java
@@ -32,6 +32,15 @@ public interface Microdescriptor extends Descriptor {
    */
   String getDigestSha256Base64();
 
+  /**
+   * Return the SHA-256 descriptor digest, encoded as 64 lower-case hexadecimal
+   * characters, that can be used as file name when writing this descriptor to
+   * disk.
+   *
+   * @since 2.15.0
+   */
+  String getDigestSha256Hex();
+
   /**
    * Return the RSA-1024 public key in PEM format used to encrypt CREATE
    * cells for this server, or null if the descriptor doesn't contain an
diff --git a/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java
index 87ab7ae..8d4ac1b 100644
--- a/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/MicrodescriptorImpl.java
@@ -6,6 +6,9 @@ package org.torproject.descriptor.impl;
 import org.torproject.descriptor.DescriptorParseException;
 import org.torproject.descriptor.Microdescriptor;
 
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.binary.Hex;
+
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -26,6 +29,7 @@ public class MicrodescriptorImpl extends DescriptorImpl
     super(descriptorBytes, offsetAndLength, descriptorFile, false);
     this.parseDescriptorBytes();
     this.calculateDigestSha256Base64(Key.ONION_KEY.keyword + NL);
+    this.convertDigestSha256Base64ToHex();
     this.checkExactlyOnceKeys(EnumSet.of(Key.ONION_KEY));
     Set<Key> atMostOnceKeys = EnumSet.of(
         Key.NTOR_ONION_KEY, Key.FAMILY, Key.P, Key.P6, Key.ID);
@@ -212,6 +216,18 @@ public class MicrodescriptorImpl extends DescriptorImpl
     }
   }
 
+  private void convertDigestSha256Base64ToHex() {
+    this.digestSha256Hex = Hex.encodeHexString(Base64.decodeBase64(
+        this.getDigestSha256Base64()));
+  }
+
+  private String digestSha256Hex;
+
+  @Override
+  public String getDigestSha256Hex() {
+    return this.digestSha256Hex;
+  }
+
   private String onionKey;
 
   @Override
diff --git a/src/test/java/org/torproject/descriptor/impl/MicrodescriptorImplTest.java b/src/test/java/org/torproject/descriptor/impl/MicrodescriptorImplTest.java
index 128d39a..fbc2fc9 100644
--- a/src/test/java/org/torproject/descriptor/impl/MicrodescriptorImplTest.java
+++ b/src/test/java/org/torproject/descriptor/impl/MicrodescriptorImplTest.java
@@ -74,6 +74,9 @@ public class MicrodescriptorImplTest {
     Microdescriptor micro = DescriptorBuilder.createWithDefaultLines();
     assertEquals("ER1AC4KqT//o3pJDrqlmej5G2qW1EQYEr/IrMQHNc6I",
         micro.getDigestSha256Base64());
+    assertEquals(
+        "111d400b82aa4fffe8de9243aea9667a3e46daa5b5110604aff22b3101cd73a2",
+        micro.getDigestSha256Hex());
   }
 
   @Test



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