From 7836bbd92dd026b49ef010defb0ae14363b6c749 Mon Sep 17 00:00:00 2001 From: s452641 Date: Thu, 25 Nov 2021 11:32:46 +0200 Subject: [PATCH] stable1 --- .idea/misc.xml | 11 +------ .../java/com/example/tasklist/ChCrypto.kt | 8 +++++ .../java/com/example/tasklist/ChangePass.kt | 30 ++++++++++++------- .../tasklist/ui/adapters/TaskAdapter.kt | 10 ++++++- .../ui/choose/list/TaskListFragment.kt | 16 +++------- app/src/main/res/layout/item_task.xml | 2 +- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 69dab29..3c4fe8a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -11,16 +11,6 @@ - - - - - - - - - - @@ -31,6 +21,7 @@ + diff --git a/app/src/main/java/com/example/tasklist/ChCrypto.kt b/app/src/main/java/com/example/tasklist/ChCrypto.kt index 63d3a5f..0bcf91c 100644 --- a/app/src/main/java/com/example/tasklist/ChCrypto.kt +++ b/app/src/main/java/com/example/tasklist/ChCrypto.kt @@ -4,6 +4,8 @@ import android.os.Build import androidx.annotation.RequiresApi import java.util.* import javax.crypto.Cipher +import javax.crypto.KeyGenerator +import javax.crypto.SecretKey import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.SecretKeySpec @@ -19,8 +21,14 @@ private object AES256{ private val encorder = Base64.getEncoder() @RequiresApi(Build.VERSION_CODES.O) private val decorder = Base64.getDecoder() + @RequiresApi(Build.VERSION_CODES.O) private fun cipher(opmode:Int): Cipher { val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX" +// val keygen = KeyGenerator.getInstance("AES") +// keygen.init(256) +// val key: SecretKey = keygen.generateKey() +// val secretKey = key.toString() + if(secretKey.length != 32) throw RuntimeException("SecretKey length is not 32 chars") val c = Cipher.getInstance("AES/CBC/PKCS5Padding") val sk = SecretKeySpec(secretKey.toByteArray(Charsets.UTF_8), "AES") diff --git a/app/src/main/java/com/example/tasklist/ChangePass.kt b/app/src/main/java/com/example/tasklist/ChangePass.kt index 591a8ab..84c0bfd 100644 --- a/app/src/main/java/com/example/tasklist/ChangePass.kt +++ b/app/src/main/java/com/example/tasklist/ChangePass.kt @@ -48,24 +48,32 @@ class ChangePass : Fragment(){ requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) val sharedPass = sharedPreference.getString("STRING_KEY", null) val oldPass = hash.hashString(binding.passwordOld.text.toString()) - var newPass = binding.passwordNew.text.toString() + val newPass = binding.passwordNew.text.toString() + val newPassHash = hash.hashString(newPass) if(oldPass.equals(sharedPass,false)) { - if (newPass.length >= 4){ - newPass = hash.hashString(newPass) - savePass(newPass) - Toast.makeText(requireActivity(),"Password updated!",Toast.LENGTH_SHORT).show() - findNavController().navigate(R.id.action_changePass_to_taskListFragment) - } - else{ - Toast.makeText(requireActivity(),"Password too short,it should have at least 4 characters!",Toast.LENGTH_SHORT).show() - findNavController().navigate(R.id.action_changePass_self) + when { + newPassHash.equals(sharedPass,false) -> { + Toast.makeText( requireActivity(), + "Password cannot be similar to the previous password!", + Toast.LENGTH_SHORT).show() + //findNavController().navigate(R.id.action_changePass_self) + } + newPass.length >= 4 -> { + savePass(newPassHash) + Toast.makeText(requireActivity(),"Password updated!",Toast.LENGTH_SHORT).show() + findNavController().navigate(R.id.action_changePass_to_taskListFragment) + } + else -> { + Toast.makeText(requireActivity(),"Password too short,it should have at least 4 characters!",Toast.LENGTH_SHORT).show() + //findNavController().navigate(R.id.action_changePass_self) + } } } else{ Toast.makeText(requireActivity(),"Password is incorrect, try again!",Toast.LENGTH_SHORT).show() - findNavController().navigate(R.id.action_changePass_self) + //findNavController().navigate(R.id.action_changePass_self) } } diff --git a/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt b/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt index 43db6de..f823147 100644 --- a/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt +++ b/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt @@ -1,11 +1,14 @@ package com.example.tasklist.ui.adapters +import android.os.Build import android.view.LayoutInflater import android.view.ViewGroup +import androidx.annotation.RequiresApi import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView +import com.example.tasklist.ChCrypto import com.example.tasklist.R import com.example.tasklist.data.Task import com.example.tasklist.databinding.ItemTaskBinding @@ -49,7 +52,12 @@ class TaskAdapter internal constructor( } interface TaskItemClickListener { - fun chooseTask(task: Task) + @RequiresApi(Build.VERSION_CODES.O) + fun chooseTask(task: Task): String { + val crypting = ChCrypto + val taskCrypred = crypting.aesDecrypt(task.name) + return taskCrypred + } } private class TaskDiffCallback : DiffUtil.ItemCallback () { diff --git a/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt b/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt index 1eb0a8b..e94c6d4 100644 --- a/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt +++ b/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt @@ -65,23 +65,15 @@ class TaskListFragment : Fragment(), TaskItemClickListener { } @RequiresApi(Build.VERSION_CODES.O) - override fun chooseTask(task: Task) { + override fun chooseTask(task: Task): String { val crypting = ChCrypto val taskCrypred = crypting.aesDecrypt(task.name) - val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie $taskCrypred", Toast.LENGTH_LONG) - taskToast.show() +// val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie $taskCrypred", Toast.LENGTH_LONG) +// taskToast.show() + return taskCrypred // val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie ${task.name}", Toast.LENGTH_LONG) } - @SuppressLint("ShowToast") - @RequiresApi(Build.VERSION_CODES.O) - fun decryptTasks(task: Task): String { - val crypting = ChCrypto - val taskDeCrypred = crypting.aesDecrypt(task.name) - val taskToast = Toast.makeText(activity,taskDeCrypred, Toast.LENGTH_LONG) - taskToast.show() - return taskDeCrypred - } fun addTask(){ findNavController().navigate(R.id.action_taskListFragment_to_adding_tasks) diff --git a/app/src/main/res/layout/item_task.xml b/app/src/main/res/layout/item_task.xml index 36bb166..b9253f2 100644 --- a/app/src/main/res/layout/item_task.xml +++ b/app/src/main/res/layout/item_task.xml @@ -30,7 +30,7 @@ android:id="@+id/checkBox_task" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text='@{tlf.decryptTasks(task) + "\n" + task.date}' + android:text='@{clickListener.chooseTask(task) + "\n" + task.date}' android:onClick="@{() -> clickListener.chooseTask(task)}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"