Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2011 Whisper Systems
 * Copyright (C) 2013 Open Whisper Systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.util.Log;

import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class Main {
    private static int generateIterationCount(String passphrase, byte[] salt) {
        int TARGET_ITERATION_TIME = 50; //ms
        int MINIMUM_ITERATION_COUNT = 100; //default for low-end devices
        int BENCHMARK_ITERATION_COUNT = 10000; //baseline starting iteration count

        try {
            PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, BENCHMARK_ITERATION_COUNT);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC");

            long startTime = System.currentTimeMillis();
            skf.generateSecret(keyspec);
            long finishTime = System.currentTimeMillis();

            int scaledIterationTarget = (int) (((double) BENCHMARK_ITERATION_COUNT
                    / (double) (finishTime - startTime)) * TARGET_ITERATION_TIME);

            if (scaledIterationTarget < MINIMUM_ITERATION_COUNT)
                return MINIMUM_ITERATION_COUNT;
            else
                return scaledIterationTarget;
        } catch (NoSuchAlgorithmException e) {
            Log.w("MasterSecretUtil", e);
            return MINIMUM_ITERATION_COUNT;
        } catch (InvalidKeySpecException e) {
            Log.w("MasterSecretUtil", e);
            return MINIMUM_ITERATION_COUNT;
        }
    }
}