zarabotalo

This commit is contained in:
s452641 2021-11-30 19:59:36 +01:00
parent 0cba453479
commit 10079517ca
12 changed files with 141 additions and 97 deletions

View File

@ -11,6 +11,8 @@
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/item_task.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/main_activity.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/main_fragment.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA5/app/src/main/res/layout/fragment_task_list.xml" value="0.25" />
<entry key="..\:/Android Projects/TaskListA5/app/src/main/res/layout/item_task.xml" value="0.25" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/drawable/bordershape.xml" value="0.22135416666666666" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/layout/activity_login_page.xml" value="0.20520833333333333" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/layout/fragment_activity_login_page.xml" value="0.2682291666666667" />

View File

@ -1,21 +1,22 @@
package com.example.tasklist
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.tasklist.data.DataSource
import com.example.tasklist.data.Database
import com.example.tasklist.data.Task
import com.example.tasklist.databinding.FragmentAddingTasksBinding
import java.text.SimpleDateFormat
import java.util.*
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
// TODO: Rename parameter arguments, choose names that match
@ -59,8 +60,10 @@ class Adding_tasks : Fragment() {
val dateTask = simpleDateFormat.format(Date())
if (textTask != "") {
val crypting = ChCrypto
val taskCrypred = crypting.aesEncrypt(textTask)
val secretKey = getSecretKey()
val crypting = Cryption()
val iv = IvParameterSpec(ByteArray(16))
val taskCrypred = crypting.encrypt(textTask, secretKey, iv)
val taskToAdd = Task(taskCrypred, dateTask)
Database.taskDao.addTask(taskToAdd)
@ -68,4 +71,20 @@ class Adding_tasks : Fragment() {
findNavController().navigate(R.id.action_adding_tasks_to_taskListFragment)
}
private fun getPass(): String? {
val sharedPreference =
requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE)
val pass = sharedPreference.getString("KEY_SECRET", null)
return pass
}
private fun getSecretKey(): SecretKeySpec{
// val sharedPreference =
// requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
// val pass = sharedPreference.getString("STRING_KEY", "pass")
// Toast.makeText(requireActivity(),pass.toString(),Toast.LENGTH_LONG).show()
val secretKey = SecretKeySpec(getPass()?.removeRange(0,32)?.toByteArray(), "AES")
return secretKey
}
}

View File

@ -24,9 +24,7 @@ private object AES256{
@SuppressLint("RestrictedApi")
@RequiresApi(Build.VERSION_CODES.O)
private fun cipher(opmode:Int): Cipher {
val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX"
val key = SetPass()
//val secretKey = key.getKey()

View File

@ -44,22 +44,18 @@ class ChangePass : Fragment(){
fun goToList() {
val hash = HashString()
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
val sharedPass = sharedPreference.getString("STRING_KEY", null)
val oldPass = hash.hashString(binding.passwordOld.text.toString())
val newPass = binding.passwordNew.text.toString()
val newPassHash = hash.hashString(newPass)
if(oldPass.equals(sharedPass,false)) {
if(oldPass.equals(getPass(),false)) {
when {
newPassHash.equals(sharedPass,false) -> {
newPassHash.equals(getPass(),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)
@ -68,14 +64,12 @@ class ChangePass : Fragment(){
}
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)
}
}
@ -89,4 +83,11 @@ class ChangePass : Fragment(){
}.apply()
}
fun getPass(): String? {
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
val pass = sharedPreference.getString("STRING_KEY", null)
return pass
}
}

View File

@ -0,0 +1,34 @@
package com.example.tasklist
import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class Cryption {
@SuppressLint("GetInstance")
@RequiresApi(Build.VERSION_CODES.O)
fun decrypt(cipherText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.DECRYPT_MODE, key, iv)
val plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText))
return String(plainText)
}
@SuppressLint("GetInstance")
@RequiresApi(Build.VERSION_CODES.O)
fun encrypt(inputText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.ENCRYPT_MODE, key, iv)
val cipherText = cipher.doFinal(inputText.toByteArray())
return Base64.getEncoder().encodeToString(cipherText)
}
// val inputText = "abcdefghigklmnopqrstuvwxyz0123456789"
// val algorithm = "AES/CBC/PKCS5Padding"
// val key = SecretKeySpec("1234567890123456".toByteArray(), "AES")
// val iv = IvParameterSpec(ByteArray(16))
}

View File

@ -3,12 +3,6 @@ package com.example.tasklist
import java.security.MessageDigest
class HashString {
// fun hashString(input: String): String {
// return MessageDigest
// .getInstance("SHA-256")
// .digest(input.toByteArray())
// .fold("", { str, it -> str + "%02x".format(it) })
// }
fun hashString(input: String): String {
return MessageDigest
.getInstance("SHA-256")

View File

@ -1,6 +1,7 @@
package com.example.tasklist
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -10,7 +11,11 @@ import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.tasklist.databinding.FragmentLoginPageBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout
import java.security.SecureRandom
import java.util.*
import javax.crypto.spec.SecretKeySpec
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@ -24,6 +29,7 @@ private const val ARG_PARAM2 = "param2"
*/
class LoginPage : Fragment(){
private lateinit var binding: FragmentLoginPageBinding
private var counter: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -41,33 +47,45 @@ class LoginPage : Fragment(){
checkIfPassSet()
binding.login = this@LoginPage
return binding.root
}
fun goToList() {
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
val hash = HashString()
val insertedText = hash.hashString(binding.password.text.toString())
if(insertedText.equals(sharedPreference.getString("STRING_KEY", null),false)){
if(insertedText.equals(getPass(),false))
{
findNavController().navigate(R.id.action_loginPage_to_taskListFragment)
}
else{
Toast.makeText(requireActivity(),"Your password is incorrect. Try again!",Toast.LENGTH_SHORT).show()
findNavController().navigate(R.id.action_loginPage_self)
if (counter<=5){
counter += 1
Toast.makeText(requireActivity(),"Your password is incorrect. Try again!",Toast.LENGTH_SHORT).show()
//Toast.makeText(requireActivity(),counter,Toast.LENGTH_SHORT).show()
//findNavController().navigate(R.id.action_loginPage_self)
}
else{
Toast.makeText(requireActivity(),"You wrote incorrect password 5 times in a row. You bhave been timed out for 30 second!",Toast.LENGTH_LONG).show()
}
}
}
private fun checkIfPassSet(){
fun getPass(): String? {
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
if(sharedPreference.getString("STRING_KEY", null) == null){
val pass = sharedPreference.getString("STRING_KEY", null)
return pass
}
private fun checkIfPassSet(){
if(getPass() == null){
findNavController().navigate(R.id.action_loginPage_to_setPass)
}
}

View File

@ -1,25 +0,0 @@
package com.example.tasklist
import java.math.BigInteger
import java.security.SecureRandom
private fun generateSalt(): ByteArray{
val sr = SecureRandom()
val salt = ByteArray(32)
sr.nextBytes(salt)
println("MOJ SALT{${toHex(salt)}}:")
return salt
}
private fun toHex(array: ByteArray): String {
val bi = BigInteger(1, array)
val hex = bi.toString(16)
val paddingLength = array.size * 2 - hex.length
return if (paddingLength > 0) {
String.format("%0" + paddingLength + "d", 0) + hex
} else {
hex
}
}

View File

@ -1,5 +1,6 @@
package com.example.tasklist
import android.R.attr
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
@ -18,6 +19,13 @@ import java.security.MessageDigest
import java.security.SecureRandom
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import android.R.attr.key
import javax.crypto.Cipher
import android.preference.PreferenceManager
import android.content.SharedPreferences
import javax.crypto.spec.SecretKeySpec
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@ -54,6 +62,7 @@ class SetPass : Fragment() {
if (pass.length >= 4) {
savePass(pass)
saveHashForKey(pass)
//Toast.makeText(requireActivity(), "Password is set", Toast.LENGTH_SHORT).show()
findNavController().navigate(R.id.action_setPass_to_taskListFragment)
} else {
@ -64,7 +73,6 @@ class SetPass : Fragment() {
private fun savePass(pass: String) {
saveSecretKey()
val hash = HashString()
val password = hash.hashString(pass)
val sharedPreference =
@ -75,25 +83,14 @@ class SetPass : Fragment() {
}.apply()
}
private fun saveSecretKey(){
val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX"
private fun saveHashForKey(pass: String) {
val hash = HashString()
val password = hash.hashString(pass)
val sharedPreference =
requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE)
val editor = sharedPreference.edit()
editor.putString("secretKey", secretKey).apply()
Toast.makeText(requireActivity(),getKey(),Toast.LENGTH_LONG).show()
}
fun getKey(): String{
val sharedPreference =
requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE)
val sharedPass = sharedPreference.getString("secretKey", "DEFAULT")
val secretKey = sharedPass.toString()
return secretKey
editor.apply {
putString("KEY_SECRET", password)
}.apply()
}
}

View File

@ -1,5 +1,6 @@
package com.example.tasklist.ui.adapters
import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
@ -8,10 +9,15 @@ 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.Adding_tasks
import com.example.tasklist.Cryption
import com.example.tasklist.LoginPage
import com.example.tasklist.R
import com.example.tasklist.data.Task
import com.example.tasklist.databinding.ItemTaskBinding
import com.example.tasklist.ui.choose.list.TaskListFragment
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class TaskAdapter internal constructor(
@ -54,9 +60,11 @@ class TaskAdapter internal constructor(
interface TaskItemClickListener {
@RequiresApi(Build.VERSION_CODES.O)
fun chooseTask(task: Task): String {
val crypting = ChCrypto
val taskCrypred = crypting.aesDecrypt(task.name)
return taskCrypred
val iv = IvParameterSpec(ByteArray(16))
val secretKey = SecretKeySpec(TaskListFragment().getPass()?.removeRange(0,32)?.toByteArray(), "AES")
val crypting = Cryption()
return crypting.decrypt(task.name, secretKey, iv)
}
}

View File

@ -13,9 +13,7 @@ import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.tasklist.ChCrypto
import com.example.tasklist.MainApplication
import com.example.tasklist.R
import com.example.tasklist.*
import com.example.tasklist.data.DataSource
import com.example.tasklist.data.Database
import com.example.tasklist.data.Repository
@ -24,6 +22,8 @@ import com.example.tasklist.data.dao.TaskDao
import com.example.tasklist.databinding.FragmentTaskListBinding
import com.example.tasklist.ui.adapters.TaskAdapter
import com.example.tasklist.ui.adapters.TaskItemClickListener
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
/**
* A simple [Fragment] subclass.
@ -66,15 +66,22 @@ class TaskListFragment : Fragment(), TaskItemClickListener {
@RequiresApi(Build.VERSION_CODES.O)
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()
return taskCrypred
val iv = IvParameterSpec(ByteArray(16))
val secretKey = SecretKeySpec(getPass()?.removeRange(0,32)?.toByteArray(), "AES")
val crypting = Cryption()
return crypting.decrypt(task.name, secretKey, iv)
//Toast.makeText(activity, "Zostało wybrane zadanie $taskCrypred", Toast.LENGTH_LONG).show()
// val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie ${task.name}", Toast.LENGTH_LONG)
}
fun getPass(): String? {
val sharedPreference =
requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE)
val pass = sharedPreference.getString("KEY_SECRET", null)
return pass
}
fun addTask(){
findNavController().navigate(R.id.action_taskListFragment_to_adding_tasks)
}

View File

@ -10,16 +10,6 @@
<variable
name="task"
type="com.example.tasklist.data.Task" />
<variable
name="crypto"
type="com.example.tasklist.ChCrypto" />
<variable
name="tlf"
type="com.example.tasklist.ui.choose.list.TaskListFragment" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
@ -30,10 +20,11 @@
android:id="@+id/checkBox_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@{clickListener.chooseTask(task) + "\n" + task.date}'
android:onClick="@{() -> clickListener.chooseTask(task)}"
android:text='@{clickListener.chooseTask(task) + "\n" + task.date}'
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="158dp" />
app:layout_constraintVertical_bias="1.0"
tools:layout_editor_absoluteX="44dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>