From 4d955f3be5634ca8b44b9283ae4b16fc65a865b3 Mon Sep 17 00:00:00 2001
From: dev <dev@welterde.de>
Date: Sun, 22 Jun 2008 14:07:30 +0000
Subject: [PATCH] minor optimization in I2PDatagramDissector(only verfy
 signature once)

---
 .../client/datagram/I2PDatagramDissector.java | 33 +++++++++++++------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/core/java/src/net/i2p/client/datagram/I2PDatagramDissector.java b/core/java/src/net/i2p/client/datagram/I2PDatagramDissector.java
index 2a314b959b..d456f9ab5e 100644
--- a/core/java/src/net/i2p/client/datagram/I2PDatagramDissector.java
+++ b/core/java/src/net/i2p/client/datagram/I2PDatagramDissector.java
@@ -42,6 +42,8 @@ public final class I2PDatagramDissector {
     private byte[] rxPayload = new byte[DGRAM_BUFSIZE];
 
     private int rxPayloadLen = 0;
+    
+    private boolean valid = false;
 
     /**
      * Crate a new I2P repliable datagram dissector.
@@ -89,11 +91,8 @@ public final class I2PDatagramDissector {
      * @throws I2PInvalidDatagramException if the signature verification fails
      */
     public byte[] getPayload() throws I2PInvalidDatagramException {
-        if (!dsaEng.verifySignature(rxSign, rxHashBytes,
-                                    rxDest.getSigningPublicKey())) {
-            throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature");
-        }
-
+        this.verifySignature();
+        
         byte[] retPayload = new byte[rxPayloadLen];
         System.arraycopy(rxPayload, 0, retPayload, 0, rxPayloadLen);
 
@@ -109,11 +108,8 @@ public final class I2PDatagramDissector {
      * @throws I2PInvalidDatagramException if the signature verification fails
      */
     public Destination getSender() throws I2PInvalidDatagramException {
-        if (!dsaEng.verifySignature(rxSign, rxHashBytes,
-                                    rxDest.getSigningPublicKey())) {
-            throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature");
-        }
-
+        this.verifySignature();
+        
         Destination retDest = new Destination();
         try {
             retDest.fromByteArray(rxDest.toByteArray());
@@ -156,4 +152,21 @@ public final class I2PDatagramDissector {
         
         return retDest;
     }
+    
+    /**
+     * Verify the signature of this datagram (previously loaded with the
+     * loadI2PDatagram() method)
+     */
+    public void verifySignature() throws I2PInvalidDatagramException {
+        // first check if it already got validated
+        if(this.valid)
+            return;
+        
+        // now validate
+        if (!dsaEng.verifySignature(rxSign, rxHashBytes, rxDest.getSigningPublicKey()))
+            throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature");
+        
+        // set validated
+        this.valid = true;
+    }
 }
-- 
GitLab