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:org.exoplatform.document.util.DigestUtils.java

/**
 * Calculates a SHA1 hash from a provided String. If text is null, a RuntimeException will be thrown.
  * //from w  w w.ja va  2s  .co  m
 * @param text - String to calculate a SHA1 hash from.
 * @return A SHA1 hash from the provided String.
 */
public static String toHexFromText(final String text) {
    String hashOfText = null;
    // If the provided String is null, throw an Exception.
    if (text == null) {
        throw new RuntimeException("There is no String to calculate a SHA1 hash from.");
    }

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        digest.update(text.getBytes(CharsetUtils.UTF_8));
        hashOfText = sha1Hex(digest.digest());
    } catch (NoSuchAlgorithmException nsae) {
        throw new RuntimeException("Could not find a SHA1 instance: " + nsae.getMessage());
    }
    return hashOfText;
}

From source file:de.sqlcoach.util.DBUtil.java

/**
 * returns a md5 fingerprinted string.//from   ww  w. j a  v  a 2  s .c o m
 * 
 * @param s
 *            String to encrypt
 * 
 * @return encrypted String
 */
public static String encrypt(String s) {
    byte[] bytearr = s.getBytes();
    String encrypted = new String();
    try {
        bytearr = MessageDigest.getInstance("md5").digest(bytearr);
        StringBuffer sb = new StringBuffer(bytearr.length * 2);
        int i, upper, bottom; // halfbytes
        for (i = 0; i < bytearr.length; i++) {
            upper = (bytearr[i] & 0xf0) >>> 4;
            bottom = bytearr[i] & 0x0f;
            sb.append(Integer.toString(upper, 16) + Integer.toString(bottom, 16));
        }
        encrypted = sb.toString();
    } catch (NoSuchAlgorithmException nsa) {
        log.error("NoSuchAlgorithmException:" + nsa.getMessage());
    }
    return encrypted;
}

From source file:org.wso2.carbon.user.cassandra.Util.java

public static String preparePassword(String password, String saltValue) throws UserStoreException {
    try {//from  w  w  w .java2s .c o m
        String digestInput = password;
        if (saltValue != null) {
            digestInput = password + saltValue;
        }
        String digsestFunction = Util.getRealmConfig().getUserStoreProperties()
                .get(JDBCRealmConstants.DIGEST_FUNCTION);
        if (digsestFunction != null) {

            if (digsestFunction.equals(UserCoreConstants.RealmConfig.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return password;
            }

            MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
            byte[] byteValue = dgst.digest(digestInput.getBytes());
            password = Base64.encode(byteValue);
        }
        return password;
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.apimgt.rest.api.util.utils.ETagGenerator.java

/**
 * This generates the ETag value using the given algorithm
 *
 * @param updatedTimeInMillis the updated/created time of the resource in UNIX time
 * @return String//from   w  ww  .j  a va  2s.c o m
 */
private static String getETag(long updatedTimeInMillis) {
    String eTagValue = null;
    try {
        eTagValue = getHash(updatedTimeInMillis);
    } catch (NoSuchAlgorithmException e) {
        log.error("Failed to generate E-Tag due to " + e.getMessage(), e);
    }
    return eTagValue;
}

From source file:Main.java

public static String getMd5Hash(File file) {
    try {/*  ww  w.  jav a2s  . co  m*/
        // CTS (6/15/2010) : stream file through digest instead of handing it the byte[]
        MessageDigest md = MessageDigest.getInstance("MD5");
        int chunkSize = 256;

        byte[] chunk = new byte[chunkSize];

        // Get the size of the file
        long lLength = file.length();

        if (lLength > Integer.MAX_VALUE) {
            Log.e(t, "File " + file.getName() + "is too large");
            return null;
        }

        int length = (int) lLength;

        InputStream is = null;
        is = new FileInputStream(file);

        int l = 0;
        for (l = 0; l + chunkSize < length; l += chunkSize) {
            is.read(chunk, 0, chunkSize);
            md.update(chunk, 0, chunkSize);
        }

        int remaining = length - l;
        if (remaining > 0) {
            is.read(chunk, 0, remaining);
            md.update(chunk, 0, remaining);
        }
        byte[] messageDigest = md.digest();

        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);
        while (md5.length() < 32)
            md5 = "0" + md5;
        is.close();
        return md5;

    } catch (NoSuchAlgorithmException e) {
        Log.e("MD5", e.getMessage());
        return null;

    } catch (FileNotFoundException e) {
        Log.e("No Cache File", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Problem reading from file", e.getMessage());
        return null;
    }

}

From source file:org.gss_project.gss.web.client.TestClient.java

public static String sign(String httpMethod, String timestamp, String path, String token) {
    String input = httpMethod + timestamp + path;
    String signed = null;/*from   w ww  .j  a  v  a 2 s  .  co m*/

    try {
        System.err.println("Token:" + token);
        // Get an HMAC-SHA1 key from the authentication token.
        System.err.println("Input: " + input);
        SecretKeySpec signingKey = new SecretKeySpec(Base64.decodeBase64(token.getBytes()), "HmacSHA1");

        // Get an HMAC-SHA1 Mac instance and initialize with the signing key.
        Mac hmac = Mac.getInstance("HmacSHA1");
        hmac.init(signingKey);

        // Compute the HMAC on the input data bytes.
        byte[] rawMac = hmac.doFinal(input.getBytes());

        // Do base 64 encoding.
        signed = new String(Base64.encodeBase64(rawMac), "US-ASCII");

    } catch (InvalidKeyException ikex) {
        System.err.println("Fatal key exception: " + ikex.getMessage());
        ikex.printStackTrace();
    } catch (UnsupportedEncodingException ueex) {
        System.err.println("Fatal encoding exception: " + ueex.getMessage());
    } catch (NoSuchAlgorithmException nsaex) {
        System.err.println("Fatal algorithm exception: " + nsaex.getMessage());
        nsaex.printStackTrace();
    }

    if (signed == null)
        System.exit(-1);
    System.err.println("Signed: " + signed);
    return signed;
}

From source file:com.eugene.fithealthmaingit.FatSecretSearchAndGet.FatSecretGetMethod.java

private static String sign(String method, String uri, String[] params) {
    String[] p = { method, Uri.encode(uri), Uri.encode(paramify(params)) };
    String s = join(p, "&");
    SecretKey sk = new SecretKeySpec(Globals.APP_SECRET.getBytes(), Globals.HMAC_SHA1_ALGORITHM);
    try {//from  www. ja v  a  2  s  .  c o  m
        Mac m = Mac.getInstance(Globals.HMAC_SHA1_ALGORITHM);
        m.init(sk);
        return Uri.encode(new String(Base64.encode(m.doFinal(s.getBytes()), Base64.DEFAULT)).trim());
    } catch (java.security.NoSuchAlgorithmException e) {
        Log.w("FatSecret_TEST FAIL", e.getMessage());
        return null;
    } catch (java.security.InvalidKeyException e) {
        Log.w("FatSecret_TEST FAIL", e.getMessage());
        return null;
    }
}

From source file:Main.java

/**
 * Compute the sha-256 digest of data./*  ww  w.  ja  va 2 s .c o m*/
 * @param data The input byte buffer. This does not change the position.
 * @return The digest.
 */
public static byte[] digestSha256(ByteBuffer data) {
    MessageDigest sha256;
    try {
        sha256 = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException exception) {
        // Don't expect this to happen.
        throw new Error("MessageDigest: SHA-256 is not supported: " + exception.getMessage());
    }
    int savePosition = data.position();
    sha256.update(data);
    data.position(savePosition);
    return sha256.digest();
}

From source file:com.alibaba.openapi.client.util.SignatureUtil.java

public static byte[] hmacSha1(byte[] data, SecretKeySpec signingKey) {
    Mac mac = null;//from   ww  w .  jav  a  2  s  .c  om
    try {
        mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    return mac.doFinal(data);
}

From source file:org.sakaiproject.nakamura.util.Signature.java

public static String calculateRFC2104HMACWithEncoding(String data, String key, boolean urlSafe)
        throws java.security.SignatureException {
    if (data == null) {
        throw new IllegalArgumentException("String data == null");
    }//  w w w . j a  v a  2s. c  om
    if (key == null) {
        throw new IllegalArgumentException("String key == null");
    }
    try {
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes("UTF-8");
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));

        // Convert raw bytes to encoding
        byte[] base64Bytes = Base64.encodeBase64(rawHmac, false, urlSafe);
        String result = new String(base64Bytes, "UTF-8");

        return result;

    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    }
}