Code Warrior snippets – found in trainings and courses

Validating the password:

 private fun validatePassword(password: String, passwordConfirmation: String): Boolean {

        val specialCharPattern = Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE)
        val upperCasePatten = Pattern.compile("[A-Z]")
        val lowerCasePatten = Pattern.compile("[a-z]")
        val digitCasePatten = Pattern.compile("[0-9]")

        if (TextUtils.isEmpty(password) || password.length <= 6) {
            textInputPassword.error = "Password must contain more than 6 characters"
            return false
        }

        if (password.toLowerCase().equals(password) || password.toUpperCase().equals(password)) {
            textInputPassword.error = "Password must contain lowercase and upper case letters"
            return false
        }

        if (!specialCharPattern.matcher(password).find()) {
            textInputPassword.error = "Password must have at least one special character"
            return false
        }
        if (!upperCasePatten.matcher(password).find()) {
            textInputPassword.error = "Password must have at least one uppercase character"
            return false
        }
        if (!lowerCasePatten.matcher(password).find()) {
            textInputPassword.error = "Password must have at least one lowercase character"
            return false
        }
        if (!digitCasePatten.matcher(password).find()) {
            textInputPassword.error = "Password must have at least one digit character"
            return false
        }
        if (!password.equals(passwordConfirmation)) {
            textInputPassword.error = "Password and confirm password does not match"
            return false
        }

        textInputPassword.error = ""

        return true;
    }

Safe storing and restoring credentials:

private fun saveCredentials(plainTextPassword: String, plainTextUsername: String) {
        val file = this.getSharedPreferences(CREDENTIAL_FILE_NAME, android.content.Context.MODE_PRIVATE)
        val cs = CipherSecurity()
        val alias = "com.xxx.yyy.zzz.alias"
        cs.createNewKeys(alias)

        val encryptedPassword = cs.encrypt(alias, plainTextPassword.toByteArray())
        val passwordIV: String = Base64.encodeToString(cs.IV, DEFAULT_BUFFER_SIZE)

        val encrypedUsername = cs.encrypt(alias, plainTextUsername.toByteArray())
        val usernameIV: String = Base64.encodeToString(cs.IV, DEFAULT_BUFFER_SIZE)

        with(file.edit()) {
            putString(CREDENTIAL_PASSWORD, encryptedPassword)
            putString(CREDENTIAL_PASSWORD_IV, passwordIV)

            putString(CREDENTIAL_USERNAME, encrypedUsername)
            putString(CREDENTIAL_USERNAME_IV, usernameIV)

            apply()
        }
    }
private fun readCredentials(): List<String> {
        val file = this.getSharedPreferences(CREDENTIAL_FILE_NAME, android.content.Context.MODE_PRIVATE)
        val cs = CipherSecurity()
        val alias = "com.xxx.yyy.zzz.alias"
        val res = mutableListOf<String>()

        //get and decrypt creds
        val encryptedPassword = file.getString(CREDENTIAL_PASSWORD, "")
        val passwordIV = Base64.decode(file.getString(CREDENTIAL_PASSWORD_IV, ""), DEFAULT_BUFFER_SIZE)
        val decryptedPassword = cs.decryptString(alias, encryptedPassword, passwordIV)

        val encryptedUsername = file.getString(CREDENTIAL_USERNAME, "")
        val usernameIV = Base64.decode(file.getString(CREDENTIAL_USERNAME_IV, ""), DEFAULT_BUFFER_SIZE)
        val decryptedUsername = cs.decryptString(alias, encryptedUsername, usernameIV)

        res.add(decryptedUsername)
        res.add(decryptedPassword)

        return res
    }

Leave a Reply

Your email address will not be published.




Enter Captcha Here :