Example usage for java.security KeyException KeyException

List of usage examples for java.security KeyException KeyException

Introduction

In this page you can find the example usage for java.security KeyException KeyException.

Prototype

public KeyException(Throwable cause) 

Source Link

Document

Creates a KeyException with the specified cause and a detail message of (cause==null ?

Usage

From source file:it.evilsocket.dsploit.core.UpdateService.java

/**
 * check if mLocalFile exists.//from  www .  j a v a2 s  .co m
 *
 * @return true if file exists and match md5sum and sha1sum.
 * @throws java.util.concurrent.CancellationException when check is cancelled by user
 * @throws SecurityException bad file permissions
 * @throws IOException when IOException occurs
 * @throws java.security.NoSuchAlgorithmException when digests cannot be created
 * @throws java.security.KeyException when file checksum fails
 */
private boolean haveLocalFile()
        throws CancellationException, SecurityException, IOException, NoSuchAlgorithmException, KeyException {

    File file = null;
    InputStream reader = null;
    boolean exitForError = true;

    if (mCurrentTask.path == null)
        return false;

    try {
        MessageDigest md5, sha1;
        byte[] buffer;
        int read;
        short percentage, previous_percentage;
        long read_counter, total;

        file = new File(mCurrentTask.path);
        buffer = new byte[4096];
        total = file.length();
        read_counter = 0;
        previous_percentage = -1;

        if (!file.exists() || !file.isFile())
            return false;

        if (!file.canWrite() || !file.canRead()) {
            read = -1;
            try {
                read = Shell.exec(String.format("chmod 777 '%s'", mCurrentTask.path));
            } catch (Exception e) {
                System.errorLogging(e);
            }
            if (read != 0)
                throw new SecurityException(String.format("bad file permissions for '%s', chmod returned: %d",
                        mCurrentTask.path, read));
        }

        if (mCurrentTask.md5 != null || mCurrentTask.sha1 != null) {
            mBuilder.setContentTitle(getString(R.string.checking))
                    .setSmallIcon(android.R.drawable.ic_popup_sync).setContentText("")
                    .setProgress(100, 0, false);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

            md5 = (mCurrentTask.md5 != null ? MessageDigest.getInstance("MD5") : null);
            sha1 = (mCurrentTask.sha1 != null ? MessageDigest.getInstance("SHA-1") : null);

            reader = new FileInputStream(file);
            while (mRunning && (read = reader.read(buffer)) != -1) {
                if (md5 != null)
                    md5.update(buffer, 0, read);
                if (sha1 != null)
                    sha1.update(buffer, 0, read);

                read_counter += read;

                percentage = (short) (((double) read_counter / total) * 100);
                if (percentage != previous_percentage) {
                    mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
                    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                    previous_percentage = percentage;
                }
            }
            reader.close();
            reader = null;
            if (!mRunning) {
                exitForError = false;
                throw new CancellationException("local file check cancelled");
            }
            if (md5 != null && !mCurrentTask.md5.equals(digest2string(md5.digest())))
                throw new KeyException("wrong MD5");
            if (sha1 != null && !mCurrentTask.sha1.equals(digest2string(sha1.digest())))
                throw new KeyException("wrong SHA-1");
            Logger.info(String.format("checksum ok: '%s'", mCurrentTask.path));
        } else if (mCurrentTask.archiver != null) {
            verifyArchiveIntegrity();
        }
        Logger.info(String.format("file already exists: '%s'", mCurrentTask.path));
        mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done)
                .setContentTitle(getString(R.string.update_available))
                .setContentText(getString(R.string.click_here_to_upgrade)).setProgress(0, 0, false) // remove progress bar
                .setAutoCancel(true);
        exitForError = false;
        return true;
    } finally {
        if (exitForError && file != null && file.exists() && !file.delete())
            Logger.error(String.format("cannot delete local file '%s'", mCurrentTask.path));
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            System.errorLogging(e);
        }
    }
}

From source file:org.csploit.android.core.UpdateService.java

/**
 * check if mLocalFile exists./*ww w . j a v a 2s.  co m*/
 *
 * @return true if file exists and match md5sum and sha1sum.
 * @throws java.util.concurrent.CancellationException when check is cancelled by user
 * @throws SecurityException bad file permissions
 * @throws IOException when IOException occurs
 * @throws java.security.NoSuchAlgorithmException when digests cannot be created
 * @throws java.security.KeyException when file checksum fails
 */
private boolean haveLocalFile()
        throws CancellationException, SecurityException, IOException, NoSuchAlgorithmException, KeyException {

    File file = null;
    InputStream reader = null;
    boolean exitForError = true;

    if (mCurrentTask.path == null)
        return false;

    try {
        MessageDigest md5, sha1;
        byte[] buffer;
        int read;
        short percentage, previous_percentage;
        long read_counter, total;

        file = new File(mCurrentTask.path);
        buffer = new byte[4096];
        total = file.length();
        read_counter = 0;
        previous_percentage = -1;

        if (!file.exists() || !file.isFile())
            return false;

        if (!file.canWrite() || !file.canRead()) {
            read = -1;
            try {
                read = System.getTools().raw.run(String.format("chmod 777 '%s'", mCurrentTask.path));
            } catch (Exception e) {
                System.errorLogging(e);
            }
            if (read != 0)
                throw new SecurityException(String.format("bad file permissions for '%s', chmod returned: %d",
                        mCurrentTask.path, read));
        }

        if (mCurrentTask.md5 != null || mCurrentTask.sha1 != null) {
            mBuilder.setContentTitle(getString(R.string.checking))
                    .setSmallIcon(android.R.drawable.ic_popup_sync).setContentText("")
                    .setProgress(100, 0, false);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

            md5 = (mCurrentTask.md5 != null ? MessageDigest.getInstance("MD5") : null);
            sha1 = (mCurrentTask.sha1 != null ? MessageDigest.getInstance("SHA-1") : null);

            reader = new FileInputStream(file);
            while (mRunning && (read = reader.read(buffer)) != -1) {
                if (md5 != null)
                    md5.update(buffer, 0, read);
                if (sha1 != null)
                    sha1.update(buffer, 0, read);

                read_counter += read;

                percentage = (short) (((double) read_counter / total) * 100);
                if (percentage != previous_percentage) {
                    mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
                    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                    previous_percentage = percentage;
                }
            }
            reader.close();
            reader = null;
            if (!mRunning) {
                exitForError = false;
                throw new CancellationException("local file check cancelled");
            }
            if (md5 != null && !mCurrentTask.md5.equals(digest2string(md5.digest())))
                throw new KeyException("wrong MD5");
            if (sha1 != null && !mCurrentTask.sha1.equals(digest2string(sha1.digest())))
                throw new KeyException("wrong SHA-1");
            Logger.info(String.format("checksum ok: '%s'", mCurrentTask.path));
        } else if (mCurrentTask.archiver != null) {
            verifyArchiveIntegrity();
        }
        Logger.info(String.format("file already exists: '%s'", mCurrentTask.path));
        mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done)
                .setContentTitle(getString(R.string.update_available))
                .setContentText(getString(R.string.click_here_to_upgrade)).setProgress(0, 0, false) // remove progress bar
                .setAutoCancel(true);
        exitForError = false;
        return true;
    } finally {
        if (exitForError && file != null && file.exists() && !file.delete())
            Logger.error(String.format("cannot delete local file '%s'", mCurrentTask.path));
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            System.errorLogging(e);
        }
    }
}

From source file:it.evilsocket.dsploit.core.UpdateService.java

/**
 * download mCurrentTask.url to mCurrentTask.path
 *
 * @throws KeyException when MD5 or SHA1 sum fails
 * @throws IOException when IOError occurs
 * @throws NoSuchAlgorithmException when required digest algorithm is not available
 * @throws CancellationException when user cancelled the download via notification
 *//*  ww w.  j  a  v a2 s. c  o  m*/
private void downloadFile()
        throws SecurityException, KeyException, IOException, NoSuchAlgorithmException, CancellationException {
    if (mCurrentTask.url == null || mCurrentTask.path == null)
        return;

    File file = null;
    FileOutputStream writer = null;
    InputStream reader = null;
    HttpURLConnection connection = null;
    boolean exitForError = true;

    try {
        MessageDigest md5, sha1;
        URL url;
        byte[] buffer;
        int read;
        short percentage, previous_percentage;
        long downloaded, total;

        mBuilder.setContentTitle(getString(R.string.downloading_update))
                .setContentText(getString(R.string.connecting))
                .setSmallIcon(android.R.drawable.stat_sys_download).setProgress(100, 0, true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        md5 = (mCurrentTask.md5 != null ? MessageDigest.getInstance("MD5") : null);
        sha1 = (mCurrentTask.sha1 != null ? MessageDigest.getInstance("SHA-1") : null);
        buffer = new byte[4096];
        file = new File(mCurrentTask.path);

        if (file.exists() && file.isFile())
            //noinspection ResultOfMethodCallIgnored
            file.delete();

        HttpURLConnection.setFollowRedirects(true);
        url = new URL(mCurrentTask.url);
        connection = (HttpURLConnection) url.openConnection();

        connection.connect();

        writer = new FileOutputStream(file);
        reader = connection.getInputStream();

        total = connection.getContentLength();
        read = connection.getResponseCode();

        if (read != 200)
            throw new IOException(
                    String.format("cannot download '%s': responseCode: %d", mCurrentTask.url, read));

        downloaded = 0;
        previous_percentage = -1;

        mBuilder.setContentText(file.getName());
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        Logger.info(String.format("downloading '%s' to '%s'", mCurrentTask.url, mCurrentTask.path));

        while (mRunning && (read = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, read);
            if (md5 != null)
                md5.update(buffer, 0, read);
            if (sha1 != null)
                sha1.update(buffer, 0, read);

            if (total >= 0) {
                downloaded += read;

                percentage = (short) (((double) downloaded / total) * 100);

                if (percentage != previous_percentage) {
                    mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
                    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                    previous_percentage = percentage;
                }
            }
        }

        if (!mRunning)
            throw new CancellationException("download cancelled");

        Logger.info("download finished successfully");

        if (md5 != null || sha1 != null) {
            if (md5 != null && !mCurrentTask.md5.equals(digest2string(md5.digest()))) {
                throw new KeyException("wrong MD5");
            } else if (sha1 != null && !mCurrentTask.sha1.equals(digest2string(sha1.digest()))) {
                throw new KeyException("wrong SHA-1");
            }
        } else if (mCurrentTask.archiver != null) {
            verifyArchiveIntegrity();
        }

        exitForError = false;

    } finally {
        if (exitForError && file != null && file.exists() && !file.delete())
            Logger.error(String.format("cannot delete file '%s'", mCurrentTask.path));
        try {
            if (writer != null)
                writer.close();
            if (reader != null)
                reader.close();
            if (connection != null)
                connection.disconnect();
        } catch (IOException e) {
            System.errorLogging(e);
        }
    }
}

From source file:org.ow2.proactive.scheduler.rest.SchedulerClient.java

@Override
public void putThirdPartyCredential(String key, String value)
        throws NotConnectedException, PermissionException, KeyException {
    try {// w ww.  j a  v  a2  s.  c o  m
        restApi().putThirdPartyCredential(sid, key, value);
    } catch (NotConnectedRestException e) {
        throw new NotConnectedException(e);
    } catch (PermissionRestException e) {
        throw new PermissionException(e);
    } catch (SchedulerRestException e) {
        throw new KeyException(e);
    }
}