Example usage for java.security NoSuchAlgorithmException printStackTrace

List of usage examples for java.security NoSuchAlgorithmException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.ibm.opensirf.jaxrs.ObjectApi.java

private String getSHA1(byte[] b) {
    try {/*from  w w w .ja  v  a  2 s.  c  o  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        return bytesToHex(md.digest(b));
    } catch (NoSuchAlgorithmException nsae) {
        nsae.printStackTrace();
        return null;
    }
}

From source file:com.liato.bankdroid.banking.banks.lansforsakringar.Lansforsakringar.java

private String generateChallenge(int originalChallenge) {
    try {//from www  .j  a v a  2s . c  o  m
        String h = Integer.toHexString(originalChallenge + (1000 * 20 / 4) + 100 * (18 / 3) + 10 * (2 / 2) + 6);
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] messageDigest = md.digest(h.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);
        while (md5.length() < 40)
            md5 = "0" + md5;
        return md5;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";

}

From source file:AuthSSLProtocolSocketFactory.java

private SSLContext createSSLContext() {
    try {// w  ww  .  j  a  va  2s  .  c o m
        KeyManager[] keymanagers = null;
        TrustManager[] trustmanagers = null;
        if (this.keystoreUrl != null) {
            KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword);
            Enumeration aliases = keystore.aliases();
            while (aliases.hasMoreElements()) {
                String alias = (String) aliases.nextElement();
                Certificate[] certs = keystore.getCertificateChain(alias);
                if (certs != null) {
                    System.out.println("Certificate chain '" + alias + "':");
                    for (int c = 0; c < certs.length; c++) {
                        if (certs[c] instanceof X509Certificate) {
                            X509Certificate cert = (X509Certificate) certs[c];
                            System.out.println(" Certificate " + (c + 1) + ":");
                            System.out.println("  Subject DN: " + cert.getSubjectDN());
                            System.out.println("  Signature Algorithm: " + cert.getSigAlgName());
                            System.out.println("  Valid from: " + cert.getNotBefore());
                            System.out.println("  Valid until: " + cert.getNotAfter());
                            System.out.println("  Issuer: " + cert.getIssuerDN());
                        }
                    }
                }
            }
            keymanagers = createKeyManagers(keystore, this.keystorePassword);
        }
        if (this.truststoreUrl != null) {
            KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword);
            Enumeration aliases = keystore.aliases();
            while (aliases.hasMoreElements()) {
                String alias = (String) aliases.nextElement();
                System.out.println("Trusted certificate '" + alias + "':");
                Certificate trustedcert = keystore.getCertificate(alias);
                if (trustedcert != null && trustedcert instanceof X509Certificate) {
                    X509Certificate cert = (X509Certificate) trustedcert;
                    System.out.println("  Subject DN: " + cert.getSubjectDN());
                    System.out.println("  Signature Algorithm: " + cert.getSigAlgName());
                    System.out.println("  Valid from: " + cert.getNotBefore());
                    System.out.println("  Valid until: " + cert.getNotAfter());
                    System.out.println("  Issuer: " + cert.getIssuerDN());
                }
            }
            trustmanagers = createTrustManagers(keystore);
        }
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(keymanagers, trustmanagers, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage());
    } catch (KeyStoreException e) {
        e.printStackTrace();
        throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage());
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
        throw new AuthSSLInitializationError("Key management exception: " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage());
    }
}

From source file:de.codecentric.jira.jenkins.plugin.servlet.RecentBuildsServlet.java

public RecentBuildsServlet(TemplateRenderer templateRenderer, JiraAuthenticationContext authenticationContext,
        PluginSettingsFactory settingsFactory, ApplicationProperties applicationProperties) {
    this.templateRenderer = templateRenderer;
    this.authenticationContext = authenticationContext;
    this.client = new HttpClient(new MultiThreadedHttpConnectionManager());
    this.serverList = new ServerList(settingsFactory);

    //test if jiraversion < 4.3
    IsPriorToJiraVersion isPrior = new IsPriorToJiraVersion(applicationProperties);
    isPrior.setmaxMajorVersion(4);//from www  . j a  va  2s. c  om
    isPrior.setmaxMinorVersion(3);
    this.old = isPrior.shouldDisplay(null);

    client.getParams().setAuthenticationPreemptive(true);

    //set SSLContext to accept all certificates
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        SSLContext.setDefault(ctx);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    SecureProtocolSocketFactory secureProtocolSocketFactory = new SSLProtocolSocketFactory();

    Protocol.registerProtocol("https",
            new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 443));

}

From source file:mendeley2kindle.KindleDAO.java

private String toKindleHash(String kindlePath) {
    MessageDigest md;/*from   w w  w.j a  v  a2  s .c  om*/
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        assert false;
        return null;
    }
    byte[] sha1bin = md.digest(kindlePath.getBytes());
    return "*" + bytes2hex(sha1bin);
}

From source file:com.base.net.volley.toolbox.HurlStack.java

private SSLSocketFactory getDefaultSSLSocketFactory() {
    SSLSocketFactory mySSLSocketFactory = null;
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from  w w w.ja  v  a 2 s .c  o m

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };

    // Install the all-trusting trust manager

    SSLContext sc;
    try {
        sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        mySSLSocketFactory = sc.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mySSLSocketFactory;
}

From source file:com.konakart.actions.gateways.WorldPayXMLRedirectResponseAction.java

/**
 * Calculates a hex MD5 based on input./*ww w  .j  a v  a  2  s .  c om*/
 * 
 * @param message
 *            String to calculate MD5 of.
 * 
 */
private String md5(String message) throws java.security.NoSuchAlgorithmException {
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (java.security.NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        if (log.isDebugEnabled()) {
            log.debug(ex);
        }
        throw ex;
    }
    byte[] dig = md5.digest(message.getBytes());
    StringBuffer code = new StringBuffer();
    for (int i = 0; i < dig.length; ++i) {
        code.append(Integer.toHexString(0x0100 + (dig[i] & 0x00FF)).substring(1));
    }
    return code.toString();
}

From source file:org.jmangos.auth.wow.controller.AccountController.java

public WoWAuthResponse checkPassword(final AccountInfo account, final byte[] a, final byte[] m1) {

    logger.debug("a length " + a.length);
    logger.debug("a value " + new BigInteger(1, a).toString(16).toUpperCase());
    logger.debug("m1 length " + m1.length);
    logger.debug("m1 value " + new BigInteger(1, m1).toString(16).toUpperCase());
    MessageDigest sha = null;/*from w  w  w  .  j  av  a2  s.c om*/
    try {
        sha = MessageDigest.getInstance("SHA-1");
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
        return WoWAuthResponse.WOW_FAIL_INCORRECT_PASSWORD;
    }
    final BigNumber B = account.getcryptoB();
    logger.debug("B value " + B.asHexStr());
    sha.update(a);
    sha.update(B.asByteArray(32));
    final BigNumber u = new BigNumber();
    u.setBinary(sha.digest());
    logger.debug("u value" + u.asHexStr());
    final BigNumber A = new BigNumber();
    A.setBinary(a);
    logger.debug("A:" + A.asHexStr());
    final BigNumber S = A.multiply((account.getV_crypto().modPow(u, AccountUtils.N))).modPow(account.getB(),
            AccountUtils.N);

    final byte[] t1 = new byte[16];
    final byte[] vK = new byte[40];

    final byte[] t = S.asByteArray(32);
    for (int i = 0; i < 16; ++i) {
        t1[i] = t[i * 2];
    }
    sha.update(t1);
    byte[] t2 = sha.digest();
    for (int i = 0; i < 20; ++i) {
        vK[i * 2] = t2[i];
    }
    for (int i = 0; i < 16; ++i) {
        t1[i] = t[(i * 2) + 1];
    }
    sha.update(t1);
    t2 = sha.digest();
    for (int i = 0; i < 20; ++i) {
        vK[(i * 2) + 1] = t2[i];
    }

    byte[] hash = new byte[20];
    logger.debug("N:" + AccountUtils.N.asHexStr());
    sha.update(AccountUtils.N.asByteArray(32));
    hash = sha.digest();
    logger.debug("hash:" + new BigInteger(1, hash).toString(16).toUpperCase());
    sha.update(AccountUtils.g.asByteArray(1));
    final byte[] gH = sha.digest();
    for (int i = 0; i < 20; ++i) {
        hash[i] ^= gH[i];
    }

    byte[] t4 = new byte[20];
    sha.update(account.getName().toUpperCase().getBytes(Charset.forName("UTF-8")));
    t4 = sha.digest();

    sha.update(hash);
    sha.update(t4);
    sha.update(account.getS_crypto().asByteArray(32));
    sha.update(A.asByteArray(32));
    sha.update(B.asByteArray(32));
    sha.update(vK);

    final byte[] sh = sha.digest();

    if (Arrays.equals(sh, m1)) {
        sha.update(A.asByteArray(32));
        sha.update(sh);
        sha.update(vK);

        account.setM2(sha.digest());
        account.setvK(vK);
        ArrayUtils.reverse(vK);

        final AccountEntity accountEntity = this.accountService.readAccountByUserName(account.getName());
        if (accountEntity != null) {
            final String sessionKey = new BigInteger(1, vK).toString(16).toUpperCase();
            accountEntity.setSessionKey(sessionKey);
            account.setSessionKey(new BigNumber(vK));
            this.accountService.createOrUpdateAccount(accountEntity);
        } else {
            return WoWAuthResponse.WOW_FAIL_INCORRECT_PASSWORD;
        }
        return WoWAuthResponse.WOW_SUCCESS;
    } else {
        return WoWAuthResponse.WOW_FAIL_INCORRECT_PASSWORD;
    }
}

From source file:org.openmuc.framework.driver.rest.RestConnection.java

public RestConnection(String deviceAddress, String credentials, int timeout) throws ConnectionException {

    this.timeout = timeout;
    wrapper = new JsonWrapper();
    authString = new String(Base64.encodeBase64(credentials.getBytes()));

    if (!deviceAddress.endsWith("/")) {
        this.deviceAddress = deviceAddress + "/channels/";
    } else {//from   www  . j a v a2 s.  c o  m
        this.deviceAddress = deviceAddress + "channels/";
    }

    if (deviceAddress.startsWith("https://")) {
        isHTTPS = true;
    } else {
        isHTTPS = false;
    }

    if (isHTTPS) {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (KeyManagementException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        // HttpsURLConnection.setFollowRedirects(false);
    }
}