###Project Moved
This project has moved to https://github.com/marklister/basen
The new version supports Scala 2.10.3 / newer sbt
###Scala Hex / Decimal / Binary / Crypto DSL
I used this DSL / library to complete Dan Boneth's Cryptography I Coursera course. I did all the exercises in Scala. Scala was (IMHO) a pretty good fit for the task and I suspect my solutions performed a bit better than Python based code. Functional programming and crypto prototyping seem to go quite well together.
####Licence
Apache 2, or if you need something else just shout. Please, if you fork this don't add any spoilers (ie exercise specific code).
####Usage
Unfortunately due to issue SI-4684 you can't load this file directly into the REPL. You can try (it might be fixed by now). scala> :load ./BaseN.scala
#####Plan B (best method!):
Save the file BaseN.scala its own directory. Run sbt console
from that directory. From the console prompt type import BaseN._; import CryptoOps._
#####Plan C -- use the REPL :paste
command
scala> :paste
// Entering paste mode (ctrl-D to finish)
//Paste the file
//Type CTRL-D
// Exiting paste mode, now interpreting.
warning: there were 2 deprecation warnings; re-run with -deprecation for details
defined class BaseN
defined module BaseN
defined class Hex
defined class Decimal
defined class Binary
import BaseN._
Now you can use the REPL as a Binary / Hex / Decimal calculator:
Syntax:
"String".b // interpret string as binary
"String".h // interpret string as hex
"String".d // interpret string as a decimal
Int.h // convert Int into a BaseN number (hex in this case)
you can use either ^ or 'xor' as an xor operator
scala> "10".b
res0: Binary = 00000010 [base: 2]
scala> "10".b * "10".b
res1: BaseN = 00000100 [base: 2]
scala> "0a".h
res2: Hex = 0a [base: 16]
scala> "0a".h <<1
res3: BaseN = 14 [base: 16]
scala> "0a".h <<"10".b
res4: BaseN = 28 [base: 16]
scala> "11111111".b xor "10101010".b
res5: BaseN = 01010101 [base: 2]
scala> "0a".h
res10: Hex = 0a [base: 16]
scala> "0a".h.d
res11: Decimal = 010 [base: 10]
scala> "0a".h.b
res12: Binary = 00001010 [base: 2]
The default toString implemenation is byte orientated, so for example, "256".d is displayed as: 001:255 [base 10]. You can use toRawString or toString(10) to display the number conventionally.
If you use a BigInt operator that I've not implemented you'll wind up with a BigInt. You can convert a BigInt using the .h .d or .b methods. scala> res14.b
The CryptoOps class adds crypto primatives to Array[Byte] via implicit conversions (ie the pimp my library pattern).
scala> "Hello World".getBytes.sha256
res6: Array[Byte] = Array(-91, -111, -90, -44, 11, -12, 32, 64, 74, 1, 23, 51, -49, -73, -79, -112, -42, 44, 101, -65, 11, -51, -93, 43, 87, -78, 119, -39, -83, -97, 20, 110)
scala> res6.h
res7: Hex = a5:91:a6:d4:0b:f4:20:40:4a:01:17:33:cf:b7:b1:90:d6:2c:65:bf:0b:cd:a3:2b:57:b2:77:d9:ad:9f:14:6e [base: 16]
scala>import BaseN._
import BaseN._
scala>import CryptoOps._
import CryptoOps._
scala>"Hello World".getBytes.pkcs5Pad.aesEncrypt("Short key".getBytes.pkcs5Pad)
res1: CryptoOps = 01:73:69:fa:78:43:38:ae:07:4e:8d:10:c4:3c:cc:fd [base: 16]
scala>res1.aesDecrypt("Short key".getBytes.pkcs5Pad).h.toByteArray.map(_.toChar)
res3: Array[Char] = Array(H, e, l, l, o, , W, o, r, l, d, ?, ?, ?, ?, ?)
Enjoy!
Mark,
I just started Dan Boneth's Cryptology I Coursera course two days ago (Mar 25, 2013) and began building a Scala module with similar functionality until I came across your code. This is a great find for me. I will make sure to post this GitHub link to the Cryptology I forum.
Thanks so much!
Greg Kulczycki