diff options
author | iximeow <me@iximeow.net> | 2014-11-21 23:04:39 -0800 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2014-11-21 23:04:39 -0800 |
commit | 8a0354348d1f022908eaeecc162ea4aeb24ba910 (patch) | |
tree | ef6bd79e9939e0dab58f9f50bca91537f67796fe | |
parent | 70e3bb3e08e8231543b0d154615ceb2d5e04c885 (diff) |
tests 'n stuff
-rw-r--r-- | src/utils/ByteUtils.scala | 34 | ||||
-rw-r--r-- | test/Set1Spec.scala | 35 |
2 files changed, 59 insertions, 10 deletions
diff --git a/src/utils/ByteUtils.scala b/src/utils/ByteUtils.scala index 344f6f7..85c6058 100644 --- a/src/utils/ByteUtils.scala +++ b/src/utils/ByteUtils.scala @@ -62,20 +62,30 @@ object ByteUtils { } } - implicit class RichArrayOfBytes(b: Array[Byte]) { + implicit class RichIterableByte(iter: Iterable[Byte]) { def to[T : SizedNumeric : BitOps]: T = { - val numeric = implicitly[SizedNumeric[T]] - if(b.length < numeric.byteSize) { - throw new RuntimeException("Byte input is not long enough") - } + // Need to know length, so we must force the iter + iter.hasDefiniteSize match { + case true => throw new IllegalArgumentException("Argument is not finite!") + case false => { + val numeric = implicitly[SizedNumeric[T]] + if(b.length < numeric.byteSize) { + throw new RuntimeException("Byte input is not long enough") + } - var out: Long = b(0) - for(i <- 1 until numeric.byteSize) { - out << 8 - out = b(i) | out + var out: Long = b(0) + for(i <- 1 until numeric.byteSize) { + out << 8 + out = b(i) | out + } + numeric.fromLong(out) + } } - numeric.fromLong(out) } + + def xor(other: Iterable[Byte]): Iterable[Byte] = + iter.zip(other).map(_ ^@ _) + } def sizedNumeric[T : Manifest](bytes: Int)(from: Long => T)(to: T => Long) = new SizedNumeric[T] { def manifest = implicitly[Manifest[T]] @@ -106,4 +116,8 @@ object ByteUtils { } buf.toString() } + + def toHexString[T : SizedNumeric : BitOps](x: Iterable[T]): String = { + + } } diff --git a/test/Set1Spec.scala b/test/Set1Spec.scala new file mode 100644 index 0000000..4dbbfef --- /dev/null +++ b/test/Set1Spec.scala @@ -0,0 +1,35 @@ +package ixee.cryptopals.test + +import com.ixee.IxeeSpec + +class Set1Spec extends IxeeSpec { + + import ixee.cryptopals.utils.ConversionUtils._ + + "Set1" - { + + "Challenge 1: converts from a hex string to base64" in { + + hexStr2Base64String( + "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" + ) mustBe( + "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" + ) + } + + "Challenge 2: xor's two equal-length buffers" in { + + val a = hexStr2Bytes( + "1c0111001f010100061a024b53535009181c" + ) + val b = hexStr2Bytes( + "686974207468652062756c6c277320657965" + ) + + (a xor b).toHexStr mustBe + "746865206b696420646f6e277420706c6179" + } + + } + +} |