Skip to content

Commit

Permalink
Merge pull request #2 from cyrildever/develop
Browse files Browse the repository at this point in the history
Remove DatatypeConverter from dependencies
  • Loading branch information
cyrildever authored Dec 14, 2021
2 parents e7d7171 + a949c16 commit 7d52bdc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -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 := {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/fr/edgewhere/feistel/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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{|}€¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷ùúûüýÿăąĊčđĕĘğħĩĭıĵķĿŀŁłňŋŏœŖřŝşŦŧũūůŲŵſƀƁƂƄƆƇƔƕƗƙƛƜƟƢƥƦƧƩƪƭƮưƱƲƵƸƺƾǀǁǂƿǬǮǵǶǹǻǿ"

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 7d52bdc

Please sign in to comment.