Skip to content

marklister/basen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BaseN

Scala for Cryptography I on Coursera //google

A simple (initially 5 classes) library for working with Hex / Decimal / Binary data in the Scala REPL or in a program.

Crypto primitives can be applied to Hex / Decimal / Binary data.

The library was developed to complete Dan Boneh's Cryptography I course on Coursera.

Provided under the Apache 2 Licence. Please don't add exercise specific code if you use it for the same purpose.

What the masses are saying about it

@marklister Thanks a lot for your tool that saved me a lot of time during the course and allowed me to focus purely on cryptography during assignments. See the pull request #1 and #3 for details.

How to use it

GIT

Simply cloning the project, installing a jdk and sbt will give you a working environment. Run sbt console from the cloned directory. sbt run will activate the improved ammonite REPL.

To use as a library execute sbt publishLocal

Scaladoc

Browse Scaladoc on line

High level concept

Strings are implicitly converted into BaseN objects. You invoke the h method to convert from Hex d to convert from Decimal and b from binary

Byte arrays are implicitly converted into Objects of type CryptoOps.

You can add subtract multiply divide, xor etc any of your BaseN objects. You can convert them to hex decimal binary or BigInts.

You can encrypt, hash, decrypt, pkspad your converted byte arrays.

The REPL tab key might be useful.

If you are running from the REPL imports are handled automatically. If you are writing a program then include:

import org.github.marklister.basen.BaseN._
import org.github.marklister.basen.CryptoOps._

REPL Session

Welcome to Scala 2.12.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_121).
Type in expressions for evaluation. Or try :help.

scala> //working with Hex / Decimal / Binary
import org.github.marklister.basen.BaseN._
import org.github.marklister.basen.CryptoOps._

scala> "255".d 
res0: org.github.marklister.basen.Decimal = 000:255 [base: 10]

scala> "ff".h
res1: org.github.marklister.basen.Hex = 00:ff [base: 16]

scala> "10".d * 2
res2: org.github.marklister.basen.BaseN = 020 [base: 10]

scala> "10101010".b xor "01010101".b
res3: org.github.marklister.basen.BaseN = 00000000:11111111 [base: 2]

scala> "2".d + "f".h
res4: org.github.marklister.basen.BaseN = 017 [base: 10]

scala> 

scala> //Convert hex to binary

scala> "ff".h.b
res5: org.github.marklister.basen.Binary = 00000000:11111111 [base: 2]

scala> 

scala> //Crypto stuff

scala> "Hello".getBytes.h
res6: org.github.marklister.basen.Hex = 48:65:6c:6c:6f [base: 16]

scala> "Hello".getBytes.sha256.h
res7: org.github.marklister.basen.Hex = 18:5f:8d:b3:22:71:fe:25:f5:61:a6:fc:93:8b:2e:26:43:06:ec:30:4e:da:51:80:07:d1:76:48:26:38:19:69 [base: 16]

scala> val encrypted="Hello World".getBytes.pkcs5Pad.aesEncrypt("my key".getBytes.pkcs5Pad)
encrypted: Array[Byte] = Array(-80, 101, 29, 31, 83, -62, 3, 95, -61, -73, -17, -45, -38, -83, 78, 22)

scala> encrypted.aesDecrypt("my key".getBytes.pkcs5Pad).h.toByteArray.map(_.toChar)
res8: Array[Char] = Array(H, e, l, l, o,  , W, o, r, l, d, ?, ?, ?, ?, ?)

About

Simple Scala library for working with binary / hex data and performing basic crypto operations. Used to complete Dan Boneh's Cryptography I course on Coursera

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages