diff options
-rw-r--r-- | build.sbt | 5 | ||||
-rw-r--r-- | src/utils/ByteUtils.scala (renamed from src/ByteUtils.scala) | 2 | ||||
-rw-r--r-- | src/utils/ConversionUtils.scala (renamed from src/ConversionUtils.scala) | 8 | ||||
-rw-r--r-- | src/utils/external/Base64.scala (renamed from src/Base64.scala) | 0 | ||||
-rw-r--r-- | test/ByteUtilsSpec.scala | 85 | ||||
-rw-r--r-- | test/ConversionUtilsSpec.scala | 31 |
6 files changed, 126 insertions, 5 deletions
@@ -6,6 +6,11 @@ scalaSource in Compile := baseDirectory.value / "src" scalaSource in Test := baseDirectory.value / "test" +lazy val testUtils = RootProject(file("/toy/scala/test_utils")) + +lazy val root = Project("root", file(".")) + .dependsOn(testUtils) + resolvers ++= Seq( "Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases" ) diff --git a/src/ByteUtils.scala b/src/utils/ByteUtils.scala index 3b6046c..344f6f7 100644 --- a/src/ByteUtils.scala +++ b/src/utils/ByteUtils.scala @@ -1,4 +1,4 @@ -package main.utils +package ixee.cryptopals.utils object ByteUtils { trait SizedNumeric[T] { diff --git a/src/ConversionUtils.scala b/src/utils/ConversionUtils.scala index 5d01429..26f2d24 100644 --- a/src/ConversionUtils.scala +++ b/src/utils/ConversionUtils.scala @@ -1,9 +1,9 @@ -package main.utils +package ixee.cryptopals.utils import ByteUtils.WithBitOpts object ConversionUtils { - def hexStr2ByteArr(s: String): Iterator[Byte] = + def hexStr2Bytes(s: String): Iterator[Byte] = padToByte(s).grouped(2).map(byteStr2Byte) def byteStr2Byte(str: String): Byte = @@ -16,7 +16,7 @@ object ConversionUtils { ((a @<< 4) @+ b) def padToByte(s: String): String = - if (s.length % 2 != 0) s + "0" else s + if (s.length % 2 != 0) "0" + s else s def octet2nibble(c: Char): Byte = { (c.toLower match { @@ -32,7 +32,7 @@ object ConversionUtils { def hexStr2Base64String(s: String): String = { import io.github.marklister.base64.Base64._ - hexStr2ByteArr(s).toArray.toBase64 + hexStr2Bytes(s).toArray.toBase64 } } diff --git a/src/Base64.scala b/src/utils/external/Base64.scala index 692abb8..692abb8 100644 --- a/src/Base64.scala +++ b/src/utils/external/Base64.scala diff --git a/test/ByteUtilsSpec.scala b/test/ByteUtilsSpec.scala new file mode 100644 index 0000000..8c7c7c9 --- /dev/null +++ b/test/ByteUtilsSpec.scala @@ -0,0 +1,85 @@ +package ixee.cryptopals.test + +import ixee.cryptopals.utils.ByteUtils + +import com.ixee.IxeeSpec + +class ByteUtilsSpec extends IxeeSpec { + + "ByteUtils" - { + + "SizedNumeric" - { + + ".liftedTo" - { + + "when the destination is >= the source in range" - { + + "losslessly converts to the destination type" - { + + } + + } + + "when the destination is smaller than the source" - { + + "throws an exception TODO: make it a Try?" - { + + } + + } + + } + + ".truncatedTo" - { + + "converts to the target type, truncating extra bytes" - { + + } + + } + + ".byteSize" - { + + "returns the size of a the type, in bytes" - { + + } + + } + + ".bitSize" - { + + "returns the size of a type, in bits" - { + + } + + } + + } + + "BitOps" - { + + "provides typed operations that don't upcast to Int" - { + + } + + } + + "toByteArray TODO: toByteSeq?" - { + + "returns the value as an array of bytes TODO: endianness?" - { + + } + + } + + "toBinaryString" - { + + "returns the value as a string of 1s and 0s, padded to the full size" - { + + } + + } + + } + +} diff --git a/test/ConversionUtilsSpec.scala b/test/ConversionUtilsSpec.scala new file mode 100644 index 0000000..d6c17b3 --- /dev/null +++ b/test/ConversionUtilsSpec.scala @@ -0,0 +1,31 @@ +package ixee.cryptopals.test + +import com.ixee.IxeeSpec +import org.scalatest._ + +class ConversionUtilsSpec extends FreeSpec with MustMatchers { + + import ixee.cryptopals.utils.ConversionUtils._ + + "ConversionUtils" - { + ".hexStr2Bytes" - { + "converts a hexadecimal string to an equivalent iterator of bytes" - { + hexStr2Bytes("12348765").toArray must equal( + Array[Byte]( + 0x12.toByte, + 0x34.toByte, + 0x87.toByte, + 0x65.toByte + ) + ) + } + + "when the string is not an even length" - { + "prepends a 0" - { + hexStr2Bytes("2123").toArray must equal(hexStr2Bytes("0123").toArray) + } + } + } + } + +} |