From a949c160ed0af34870de5a331665ccf53f66dd12 Mon Sep 17 00:00:00 2001 From: Cyril Dever Date: Tue, 14 Dec 2021 11:42:55 +0100 Subject: [PATCH] remove javax.xml.bind.DatatypeConverter from dependencies - add hex utilities - tests --- build.sbt | 2 +- .../scala/fr/edgewhere/feistel/Main.scala | 4 ++-- .../common/utils/base256/Readable.scala | 8 +++---- .../feistel/common/utils/hash/Hash.scala | 8 +++---- .../feistel/common/utils/hex/Hex.scala | 20 ++++++++++++++++++ .../common/utils/base256/ReadableSpecs.scala | 6 +++--- .../feistel/common/utils/hex/HexSpecs.scala | 21 +++++++++++++++++++ 7 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala create mode 100644 src/test/scala/fr/edgewhere/feistel/common/utils/hex/HexSpecs.scala diff --git a/build.sbt b/build.sbt index 79a689f..005c99e 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ organization := "fr.edgewhere" name := "feistel-jar" -version := "1.3.5" +version := "1.4.0" scalaVersion := "2.12.13" assemblyMergeStrategy in assembly := { diff --git a/src/main/scala/fr/edgewhere/feistel/Main.scala b/src/main/scala/fr/edgewhere/feistel/Main.scala index 70ab243..7ddb549 100644 --- a/src/main/scala/fr/edgewhere/feistel/Main.scala +++ b/src/main/scala/fr/edgewhere/feistel/Main.scala @@ -10,8 +10,8 @@ import fr.edgewhere.feistel.common.utils.hash.Engine._ * @since 1.0 * @version 1.0 * @example {{{ - * $ sbt assembly && java -cp target/scala-2.12/feistel-jar-0.1.0.jar fr.edgewhere.feistel.Main 'myWordToObfuscate' - * $ java -cp target/scala-2.12/feistel-jar-0.1.0.jar fr.edgewhere.feistel.Main -d '!f^w=€¦-Hyrbq¡#bs' + * $ sbt assembly && java -cp target/scala-2.12/feistel-jar-1.4.0.jar fr.edgewhere.feistel.Main 'myWordToObfuscate' + * $ java -cp target/scala-2.12/feistel-jar-1.4.0.jar fr.edgewhere.feistel.Main -d '!f^w=€¦-Hyrbq¡#bs' * }}} */ object Main extends App { diff --git a/src/main/scala/fr/edgewhere/feistel/common/utils/base256/Readable.scala b/src/main/scala/fr/edgewhere/feistel/common/utils/base256/Readable.scala index 36e7d96..74cc3e8 100644 --- a/src/main/scala/fr/edgewhere/feistel/common/utils/base256/Readable.scala +++ b/src/main/scala/fr/edgewhere/feistel/common/utils/base256/Readable.scala @@ -1,12 +1,12 @@ package fr.edgewhere.feistel.common.utils.base256 -import javax.xml.bind.DatatypeConverter +import fr.edgewhere.feistel.common.utils.hex.Hex /** * Readable class * * @author Cyril Dever - * @since 1.0 + * @since 2.0 * @version 1.0 */ object Readable { @@ -30,11 +30,11 @@ object Readable { */ def string: String = r.bytes.map(_.toChar).mkString // TODO toString couldn't be overridden either - def toHex: String = DatatypeConverter.printHexBinary(bytes).toLowerCase + def toHex: String = Hex.byteArrayToHexString(bytes) } def fromHex(str: String): Readable = - Readable(DatatypeConverter.parseHexBinary(str).map(b => charAt(b.toInt)).mkString) + Hex.stringToSomeByteArray(str).getOrElse(Seq.empty).map(b => charAt(b.toInt)).mkString val CHARSET = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}€¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷ùúûüýÿăąĊčđĕĘğħĩĭıĵķĿŀŁłňŋŏœŖřŝşŦŧũūůŲŵſƀƁƂƄƆƇƔƕƗƙƛƜƟƢƥƦƧƩƪƭƮưƱƲƵƸƺƾǀǁǂƿǬǮǵǶǹǻǿ" diff --git a/src/main/scala/fr/edgewhere/feistel/common/utils/hash/Hash.scala b/src/main/scala/fr/edgewhere/feistel/common/utils/hash/Hash.scala index 465877b..82b3a7a 100644 --- a/src/main/scala/fr/edgewhere/feistel/common/utils/hash/Hash.scala +++ b/src/main/scala/fr/edgewhere/feistel/common/utils/hash/Hash.scala @@ -1,20 +1,20 @@ package fr.edgewhere.feistel.common.utils.hash -import javax.xml.bind.DatatypeConverter +import fr.edgewhere.feistel.common.utils.hex.Hex /** * Hash type * * @author Cyril Dever - * @since 1.0 + * @since 2.0 * @version 1.0 */ object Hash { type Hash = Seq[Byte] implicit class HashOps(h: Hash) { - def toHex: String = DatatypeConverter.printHexBinary(h.toArray).toLowerCase + def toHex: String = h.map(b => f"$b%02x").mkString.toLowerCase } - def fromHex(str: String): Hash = DatatypeConverter.parseHexBinary(str) + def fromHex(str: String): Hash = Hex.stringToSomeByteArray(str).getOrElse(Seq.empty) } diff --git a/src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala b/src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala new file mode 100644 index 0000000..646edaa --- /dev/null +++ b/src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala @@ -0,0 +1,20 @@ +package fr.edgewhere.feistel.common.utils.hex + +import java.math.BigInteger + +/** + * Hex utility + * + * @author Cyril Dever + * @since 1.0 + * @version 1.4 + */ +object Hex { + def byteArrayToHexString(bytes: Seq[Byte]): String = bytes.map(b => f"$b%02x").mkString.toLowerCase + + def stringToSomeByteArray(str: String): Option[Seq[Byte]] = try { + Some(new BigInteger(str, 16).toByteArray) + } catch { + case _: Exception => None + } +} diff --git a/src/test/scala/fr/edgewhere/feistel/common/utils/base256/ReadableSpecs.scala b/src/test/scala/fr/edgewhere/feistel/common/utils/base256/ReadableSpecs.scala index c996512..551b5bc 100644 --- a/src/test/scala/fr/edgewhere/feistel/common/utils/base256/ReadableSpecs.scala +++ b/src/test/scala/fr/edgewhere/feistel/common/utils/base256/ReadableSpecs.scala @@ -1,13 +1,13 @@ package fr.edgewhere.feistel.common.utils.base256 import fr.edgewhere.BasicUnitSpecs -import javax.xml.bind.DatatypeConverter +import fr.edgewhere.feistel.common.utils.hex.Hex /** * ReadableSpecs test class * * @author Cyril Dever - * @since 1.0 + * @since 2.0 * @version 1.0 */ class ReadableSpecs extends BasicUnitSpecs { @@ -27,7 +27,7 @@ class ReadableSpecs extends BasicUnitSpecs { val fpeEdgewhere = Readable("K¡(#q|r5*") fpeEdgewhere.length should equal (9) val fpeBytes = Seq[Byte](42, 93, 7, 2, 79, 90, 80, 20, 9) - fpeBytes should equal (DatatypeConverter.parseHexBinary("2a5d07024f5a501409")) + fpeBytes should equal (Hex.stringToSomeByteArray("2a5d07024f5a501409").get) val fpeB256 = Readable(fpeBytes) fpeB256 should equal (fpeEdgewhere) fpeB256.length should equal (fpeEdgewhere.length) diff --git a/src/test/scala/fr/edgewhere/feistel/common/utils/hex/HexSpecs.scala b/src/test/scala/fr/edgewhere/feistel/common/utils/hex/HexSpecs.scala new file mode 100644 index 0000000..4bed305 --- /dev/null +++ b/src/test/scala/fr/edgewhere/feistel/common/utils/hex/HexSpecs.scala @@ -0,0 +1,21 @@ +package fr.edgewhere.feistel.common.utils.hex + +import fr.edgewhere.BasicUnitSpecs + +/** + * HexSpecs test class + * + * @author Cyril Dever + * @since 1.0 + * @version 1.4 + */ +class HexSpecs extends BasicUnitSpecs { + + "Hex.stringToSomeByteArray" should "return the appropriate byte array" in { + val hexString = "123abc" + + val found = Hex.stringToSomeByteArray(hexString) + found.isEmpty shouldBe false + found.get should equal (Seq(18, 58, -68)) + } +}