<commit_before>package org.shadowice.flocke.andotp.Tasks;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.shadowice.flocke.andotp.Tasks.AuthenticationTask.Result;
import org.shadowice.flocke.andotp.Utilities.Constants.AuthMethod;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper.PBKDF2Credentials;
import org.shadowice.flocke.andotp.Utilities.Settings;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;

public class AuthenticationTask extends AsyncTask<Void, Void, Result> {

    private final Settings settings;
    private final Callback callback;

    private final boolean isAuthUpgrade;
    private final String existingAuthCredentials;
    private final String plainPassword;

    public AuthenticationTask(Context context, Callback callback, boolean isAuthUpgrade, String existingAuthCredentials, String plainPassword) {
        Context applicationContext = context.getApplicationContext();
        this.settings = new Settings(applicationContext);
        this.callback = callback;

        this.isAuthUpgrade = isAuthUpgrade;
        this.existingAuthCredentials = existingAuthCredentials;
        this.plainPassword = <PASSWORD>;
    }

    @Override
    @NonNull
    protected Result doInBackground(Void... ignore) {
        if (isAuthUpgrade) {
            return upgradeAuthentication();
        } else {
            return confirmAuthentication();
        }
    }

    @NonNull
    private Result upgradeAuthentication() {
        String hashedPassword = new String(Hex.encodeHex(DigestUtils.sha256(plainPassword)));
        if (!hashedPassword.equals(existingAuthCredentials))
            return Result.failure();

        byte[] key = settings.setAuthCredentials(plainPassword);

        AuthMethod authMethod = settings.getAuthMethod();
        if (authMethod == AuthMethod.PASSWORD)
            settings.removeAuthPasswordHash();
        else if (authMethod == AuthMethod.PIN)
            settings.removeAuthPINHash();

        if (key == null)
            return Result.upgradeFailure();
        else
            return Result.success(key);
    }

    @NonNull
    private Result confirmAuthentication() {
        try {
            PBKDF2Credentials credentials = EncryptionHelper.generatePBKDF2Credentials(plainPassword, settings.getSalt(), settings.getIterations());
            byte[] passwordArray = Base64.decode(existingAuthCredentials, Base64.URL_SAFE);

            if (Arrays.equals(passwordArray, credentials.password)) {
                return Result.success(credentials.key);
            }
            return Result.failure();
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalArgumentException e) {
            Log.e("DecodePasswordTask", "Problem decoding password", e);
            return Result.failure();
        }
    }

    @Override
    protected void onPostExecute(Result result) {
        callback.onComplete(result);
    }

    @FunctionalInterface
    public interface Callback {
        void onComplete(Result result);<commit_msg>#635 - Correct a misleading log tag<commit_after>package org.shadowice.flocke.andotp.Tasks;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.shadowice.flocke.andotp.Tasks.AuthenticationTask.Result;
import org.shadowice.flocke.andotp.Utilities.Constants.AuthMethod;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper.PBKDF2Credentials;
import org.shadowice.flocke.andotp.Utilities.Settings;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;

public class AuthenticationTask extends AsyncTask<Void, Void, Result> {

    private final Settings settings;
    private final Callback callback;

    private final boolean isAuthUpgrade;
    private final String existingAuthCredentials;
    private final String plainPassword;

    public AuthenticationTask(Context context, Callback callback, boolean isAuthUpgrade, String existingAuthCredentials, String plainPassword) {
        Context applicationContext = context.getApplicationContext();
        this.settings = new Settings(applicationContext);
        this.callback = callback;

        this.isAuthUpgrade = isAuthUpgrade;
        this.existingAuthCredentials = existingAuthCredentials;
        this.plainPassword = <PASSWORD>;
    }

    @Override
    @NonNull
    protected Result doInBackground(Void... ignore) {
        if (isAuthUpgrade) {
            return upgradeAuthentication();
        } else {
            return confirmAuthentication();
        }
    }

    @NonNull
    private Result upgradeAuthentication() {
        String hashedPassword = new String(Hex.encodeHex(DigestUtils.sha256(plainPassword)));
        if (!hashedPassword.equals(existingAuthCredentials))
            return Result.failure();

        byte[] key = settings.setAuthCredentials(plainPassword);

        AuthMethod authMethod = settings.getAuthMethod();
        if (authMethod == AuthMethod.PASSWORD)
            settings.removeAuthPasswordHash();
        else if (authMethod == AuthMethod.PIN)
            settings.removeAuthPINHash();

        if (key == null)
            return Result.upgradeFailure();
        else
            return Result.success(key);
    }

    @NonNull
    private Result confirmAuthentication() {
        try {
            PBKDF2Credentials credentials = EncryptionHelper.generatePBKDF2Credentials(plainPassword, settings.getSalt(), settings.getIterations());
            byte[] passwordArray = Base64.decode(existingAuthCredentials, Base64.URL_SAFE);

            if (Arrays.equals(passwordArray, credentials.password)) {
                return Result.success(credentials.key);
            }
            return Result.failure();
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalArgumentException e) {
            Log.e("AuthenticationTask", "Problem decoding password", e);
            return Result.failure();
        }
    }

    @Override
    protected void onPostExecute(Result result) {
        callback.onComplete(result);
    }

    @FunctionalInterface
    public interface Callback {
        void onComplete(Result result);
{'fraction_non_alphanumeric': 0.08587338804220399, 'fraction_numerical': 0.003956623681125439, 'mean_word_length': 3.3638107416879794, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2279075', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1734, 'n_words': 364}