From 8062ee5f885203ab1b414031bf72704d68ddac88 Mon Sep 17 00:00:00 2001 From: s452641 Date: Sun, 21 Nov 2021 15:10:52 +0100 Subject: [PATCH] MD5(new) --- .../java/com/example/tasklist/ChangePass.kt | 40 ++++--------------- .../java/com/example/tasklist/LoginPage.kt | 36 ++--------------- .../main/java/com/example/tasklist/SetPass.kt | 36 ++--------------- 3 files changed, 16 insertions(+), 96 deletions(-) diff --git a/app/src/main/java/com/example/tasklist/ChangePass.kt b/app/src/main/java/com/example/tasklist/ChangePass.kt index c534f60..5216eba 100644 --- a/app/src/main/java/com/example/tasklist/ChangePass.kt +++ b/app/src/main/java/com/example/tasklist/ChangePass.kt @@ -15,6 +15,7 @@ import android.content.SharedPreferences import android.net.wifi.WifiEnterpriseConfig import android.widget.Toast import java.lang.Exception +import java.math.BigInteger import java.security.MessageDigest @@ -33,6 +34,10 @@ class ChangePass : Fragment(){ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + val sharedPreference = + requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) + val savedString = sharedPreference.getString("STRING_KEY", null) + Toast.makeText(requireActivity(),savedString,Toast.LENGTH_SHORT).show() } override fun onCreateView( @@ -87,38 +92,9 @@ class ChangePass : Fragment(){ } - private fun byteArrayToHexString( array: Array ): String { - - val result = StringBuilder(array.size * 2) - - for ( byte in array ) { - - val toAppend = - String.format("%2X", byte).replace(" ", "0") // hexadecimal - result.append(toAppend).append("-") - } - result.setLength(result.length - 1) // remove last '-' - - return result.toString() - } - - - private fun toMD5Hash( text: String ): String { - - var result = "" - - result = try { - - val md5 = MessageDigest.getInstance("MD5") - val md5HashBytes = md5.digest(text.toByteArray()).toTypedArray() - - byteArrayToHexString(md5HashBytes) - } catch ( e: Exception) { - - "error: ${e.message}" - } - - return result + private fun toMD5Hash(input:String): String { + val md = MessageDigest.getInstance("MD5") + return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') } diff --git a/app/src/main/java/com/example/tasklist/LoginPage.kt b/app/src/main/java/com/example/tasklist/LoginPage.kt index b661214..49af8d2 100644 --- a/app/src/main/java/com/example/tasklist/LoginPage.kt +++ b/app/src/main/java/com/example/tasklist/LoginPage.kt @@ -12,6 +12,7 @@ import com.example.tasklist.databinding.FragmentLoginPageBinding import android.content.SharedPreferences import android.widget.Toast import java.lang.Exception +import java.math.BigInteger import java.security.MessageDigest import java.util.* @@ -79,38 +80,9 @@ class LoginPage : Fragment(){ } } - private fun byteArrayToHexString( array: Array ): String { - - val result = StringBuilder(array.size * 2) - - for ( byte in array ) { - - val toAppend = - String.format("%2X", byte).replace(" ", "0") // hexadecimal - result.append(toAppend).append("-") - } - result.setLength(result.length - 1) // remove last '-' - - return result.toString() - } - - - private fun toMD5Hash( text: String ): String { - - var result = "" - - result = try { - - val md5 = MessageDigest.getInstance("MD5") - val md5HashBytes = md5.digest(text.toByteArray()).toTypedArray() - - byteArrayToHexString(md5HashBytes) - } catch ( e: Exception) { - - "error: ${e.message}" - } - - return result + private fun toMD5Hash(input:String): String { + val md = MessageDigest.getInstance("MD5") + return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') } } \ No newline at end of file diff --git a/app/src/main/java/com/example/tasklist/SetPass.kt b/app/src/main/java/com/example/tasklist/SetPass.kt index d74c812..286ad4b 100644 --- a/app/src/main/java/com/example/tasklist/SetPass.kt +++ b/app/src/main/java/com/example/tasklist/SetPass.kt @@ -11,6 +11,7 @@ import androidx.databinding.DataBindingUtil import androidx.navigation.fragment.findNavController import com.example.tasklist.databinding.FragmentSetPassBinding import java.lang.Exception +import java.math.BigInteger import java.security.MessageDigest // TODO: Rename parameter arguments, choose names that match @@ -69,38 +70,9 @@ class SetPass : Fragment() { }.apply() } - private fun byteArrayToHexString( array: Array ): String { - - val result = StringBuilder(array.size * 2) - - for ( byte in array ) { - - val toAppend = - String.format("%2X", byte).replace(" ", "0") // hexadecimal - result.append(toAppend).append("-") - } - result.setLength(result.length - 1) // remove last '-' - - return result.toString() - } - - - private fun toMD5Hash( text: String ): String { - - var result = "" - - result = try { - - val md5 = MessageDigest.getInstance("MD5") - val md5HashBytes = md5.digest(text.toByteArray()).toTypedArray() - - byteArrayToHexString(md5HashBytes) - } catch ( e: Exception ) { - - "error: ${e.message}" - } - - return result + private fun toMD5Hash(input:String): String { + val md = MessageDigest.getInstance("MD5") + return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') }