Permalink
Browse files

done

  • Loading branch information...
Frostman committed May 1, 2012
1 parent 5cce529 commit f3dad2c16bf86ce0925dd7572ff20f8ff645ef95
Showing with 121 additions and 87 deletions.
  1. +55 −45 problem_1/src/auth/Auth.kt
  2. +11 −8 problem_1/src/ui/EditableTable.kt
  3. +55 −34 problem_1/src/ui/GUI.kt
View
@@ -10,16 +10,16 @@ import java.util.List
import java.util.Map
import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.StringUtils
+import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import ui.et.Column
import ui.et.StringValue
import ui.et.Value
-import java.text.NumberFormat
class AuthDb(val dbPath : String) {
// login -> user
public val users : Map<String, User> = LinkedHashMap<String, User>()
- public val confs : Map<String, String> = LinkedHashMap<String, String>()
+ public val infos : List<Info> = LinkedList<Info>()
fun load(val encrypted : Boolean = false) {
if(encrypted) {
@@ -29,19 +29,21 @@ class AuthDb(val dbPath : String) {
users.clear()
for (line in FileUtils.readLines(File(dbPath))) {
val split = line.sure().split(" ").sure()
- if(split.size == 5) {
+ if(split.size == 4) {
val login = split[0].sure().fromB64()
val password = split[1].sure().fromB64()
- val role = if (Integer.parseInt(split[2]) == 0) Role.ADMIN else Role.USER
- val lastModified = Long.parseLong(split[3]!!)
- val strange = split[4]!!.fromB64()
-
- users.put(login, User(login, password, role, lastModified, strange))
- } else if(split.size == 2) {
- val key = split[0]!!.fromB64()
- val value = split[1]!!.fromB64()
-
- confs.put(key, value)
+ val role = if (Integer.parseInt(split[2]) == 0) Role.ADMIN else if (Integer.parseInt(split[2]) == 1) Role.EDITOR else Role.USER
+ val lastModified = Long.parseLong(split[3])
+
+ users.put(login, User(login, password, role, lastModified))
+ } else if(split.size == 5) {
+ val name = split[0].fromB64()
+ val middleName = split[1].fromB64()
+ val lastName = split[2].fromB64()
+ val phone = split[3].fromB64()
+ val address = split[4].fromB64()
+
+ infos.add(Info(name, middleName, lastName, phone, address))
}
}
@@ -53,7 +55,7 @@ class AuthDb(val dbPath : String) {
fun save(val encrypt : Boolean = false) {
val list = ArrayList<Any?>()
list.addAll(users.b64Values())
- list.addAll(confs.b64Values2())
+ list.addAll(infos.b64Values2())
FileUtils.writeLines(File(dbPath), list)
if (encrypt) {
encryptFile(ui.key, File(dbPath))
@@ -63,56 +65,68 @@ class AuthDb(val dbPath : String) {
enum class Role {
ADMIN
+ EDITOR
USER
}
-class User(val login : String, val password : String, val role : Role, val lastModified : Long, val passwordStrange : String) {
+class User(val login : String, val password : String, val role : Role, val lastModified : Long) {
//todo think about password hashing
- fun toString() = "${login} ${password} ${if (role == Role.ADMIN) 0 else 1} $lastModified strange=${passwordStrange}"
+ fun toString() = "${login} ${password} ${if (role == Role.ADMIN) 0 else if (role == Role.EDITOR) 1 else 2} $lastModified"
- fun asB64String() = "${login.toB64()} ${password.toB64()} ${if (role == Role.ADMIN) 0 else 1} $lastModified ${passwordStrange.toB64()}"
+ fun asB64String() = "${login.toB64()} ${password.toB64()} ${if (role == Role.ADMIN) 0 else if (role == Role.EDITOR) 1 else 2} $lastModified"
fun asColumns() : List<Value> {
val columns = ArrayList<Value>()
columns.add(StringValue(login))
columns.add(StringValue(password))
- columns.add(StringValue(if (role == Role.ADMIN) "admin" else "user"))
+ columns.add(StringValue(if (role == Role.ADMIN) "admin" else if (role == Role.EDITOR) "editor" else "user"))
columns.add(StringValue(lastModified.toString()))
- columns.add(StringValue(passwordStrange))
return columns
}
class object {
public val columns : List<Column> = arrayList(Column("login"), object : Column("password", true){
- public override fun onChanged(val value : String) : List<#(Int, Any?)>? = arrayList(#(3, StringValue(System.currentTimeMillis().toString())), #(4, StringValue(calcPasswordStrange(value))))
- }, Column("role"), Column("lastModified"), Column("Strange", false));
+ public override fun onChanged(val value : String) : List<#(Int, Any?)>? = arrayList(#(1, StringValue(value.sha())))
+ }, Column("role"), Column("lastModified"));
public fun fromColumns(val it : List<Value>) : User = User(
(it[0] as StringValue).str,
(it[1] as StringValue).str,
- if(it[2] is StringValue && (it[2] as StringValue).str.equals("admin")) Role.ADMIN else Role.USER,
- (it[3] as StringValue).str.toLong(),
- (it[4] as StringValue).str
+ if(it[2] is StringValue && (it[2] as StringValue).str.equals("admin")) Role.ADMIN else
+ (if(it[2] is StringValue && (it[2] as StringValue).str.equals("editor")) Role.EDITOR else Role.USER),
+ (it[3] as StringValue).str.toLong()
)
+ }
+}
- public fun calcPasswordStrange(val password : String) : String {
- val db = AuthDb("database")
- db.load(true)
+class Info (val name : String, val middleName : String, val lastName : String, val phone : String, val address : String) {
+ fun asB64String() = "${name.toB64()} ${middleName.toB64()} ${lastName.toB64()} ${phone.toB64()} ${address.toB64()}"
- val bruteForceSpeed = db.confs.get("password bruteforce speed")!!.toDouble()
- val maxLifeTime = db.confs.get("max password life time")!!.toDouble()
- val alphabetSize = db.confs.get("alphabet size")!!.toDouble()
- val passwordSpace = Math.pow(alphabetSize, password.length().toDouble())
- val value = Math.min(bruteForceSpeed * maxLifeTime / passwordSpace, 100.toDouble())
- val format = NumberFormat.getInstance()!!
- format.setMaximumFractionDigits(4)
+ fun asColumns() : List<Value> = arrayList(
+ StringValue(name),
+ StringValue(middleName),
+ StringValue(lastName),
+ StringValue(phone),
+ StringValue(address)
+ )
+
+ class object {
+ public val editableColumns : List<Column> = arrayList(Column("name"), Column("middle name"), Column("last name"), Column("phone"), Column("address"))
+
+ public val columns : List<Column> = arrayList(Column("name", false), Column("middle name", false), Column("last name", false), Column("phone", false), Column("address", false))
+
+ public fun fromColumns(val it : List<Value>) : Info = Info(
+ (it[0] as StringValue).str,
+ (it[1] as StringValue).str,
+ (it[2] as StringValue).str,
+ (it[3] as StringValue).str,
+ (it[4] as StringValue).str
+ )
- return "${format.format(100 - value)}%"
- }
}
}
@@ -126,11 +140,11 @@ fun Map<String, User>.b64Values() : List<String> {
return result
}
-fun Map<String, String>.b64Values2() : List<String> {
+fun List<Info>.b64Values2() : List<String> {
val result = LinkedList<String>()
- for (pair in this.entrySet()) {
- result.add(pair.getKey().toB64() + " " + pair.getValue().toB64())
+ for (entry in this) {
+ result.add(entry.asB64String())
}
return result
@@ -143,7 +157,7 @@ fun String.fromB64() = StringUtils.newStringUtf8(Base64.decodeBase64(this.getByt
fun checkCredentials(val login : String, val password : String, val key : String = "test key", val dbPath : String = "database") : Boolean {
val db = AuthDb(dbPath)
db.load(true)
- val result = db.users.get(login)?.password.equals(password)
+ val result = db.users.get(login)?.password.equals(password.sha())
if (db.users.size == 0) {
return true
@@ -159,8 +173,4 @@ fun getUser(val login : String, val dbPath : String = "database") : User? {
return db.users.get(login)
}
-fun main(args : Array<String>) {
- val db = AuthDb("database")
- db.users.put("admin", User("admin", "admin", Role.ADMIN, 0, "0%"))
- db.save(true)
-}
+fun String.sha() = DigestUtils.sha256Hex(this)!!
@@ -40,22 +40,25 @@ class EditableTable(val columns : List<Column>) {
public override fun getColumnName(column : Int) = columns[column].name
public override fun setValueAt(newValue : Any?, row : Int, column : Int) {
+ var changed : List<#(Int, Any?)>?;
if(data[row][column] is StringValue && !(data[row][column] as StringValue).str.equals(newValue)) {
- val changed = columns[column].onChanged(if(newValue is StringValue) newValue.str else newValue as String)
+ changed = columns[column].onChanged(if(newValue is StringValue) newValue.str else newValue as String)
+
+ data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
+ fireTableCellUpdated(row, column)
+
if(changed != null) {
for(change in changed) {
- setValueAt(change._2, row, change._1)
+ data[row][change._1] = if (change._2 is String) StringValue(change._2 as String) else change._2 as Value
+ fireTableCellUpdated(row, change._1)
}
}
+ } else {
+ data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
+ fireTableCellUpdated(row, column)
}
- data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
- fireTableCellUpdated(row, column)
}
})
- // var idx = 0
- // for (column in columns) {
- // table.getColumnModel()!!.getColumn(idx++)!!.setCellEditor(column.editor)
- // }
}
fun <T> getObjects(transform : List<Value>.() -> T) = data.map{it.transform()}
Oops, something went wrong.

0 comments on commit f3dad2c

Please sign in to comment.