[tor-commits] [metrics-lib/release] Escalate NoSuchAlgorithmExceptions.

karsten at torproject.org karsten at torproject.org
Fri Jun 16 15:30:39 UTC 2017


commit 1ac254697b21ac297a2e3ef151fcf0bc22c53354
Author: iwakeh <iwakeh at torproject.org>
Date:   Thu Jun 8 08:54:06 2017 +0000

    Escalate NoSuchAlgorithmExceptions.
---
 .../torproject/descriptor/impl/DescriptorImpl.java | 30 ++++++++++------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
index 79905c0..52ff6d4 100644
--- a/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/DescriptorImpl.java
@@ -364,14 +364,8 @@ public abstract class DescriptorImpl implements Descriptor {
         end = ascii.indexOf(endToken) + endToken.length();
       }
       if (start >= 0 && end >= 0 && end > start) {
-        try {
-          MessageDigest md = MessageDigest.getInstance("SHA-1");
-          md.update(this.rawDescriptorBytes, this.offset + start, end - start);
-          this.digestSha1Hex = DatatypeConverter.printHexBinary(md.digest())
-              .toLowerCase();
-        } catch (NoSuchAlgorithmException e) {
-          /* Handle below. */
-        }
+        this.digestSha1Hex = DatatypeConverter.printHexBinary(
+            messageDigest("SHA-1", start, end)).toLowerCase();
       }
     }
     if (null == this.digestSha1Hex) {
@@ -380,6 +374,16 @@ public abstract class DescriptorImpl implements Descriptor {
     }
   }
 
+  private byte[] messageDigest(String alg, int start, int end) {
+    try {
+      MessageDigest md = MessageDigest.getInstance(alg);
+      md.update(this.rawDescriptorBytes, this.offset + start, end - start);
+      return md.digest();
+    } catch (NoSuchAlgorithmException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   public String getDigestSha1Hex() {
     return this.digestSha1Hex;
   }
@@ -403,14 +407,8 @@ public abstract class DescriptorImpl implements Descriptor {
         end = ascii.indexOf(endToken) + endToken.length();
       }
       if (start >= 0 && end >= 0 && end > start) {
-        try {
-          MessageDigest md = MessageDigest.getInstance("SHA-256");
-          md.update(this.rawDescriptorBytes, this.offset + start, end - start);
-          this.digestSha256Base64 = DatatypeConverter.printBase64Binary(
-              md.digest()).replaceAll("=", "");
-        } catch (NoSuchAlgorithmException e) {
-          /* Handle below. */
-        }
+        this.digestSha256Base64 = DatatypeConverter.printBase64Binary(
+            messageDigest("SHA-256", start, end)).replaceAll("=", "");
       }
     }
     if (null == this.digestSha256Base64) {





More information about the tor-commits mailing list