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.pinterest.secor.util.FileUtil.java

/** Generate MD5 hash of topic and partitions. And extract first 4 characters of the MD5 hash.
 * @param topic/* w  ww  .java  2s  .  co  m*/
 * @param partitions
 * @return
 */
public static String getMd5Hash(String topic, String[] partitions) {
    ArrayList<String> elements = new ArrayList<String>();
    elements.add(topic);
    for (String partition : partitions) {
        elements.add(partition);
    }
    String pathPrefix = StringUtils.join(elements, "/");
    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5Bytes = messageDigest.digest(pathPrefix.getBytes("UTF-8"));
        return getHexEncode(md5Bytes).substring(0, 4);
    } catch (NoSuchAlgorithmException e) {
        LOG.error(e.getMessage());
    } catch (UnsupportedEncodingException e) {
        LOG.error(e.getMessage());
    }
    return "";
}

From source file:com.l2jfree.loginserver.manager.GameServerManager.java

/**
 * Return singleton/*w ww. ja va2  s  . c  o  m*/
 * exit the program if we didn't succeed to load the instance
 * @return  GameServerManager
 */
public static GameServerManager getInstance() {
    if (__instance == null) {
        try {
            __instance = new GameServerManager();
        } catch (NoSuchAlgorithmException e) {
            _log.fatal("FATAL: Failed loading GameServerManager. Reason: " + e.getMessage(), e);
            System.exit(1);
        } catch (InvalidAlgorithmParameterException e) {
            _log.fatal("FATAL: Failed loading GameServerManager. Reason: " + e.getMessage(), e);
            System.exit(1);
        }
    }
    return __instance;
}

From source file:org.sonatype.nexus.test.utils.FileTestingUtils.java

/**
 * Creates a SHA1 hash from an InputStream.
 * //w  w  w  . j  av a2s.com
 * @param in Inputstream to be digested.
 * @returnn SHA1 hash based on the contents of the stream.
 * @throws IOException
 */
public static String createSHA1FromStream(InputStream in) throws IOException {

    byte[] bytes = new byte[BUFFER_SIZE];

    try {
        MessageDigest digest = MessageDigest.getInstance(SHA1);
        for (int n; (n = in.read(bytes)) >= 0;) {
            if (n > 0) {
                digest.update(bytes, 0, n);
            }
        }

        bytes = digest.digest();
        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            int n = bytes[i] & 0xFF;
            if (n < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(n));
        }

        return sb.toString();
    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
        throw new IllegalStateException(noSuchAlgorithmException.getMessage(), noSuchAlgorithmException);
    }
}

From source file:io.wcm.caravan.commons.httpclient.impl.helpers.CertificateLoader.java

/**
 * Creates default SSL context.//from   w w w .  j  a  v  a2  s . c om
 * @return SSL context
 */
public static SSLContext createDefaultSSlContext() throws SSLInitializationException {
    try {
        final SSLContext sslcontext = SSLContext.getInstance(SSL_CONTEXT_TYPE_DEFAULT);
        sslcontext.init(null, null, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    } catch (KeyManagementException ex) {
        throw new SSLInitializationException(ex.getMessage(), ex);
    }
}

From source file:com.liferay.portal.security.pwd.PwdEncryptor.java

public static String encodePassword(String algorithm, String clearTextPassword, byte[] saltBytes)
        throws PwdEncryptorException {

    try {/*from  w w w .j  a v  a2  s .  c o  m*/
        if (algorithm.equals(TYPE_BCRYPT)) {
            String salt = new String(saltBytes);

            return null; // BCrypt.hashpw(clearTextPassword, salt);
        } else if (algorithm.equals(TYPE_CRYPT) || algorithm.equals(TYPE_UFC_CRYPT)) {

            return null; // Crypt.crypt(
            //saltBytes, clearTextPassword.getBytes(Digester.ENCODING));
        } else if (algorithm.equals(TYPE_SSHA)) {
            byte[] clearTextPasswordBytes = clearTextPassword.getBytes("UTF8");//Digester.ENCODING);

            // Create a byte array of salt bytes appended to password bytes

            byte[] pwdPlusSalt = new byte[clearTextPasswordBytes.length + saltBytes.length];

            System.arraycopy(clearTextPasswordBytes, 0, pwdPlusSalt, 0, clearTextPasswordBytes.length);

            System.arraycopy(saltBytes, 0, pwdPlusSalt, clearTextPasswordBytes.length, saltBytes.length);

            // Digest byte array

            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");

            byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);

            // Appends salt bytes to the SHA-1 digest.

            byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length];

            System.arraycopy(pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length);

            System.arraycopy(saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length);

            // Base64 encode and format string

            return ByteArray.toBase64String(digestPlusSalt);
            //return Base64.encode(digestPlusSalt);
        } else {
            return null; // DigesterUtil.digest(algorithm, clearTextPassword);
        }
    } catch (NoSuchAlgorithmException nsae) {
        throw new PwdEncryptorException(nsae.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new PwdEncryptorException(uee.getMessage());
    }
}

From source file:pepperim.util.IMCrypt.java

/**
 * Generates a random string to be used as AES encryption key
 * @return random AES encryption key/*from www. j  a  v  a2  s  . com*/
 */
public static String AES_genKey() {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");

        kgen.init(128);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();

        return binToHex(raw);

    } catch (NoSuchAlgorithmException e) {
        Main.log(e.getMessage());
        return "";
    }
}

From source file:pepperim.util.IMCrypt.java

/**
 * @param text String to be hashed//  ww  w  . j  a  v a2  s  .  co  m
 * @return SHA512-hash
 */
public static String SHA512(String text) {
    try {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-512");
        byte[] hash = new byte[40];
        md.update(text.getBytes("UTF-8"), 0, text.length());
        hash = md.digest();
        return binToHex(hash);
    }

    catch (NoSuchAlgorithmException ex) {
        Main.log(ex.getMessage());
        return "";
    } catch (UnsupportedEncodingException ex) {
        Main.log(ex.getMessage());
        return "";
    }
}

From source file:org.artifactory.security.CryptoHelper.java

public static SecretKey generatePbeKey(String password) {
    try {/* w  w w.  jav a 2  s  . co  m*/
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SYM_ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(pbeKeySpec);
        return secretKey;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("No such algorithm: " + e.getMessage());
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("Unexpected exception: ", e);
    }
}

From source file:dk.clanie.io.IOUtil.java

/**
 * Calculates SHA1 for the contents of the supplied InputStream.
 * //  w  ww  .j av  a2s .c o  m
 * @param is InputStream
 * @return String with SHA1 calculated from the contents of <code>is</code>.
 * 
 * @throws RuntimeIOException
 */
public static String sha1(InputStream is) {
    byte[] buf = new byte[1024 * 1024 * 5];
    int bytesRead = 0;
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException nsae) {
        throw new RuntimeIOException(nsae.getMessage(), nsae);
    }
    try {
        while ((bytesRead = is.read(buf)) > 0) {
            md.update(buf, 0, bytesRead);
        }
    } catch (IOException ioe) {
        throw new RuntimeIOException(ioe.getMessage(), ioe);
    }
    byte[] digest = md.digest();
    String hexDigest = new String(Hex.encodeHex(digest));
    return hexDigest;
}

From source file:com.sixsq.slipstream.cookie.CryptoUtils.java

/**
 * Determine if the given signature matches the given data.
 *
 * @param signed//  w ww. ja  v a  2 s.c o m
 *            String representation of signature
 * @param data
 *            information to check
 *
 * @return true if the signature matches the given data, false otherwise
 */
public static boolean verify(String signed, String data) {

    boolean valid = false;

    try {

        Signature signature = Signature.getInstance(signatureAlgorithm);
        signature.initVerify(publicKey);

        signature.update(data.getBytes());

        byte[] signBytes = (new BigInteger(signed, radix)).toByteArray();
        valid = signature.verify(signBytes);

    } catch (NoSuchAlgorithmException e) {
        log.warning("Algorithm not recognized: " + signatureAlgorithm + " with details: " + e.getMessage());
    } catch (InvalidKeyException e) {
        log.warning(e.toString());
    } catch (SignatureException e) {
        log.warning(e.toString());
    }

    return valid;
}