From 951c9976930489e4232a9c431e73573a5e17b4da Mon Sep 17 00:00:00 2001 From: "guillaume.lambert" Date: Fri, 6 Nov 2020 21:36:08 +0100 Subject: [PATCH] create a FnvUtils class in common mapping This way, all the various PortMapping versions now call the same FNV-1 64 bit function. The related common code is extracted from PortMapping classes and mutualized in FnvUtils. JIRA: TRNSPRTPCE-352 Signed-off-by: guillaume.lambert Change-Id: I1562ed2b919cb784f4c1b833af946568f0ac75e1 --- .../transportpce/common/mapping/FnvUtils.java | 46 +++++++++++++++++++ .../common/mapping/PortMappingVersion121.java | 31 +------------ .../common/mapping/PortMappingVersion221.java | 29 +----------- .../common/mapping/PortMappingVersion710.java | 30 +----------- 4 files changed, 49 insertions(+), 87 deletions(-) create mode 100644 common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java new file mode 100644 index 000000000..1076b0add --- /dev/null +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java @@ -0,0 +1,46 @@ +/* + * Copyright © 2020 Orange, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.transportpce.common.mapping; + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public final class FnvUtils { + + //FNV1 64 bit hash constants + private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16); + private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16); + private static final BigInteger FNV_MOD = new BigInteger("2").pow(64); + + /** + * Implements the FNV-1 64bit algorithm. + * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding. + * Otherwise, the hash cannot be stored in a UTF-8 string. + * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash + * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java + * @param stringdata the String to be hashed + * @return the hash string + */ + protected static String fnv1_64(String stringdata) { + BigInteger hash = FNV_INIT; + byte[] data = stringdata.getBytes(StandardCharsets.UTF_8); + + for (byte b : data) { + hash = hash.multiply(FNV_PRIME).mod(FNV_MOD); + hash = hash.xor(BigInteger.valueOf((int) b & 0xff)); + } + + return Base64.getEncoder().encodeToString(hash.toByteArray()); + } + + private FnvUtils() { + //Noop - should not be called + } +} diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java index 382d96149..d7def4d7a 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java @@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping; import com.google.common.util.concurrent.FluentFuture; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -90,11 +87,6 @@ public class PortMappingVersion121 { private final DeviceTransactionManager deviceTransactionManager; private final OpenRoadmInterfaces openRoadmInterfaces; - //FNV1 64 bit hash constants - private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16); - private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16); - private static final BigInteger FNV_MOD = new BigInteger("2").pow(64); - public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager, OpenRoadmInterfaces openRoadmInterfaces) { this.dataBroker = dataBroker; @@ -699,7 +691,7 @@ public class PortMappingVersion121 { .setSupportingCircuitPackName(circuitPackName) .setSupportingPort(port.getPortName()) .setPortDirection(port.getPortDirection().getName()) - .setLcpHashVal(fnv(nodeIdLcp)); + .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp)); if (port.getPortQual() != null) { mpBldr.setPortQual(port.getPortQual().getName()); } @@ -877,25 +869,4 @@ public class PortMappingVersion121 { } return nodeInfoBldr.build(); } - - /** - * Implements the FNV-1 64bit algorithm. - * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding. - * Otherwise, the hash cannot be stored in a UTF-8 string. - * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash - * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java - * @param stringdata the String to be hashed - * @return the base64 formatted hash string - */ - private String fnv(String stringdata) { - BigInteger hash = FNV_INIT; - byte[] data = stringdata.getBytes(StandardCharsets.UTF_8); - - for (byte b : data) { - hash = hash.multiply(FNV_PRIME).mod(FNV_MOD); - hash = hash.xor(BigInteger.valueOf((int) b & 0xff)); - } - - return Base64.getEncoder().encodeToString(hash.toByteArray()); - } } diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java index 1a9af0b8b..9669c3420 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java @@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping; import com.google.common.util.concurrent.FluentFuture; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -105,10 +102,6 @@ public class PortMappingVersion221 { private final DataBroker dataBroker; private final DeviceTransactionManager deviceTransactionManager; private final OpenRoadmInterfaces openRoadmInterfaces; - //FNV1 64 bit hash constants - private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16); - private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16); - private static final BigInteger FNV_MOD = new BigInteger("2").pow(64); public PortMappingVersion221(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager, OpenRoadmInterfaces openRoadmInterfaces) { @@ -972,7 +965,7 @@ public class PortMappingVersion221 { .setSupportingCircuitPackName(circuitPackName) .setSupportingPort(port.getPortName()) .setPortDirection(port.getPortDirection().getName()) - .setLcpHashVal(fnv(nodeIdLcp)); + .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp)); if (port.getPortQual() != null) { mpBldr.setPortQual(port.getPortQual().getName()); } @@ -1170,24 +1163,4 @@ public class PortMappingVersion221 { return nodeInfoBldr.build(); } - /** - * Implements the FNV-1 64bit algorithm. - * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding. - * Otherwise, the hash cannot be stored in a UTF-8 string. - * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash - * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java - * @param stringdata the String to be hashed - * @return the hash string - */ - private String fnv(String stringdata) { - BigInteger hash = FNV_INIT; - byte[] data = stringdata.getBytes(StandardCharsets.UTF_8); - - for (byte b : data) { - hash = hash.multiply(FNV_PRIME).mod(FNV_MOD); - hash = hash.xor(BigInteger.valueOf((int) b & 0xff)); - } - - return Base64.getEncoder().encodeToString(hash.toByteArray()); - } } diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java index bac76e0dd..077c94955 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java @@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping; import com.google.common.util.concurrent.FluentFuture; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -108,10 +105,6 @@ public class PortMappingVersion710 { private final DataBroker dataBroker; private final DeviceTransactionManager deviceTransactionManager; private final OpenRoadmInterfaces openRoadmInterfaces; - //FNV1 64 bit hash constants - private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16); - private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16); - private static final BigInteger FNV_MOD = new BigInteger("2").pow(64); public PortMappingVersion710(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager, OpenRoadmInterfaces openRoadmInterfaces) { @@ -1015,7 +1008,7 @@ public class PortMappingVersion710 { .setSupportingCircuitPackName(circuitPackName) .setSupportingPort(ports.getPortName()) .setPortDirection(ports.getPortDirection().getName()) - .setLcpHashVal(fnv(nodeIdLcp)); + .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp)); if (ports.getPortQual() != null) { mpBldr.setPortQual(ports.getPortQual().getName()); @@ -1215,25 +1208,4 @@ public class PortMappingVersion710 { return nodeInfoBldr.build(); } - - /** - * Implements the FNV-1 64bit algorithm. - * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding. - * Otherwise, the hash cannot be stored in a UTF-8 string. - * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash - * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java - * @param stringdata the String to be hashed - * @return the hash string - */ - private String fnv(String stringdata) { - BigInteger hash = FNV_INIT; - byte[] data = stringdata.getBytes(StandardCharsets.UTF_8); - - for (byte b : data) { - hash = hash.multiply(FNV_PRIME).mod(FNV_MOD); - hash = hash.xor(BigInteger.valueOf((int) b & 0xff)); - } - - return Base64.getEncoder().encodeToString(hash.toByteArray()); - } } -- 2.36.6