Example usage for java.security NoSuchAlgorithmException getMessage

List of usage examples for java.security NoSuchAlgorithmException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.ovea.facebook.client.DefaultFacebookClient.java

public DefaultFacebookClient(String client_id, String client_secret, String redirect_uri) {
    this.clientId = client_id;
    this.clientSecret = client_secret;
    this.redirectUri = redirect_uri;
    try {/*  w  w w . ja  v a 2 s . c  o  m*/
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } }, new SecureRandom());
        sslSocketFactory = new SSLSocketFactory(sslContext);
        //noinspection deprecation
        sslSocketFactory.setHostnameVerifier(new X509HostnameVerifier() {
            @Override
            public void verify(String host, SSLSocket ssl) throws IOException {
            }

            @Override
            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
    } catch (NoSuchAlgorithmException e) {
        throw new FacebookException(e.getMessage(), e);
    } catch (KeyManagementException e) {
        throw new FacebookException(e.getMessage(), e);
    }
}

From source file:be.e_contract.eid.applet.service.impl.handler.IdentityDataMessageHandler.java

private byte[] digestPhoto(String digestAlgoName, byte[] photoFile) {
    MessageDigest messageDigest;//from w w  w. j  ava 2 s  . co  m
    try {
        messageDigest = MessageDigest.getInstance(digestAlgoName);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("digest error: " + e.getMessage(), e);
    }
    byte[] photoDigest = messageDigest.digest(photoFile);
    return photoDigest;
}

From source file:com.sfs.upload.S3FileUploadImpl.java

/**
 * Upload the identified File to the defined storage pool.
 *
 * @param file the file/* w ww  .  j av a 2 s.  c  o  m*/
 *
 * @return the file upload details
 *
 * @throws FileUploadException the file upload exception
 */
public final FileUploadDetails upload(final File file) throws FileUploadException {
    if (file == null) {
        throw new FileUploadException("The File object cannot be null");
    }
    if (!file.exists()) {
        throw new FileUploadException("No file exists to upload");
    }
    if (StringUtils.isBlank(this.awsAccessKey)) {
        throw new FileUploadException("An AWS access key is required");
    }
    if (StringUtils.isBlank(this.awsSecretKey)) {
        throw new FileUploadException("An AWS secret key is required");
    }
    if (StringUtils.isBlank(this.awsBucketName)) {
        throw new FileUploadException("An AWS bucket name is required");
    }

    FileUploadDetails details = null;

    final Date currentDate = Calendar.getInstance().getTime();
    // Add a prefix to ensure that there isn't a duplicate file name issue.
    final String format = "yyyyMMddhhmm_";
    final String fileName = Formatter.numericDate(currentDate, format) + file.getName();

    try {
        AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey.trim(), awsSecretKey.trim());

        Jets3tProperties jp = new Jets3tProperties();

        String proxyHost = System.getProperty("https.proxyHost");
        String proxyPortString = System.getProperty("https.proxyPort");
        int proxyPort = -1;

        try {
            proxyPort = Integer.parseInt(proxyPortString);
        } catch (NumberFormatException nfe) {
            logger.debug("No proxy port defined", nfe);
            proxyPort = -1;
        }

        if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) {
            jp.setProperty("httpclient.proxy-host", proxyHost);
            jp.setProperty("httpclient.proxy-port", String.valueOf(proxyPort));
            jp.setProperty("httpclient.proxy-autodetect", "false");
            jp.setProperty("s3service.https-only", "true");
        }

        S3Service s3Service = new RestS3Service(awsCredentials, null, null, jp);

        S3Bucket[] myBuckets = s3Service.listAllBuckets();
        logger.info("Buckets in S3 account: " + myBuckets.length);

        S3Bucket uploadBucket = s3Service.getOrCreateBucket(awsBucketName.trim());

        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
        acl.setOwner(uploadBucket.getOwner());

        S3Object fileObject = new S3Object(uploadBucket, file);
        fileObject.setKey(fileName);
        fileObject.setAcl(acl);

        s3Service.putObject(uploadBucket, fileObject);

        final String url = this.awsUrl + uploadBucket.getName() + "/" + fileObject.getKey();

        logger.info("View public object contents here: " + url);

        details = new FileUploadDetails();
        details.setFileName(file.getName());
        details.setFileSize(file.length());
        details.setUrl(url);

    } catch (S3ServiceException s3se) {
        throw new FileUploadException("Error performing S3 upload: " + s3se.getMessage());
    } catch (NoSuchAlgorithmException nsae) {
        throw new FileUploadException("Error encrypting file for upload: " + nsae.getMessage());
    } catch (IOException ioe) {
        throw new FileUploadException("Error accessing file to upload: " + ioe.getMessage());
    }

    return details;
}

From source file:au.com.borner.salesforce.client.rest.domain.LoginResponse.java

public void verify(String consumerSecret) {
    SecretKey hmacKey = null;//from   w w  w. j  ava 2s . c o  m
    try {
        byte[] key = consumerSecret.getBytes();
        hmacKey = new SecretKeySpec(key, ALGORITHM);
        Mac mac = Mac.getInstance(ALGORITHM);
        mac.init(hmacKey);
        byte[] digest = mac.doFinal((getIdUrl() + getIssuedAt()).getBytes());
        byte[] decode_sig = new Base64(true).decode(getSignature());
        if (!Arrays.equals(digest, decode_sig)) {
            throw new SecurityException("Signature could not be verified!");
        }
    } catch (NoSuchAlgorithmException e) {
        throw new SecurityException(String.format(
                "Algorithm not found while trying to verifying signature: algorithm=%s; message=%s", ALGORITHM,
                e.getMessage()), e);
    } catch (InvalidKeyException e) {
        throw new SecurityException(
                String.format("Invalid key encountered while trying to verify signature: key=%s; message=%s",
                        hmacKey, e.getMessage()),
                e);
    }
}

From source file:com.invariantproperties.sandbox.springentitylistener.service.EncryptorBean.java

/**
 * Decrypt string//from w w w .ja  v  a2  s .c o m
 */
public String decryptString(String ciphertext, String salt) {
    String plaintext = null;

    if (ciphertext != null) {
        try {
            // Encryptor encryptor = JavaEncryptor.getInstance();
            // CipherText ct =
            // CipherText.fromPortableSerializedBytes(Base64.decode(ciphertext));
            // plaintext = encryptor.decrypt(key, ct).toString();
            IvParameterSpec iv = new IvParameterSpec(Base64.decode(salt));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);

            plaintext = new String(cipher.doFinal(Base64.decode(ciphertext)));
        } catch (NoSuchAlgorithmException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (InvalidKeyException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (BadPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (Throwable e) {
            e.printStackTrace(System.out);
        }
    }

    return plaintext;
}

From source file:be.fedict.eid.idp.protocol.saml2.artifact.ArtifactServiceServerHandler.java

private void handleOutboundDocument(SOAPPart soapPart, SOAPMessageContext soapMessageContext) {

    LOG.debug("handle outbound");

    // find optional IdP Identity for signing
    ServletContext servletContext = (ServletContext) soapMessageContext.get(MessageContext.SERVLET_CONTEXT);
    IdentityProviderConfiguration configuration = AbstractSAML2ProtocolService
            .getIdPConfiguration(servletContext);
    IdPIdentity idpIdentity = configuration.findIdentity();

    if (null != idpIdentity) {

        try {//from w  w  w .  j  av  a 2  s  .c  om
            LOG.debug("IdP Identity found, singing...");

            // find assertion and sing
            if (null != Saml2Util.find(soapPart, XPATH_RESPONSE_ASSERTION)) {
                sign(soapPart, XPATH_RESPONSE_ASSERTION, XPATH_RESPONSE_ASSERTION_ISSUER, idpIdentity);
            }

            // find Response and sign
            if (null != Saml2Util.find(soapPart, XPATH_RESPONSE)) {
                sign(soapPart, XPATH_RESPONSE, XPATH_RESPONSE_STATUS, idpIdentity);

            }

            // find ArtifactResponse and sign
            sign(soapPart, XPATH_ARTIFACT_RESPONSE, XPATH_STATUS, idpIdentity);

        } catch (NoSuchAlgorithmException e) {
            throw createSOAPFaultException("Signing failed: " + "NoSuchAlgorithmException: " + e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            throw createSOAPFaultException(
                    "Signing failed: " + "InvalidAlgorithmParameterException: " + e.getMessage());
        } catch (MarshalException e) {
            throw createSOAPFaultException("Signing failed: " + "MarshalException: " + e.getMessage());
        } catch (XMLSignatureException e) {
            throw createSOAPFaultException("Signing failed: " + "XMLSignatureException: " + e.getMessage());
        }

    }
}

From source file:com.invariantproperties.sandbox.springentitylistener.service.EncryptorBean.java

/**
 * Encrypt string. We should use a container class to contain the ciphertext
 * and key but an array is good enough for testing.
 *///from  w w  w  . j  a v a 2s.c  o  m
public String[] encryptString(String plaintext) {
    String ciphertext = null;
    String salt = null;

    if (plaintext != null) {
        try {
            // Encryptor encryptor = JavaEncryptor.getInstance();
            // CipherText ct = encryptor.encrypt(key, new
            // PlainText(plaintext));
            // ciphertext =
            // Base64.encodeBytes(ct.asPortableSerializedByteArray());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            ciphertext = Base64.encodeBytes(cipher.doFinal(plaintext.getBytes()));
            salt = Base64.encodeBytes(cipher.getIV());
        } catch (NoSuchAlgorithmException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (InvalidKeyException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (BadPaddingException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            // handle exception. Perhaps set value to null?
            System.out.println("decryption exception: " + e.getMessage());
        } catch (Throwable e) {
            e.printStackTrace(System.out);
        }
    }

    return new String[] { ciphertext, salt };
}

From source file:com.hichinaschool.flashcards.libanki.Utils.java

/**
 * SHA1 checksum./*  ww  w  .j a  v a  2  s . co m*/
 * Equivalent to python sha1.hexdigest()
 *
 * @param data the string to generate hash from
 * @return A string of length 40 containing the hexadecimal representation of the MD5 checksum of data.
 */
public static String checksum(String data) {
    String result = "";
    if (data != null) {
        MessageDigest md = null;
        byte[] digest = null;
        try {
            md = MessageDigest.getInstance("SHA1");
            digest = md.digest(data.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            Log.e(AnkiDroidApp.TAG, "Utils.checksum: No such algorithm. " + e.getMessage());
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            Log.e(AnkiDroidApp.TAG, "Utils.checksum: " + e.getMessage());
            e.printStackTrace();
        }
        BigInteger biginteger = new BigInteger(1, digest);
        result = biginteger.toString(16);

        // pad with zeros to length of 40 This method used to pad
        // to the length of 32. As it turns out, sha1 has a digest
        // size of 160 bits, leading to a hex digest size of 40,
        // not 32.
        if (result.length() < 40) {
            String zeroes = "0000000000000000000000000000000000000000";
            result = zeroes.substring(0, zeroes.length() - result.length()) + result;
        }
    }
    return result;
}

From source file:org.mitre.svmp.activities.AppRTCRefreshAppsActivity.java

private byte[] getIconHash(byte[] icon) {
    byte[] value = null;
    if (icon != null) {
        try {//w  w w.j  a  va  2s .  co  m
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            value = md.digest(icon);
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "getIconHash failed: " + e.getMessage());
        }
    }
    return value;
}

From source file:org.dasein.cloud.qingcloud.util.requester.QingCloudRequestBuilder.java

private String doMac(byte[] accessKeySecret, String stringToSign) throws InternalException {
    String signature;//from  w w w .ja v  a  2s  . c  o m
    try {
        Mac mac = Mac.getInstance(SIGNATURE_ALGORITHM);
        mac.init(new SecretKeySpec(accessKeySecret, SIGNATURE_ALGORITHM));
        byte[] signedData = mac.doFinal(stringToSign.getBytes(ENCODING));
        signature = new String(Base64.encodeBase64(signedData));
    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
        logger.error("AliyunRequestBuilderStrategy.sign() failed due to algorithm not supported: "
                + noSuchAlgorithmException.getMessage());
        throw new InternalException(noSuchAlgorithmException);
    } catch (InvalidKeyException invalidKeyException) {
        logger.error("AliyunRequestBuilderStrategy.sign() failed due to key invalid: "
                + invalidKeyException.getMessage());
        throw new InternalException(invalidKeyException);
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        logger.error("AliyunMethod.sign() failed due to encoding not supported: "
                + unsupportedEncodingException.getMessage());
        throw new InternalException(unsupportedEncodingException);
    }
    return signature;
}