-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove javax.xml.bind.DatatypeConverter from dependencies
- add hex utilities - tests
- Loading branch information
1 parent
e7d7171
commit a949c16
Showing
7 changed files
with
55 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/main/scala/fr/edgewhere/feistel/common/utils/hash/Hash.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/scala/fr/edgewhere/feistel/common/utils/hex/Hex.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/scala/fr/edgewhere/feistel/common/utils/hex/HexSpecs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |