Skip to content

Commit

Permalink
added encryption to the storage system
Browse files Browse the repository at this point in the history
added some more styling
fine tuned date parsing
added link to my page
added versioning
reset db location
  • Loading branch information
AndySakov committed Jul 25, 2021
1 parent 7ae7dcd commit 019c5b2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea
project/target
target
db.csv
target
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "alist"
organization := "com.shiftio"
version := "1.0-SNAPSHOT"
version := "1.0.1"
scalaVersion := "2.13.1"
lazy val circeVersion = "0.12.3"
libraryDependencies ++= Seq(
Expand Down
1 change: 0 additions & 1 deletion db.csv

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/scala/com/shiftio/alist/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.shiftio.alist.api.Api.{add, cls, edit, list, _}
import com.shiftio.alist.api.DB.{commit, inMemory}
import com.shiftio.alist.api.commons._

import java.awt.Desktop
import java.net.URI
import scala.annotation.tailrec
import scala.io.StdIn._
import scala.language.postfixOps
Expand Down Expand Up @@ -73,11 +75,19 @@ object Main extends App {
}
}
list()
case v if v equalsIgnoreCase "version" => println(s"\tALIST version $version")

case m if m equalsIgnoreCase "more" =>
println("\tOpening developers page...")
Desktop.getDesktop.browse(URI.create("https://github.com/AndySakov/ALIST"))

case q if q take 4 equalsIgnoreCase "quit" =>
println(success("\tGoodbye!"))
System.exit(0)

case "" =>
// Do nothing

case _ => println(error("\t\tInvalid Input!"))

}
Expand Down
44 changes: 36 additions & 8 deletions src/main/scala/com/shiftio/alist/api/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import com.shiftio.alist.api.commons.OS.{FREEBSD, LINUX, MAC_OS_X, SUN_OS, UNKNO
import com.shiftio.alist.api.commons.{TodoItem, error, info}

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.{DateTimeFormatter, DateTimeParseException}
import scala.io.StdIn.readLine
import scala.language.postfixOps
import scala.sys.process._
import scala.util.{Failure, Success, Try}


object Api {
Expand Down Expand Up @@ -52,9 +53,23 @@ object Api {
println(info("\tAdd a new todo list item"))
val name = readVar("\tName: ")
val desc = readVar("\tDescription: ")
val dueBy = readVar("\tDue Date[yyyy-MM-dd][Press Enter to skip]: ") match {
case "" => None
case date => Some(LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")))
val dueBy = readVar("\n\tPress Enter to skip\n\tDue Date[yyyy-MM-dd]: ") match {
case date =>
Try {
LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
} match {
case Failure(err) =>
err match {
case _: DateTimeParseException =>
if (date.nonEmpty) {
println(error(s"\n\t'$date' is not a valid date!"))
}
None
}
case Success(parsed) =>
Some(parsed)
}

}
val remind = readVar("\tRemind[y/n]: ") match {
case "y" => true
Expand Down Expand Up @@ -107,10 +122,23 @@ object Api {
}
},
{
if (newDueBy == "") {
x.dueBy
} else {
Some(LocalDate.parse(newDueBy, DateTimeFormatter.ofPattern("yyyy-MM-dd")))
newDueBy match {
case date =>
Try {
LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
} match {
case Failure(err) =>
err match {
case _: DateTimeParseException =>
println(error(s"\n\tString '$date' is not a valid date!"))
None
}
case Success(parsed) =>
Some(parsed)
}
case "" =>
x.dueBy

}
},
{
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/com/shiftio/alist/api/DB.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.shiftio.alist.api

import better.files.Dsl.pwd
import better.files.File
import better.files.File.home
import com.shiftio.alist.api.commons.{TodoItem, error}

import java.time.LocalDate
import java.time.format.DateTimeFormatter

object DB {

val db: File = pwd / "db.csv"
val db: File = home / ".alist" / "~.db"
val dir: File = home / ".alist"

var inMemory: List[TodoItem] = selectAll

def selectAll: List[TodoItem] = {
if (!db.exists) {
println(error("[INFO] DB file not found!\n[INFO] Creating new db in present working directory"))
db.createFile()
dir.createDirectoryIfNotExists(createParents = true)
db.createFileIfNotExists()
}
read()
}

def read(): List[TodoItem] = {
val data = db.contentAsString
val data = Golem.decrypt(db.contentAsString)
if (data == "") {
List()
} else {
Expand All @@ -40,8 +42,8 @@ object DB {

def commit(): Unit = {
db.write(
inMemory.map(item => {
Golem.encrypt(inMemory.map(item => {
List(item.id, item.name, item.desc, item.dueBy.getOrElse("N/A"), item.remind).mkString(",")
}).mkString("\n"))
}).mkString("\n")))
}
}
35 changes: 35 additions & 0 deletions src/main/scala/com/shiftio/alist/api/Golem.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.shiftio.alist.api

import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}

object Golem {

private val key = "DSRqGmPgssSfx6r9"
private val initVector = "QFMgtdtxp6QektB4"

def encrypt(text: String): String = {

val iv = new IvParameterSpec(initVector.getBytes("UTF-8"))
val keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES")

val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv)

val encrypted = cipher.doFinal(text.getBytes())
return Base64.getEncoder.encodeToString(encrypted)
}

def decrypt(text: String): String = {
val iv = new IvParameterSpec(initVector.getBytes("UTF-8"))
val skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES")

val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv)
val original = cipher.doFinal(Base64.getDecoder.decode(text))

new String(original)
}

}
5 changes: 4 additions & 1 deletion src/main/scala/com/shiftio/alist/api/commons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ object commons {
val asciiStyle: Attrs = fansi.Bold.On ++ Random.shuffle(fansi.Color.all).head
val captionStyle: Attrs = fansi.Bold.On ++ Random.shuffle(fansi.Color.all).head
val help: Attrs = fansi.Underlined.On ++ fansi.Bold.On ++ Random.shuffle(fansi.Color.all).head
val error: Attrs = fansi.Underlined.On ++ fansi.Bold.On ++ fansi.Color.Red
val error: Attrs = fansi.Bold.On ++ fansi.Color.Red
val info: Attrs = fansi.Color.LightBlue
val success: Attrs = fansi.Color.Green ++ fansi.Bold.On
val promptText: Str = asciiStyle("~❯ ")
val version = "1.0.1"

val header =
s"${
Expand All @@ -35,7 +36,9 @@ object commons {
| view [x]: View todo list item with id x
| edit [x]: Edit todo list item with id x
| drop [x]: Drop todo list item with id x
| version: Prints the project version
| help: Prints this message
| more: Opens developer page
| quit: Exit the program
""" stripMargin

Expand Down

0 comments on commit 019c5b2

Please sign in to comment.