[maven-resolver] branch master updated: [MRESOLVER-158] Simplify SimpleDigest

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-resolver] branch master updated: [MRESOLVER-158] Simplify SimpleDigest

cstamas-2
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 6c418e3  [MRESOLVER-158] Simplify SimpleDigest
6c418e3 is described below

commit 6c418e32ed9c12f1deadbf3be9ab0ab67aa09217
Author: Tamas Cservenak <[hidden email]>
AuthorDate: Tue Jan 12 09:25:03 2021 +0100

    [MRESOLVER-158] Simplify SimpleDigest
---
 .../eclipse/aether/internal/impl/SimpleDigest.java | 53 ++++++++--------------
 1 file changed, 20 insertions(+), 33 deletions(-)

diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleDigest.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleDigest.java
index daf56fc..0040cf2 100644
--- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleDigest.java
+++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleDigest.java
@@ -22,6 +22,7 @@ package org.eclipse.aether.internal.impl;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
 
 /**
  * A simple digester for strings. It will traverse through a list of digest algorithms and pick the
@@ -32,26 +33,27 @@ class SimpleDigest
 
     private static final String[] HASH_ALGOS = new String[] { "SHA-1", "MD5" };
 
-    private MessageDigest digest;
-
-    private long hash;
+    private final MessageDigest digest;
 
     SimpleDigest()
     {
+        MessageDigest md = null;
         for ( String hashAlgo : HASH_ALGOS )
         {
             try
             {
-                digest = MessageDigest.getInstance( hashAlgo );
-                hash = 0;
+                md = MessageDigest.getInstance( hashAlgo );
                 break;
             }
             catch ( NoSuchAlgorithmException ne )
             {
-                digest = null;
-                hash = 13;
             }
         }
+        if ( md == null )
+        {
+            throw new IllegalStateException( "Not supported digests: " + Arrays.toString( HASH_ALGOS ) );
+        }
+        this.digest = md;
     }
 
     public void update( String data )
@@ -60,42 +62,27 @@ class SimpleDigest
         {
             return;
         }
-        if ( digest != null )
-        {
-            digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
-        }
-        else
-        {
-            hash = hash * 31 + data.hashCode();
-        }
+        digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
     }
 
     @SuppressWarnings( "checkstyle:magicnumber" )
     public String digest()
     {
-        if ( digest != null )
+        StringBuilder buffer = new StringBuilder( 64 );
+
+        byte[] bytes = digest.digest();
+        for ( byte aByte : bytes )
         {
-            StringBuilder buffer = new StringBuilder( 64 );
+            int b = aByte & 0xFF;
 
-            byte[] bytes = digest.digest();
-            for ( byte aByte : bytes )
+            if ( b < 0x10 )
             {
-                int b = aByte & 0xFF;
-
-                if ( b < 0x10 )
-                {
-                    buffer.append( '0' );
-                }
-
-                buffer.append( Integer.toHexString( b ) );
+                buffer.append( '0' );
             }
 
-            return buffer.toString();
+            buffer.append( Integer.toHexString( b ) );
         }
-        else
-        {
-            return Long.toHexString( hash );
-        }
-    }
 
+        return buffer.toString();
+    }
 }