List of usage examples for java.security NoSuchAlgorithmException getMessage
public String getMessage()
From source file:org.apache.ws.security.message.token.UsernameToken.java
/** * This static method generates a derived key as defined in WSS Username * Token Profile./* w ww . jav a 2 s . c o m*/ * * @param password The password to include in the key generation * @param salt The Salt value * @param iteration The Iteration value. If zero (0) is given the method uses the * default value * @return Returns the derived key a byte array * @throws WSSecurityException */ public static byte[] generateDerivedKey(byte[] password, byte[] salt, int iteration) throws WSSecurityException { if (iteration == 0) { iteration = DEFAULT_ITERATION; } byte[] pwSalt = new byte[salt.length + password.length]; System.arraycopy(password, 0, pwSalt, 0, password.length); System.arraycopy(salt, 0, pwSalt, password.length, salt.length); MessageDigest sha = null; try { sha = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { if (DO_DEBUG) { LOG.debug(e.getMessage(), e); } throw new WSSecurityException(WSSecurityException.FAILURE, "noSHA1availabe", null, e); } sha.reset(); // // Make the first hash round with start value // byte[] K = sha.digest(pwSalt); // // Perform the 1st up to iteration-1 hash rounds // for (int i = 1; i < iteration; i++) { K = sha.digest(K); } return K; }
From source file:org.nuclos.common2.StringUtils.java
public static String encrypt(String x) { MessageDigest digest = null;//from w w w .ja v a2s .c o m try { digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(x.getBytes()); return new String(digest.digest()); } catch (NoSuchAlgorithmException e) { throw new CommonFatalException(e.getMessage(), e); } }
From source file:org.sakaiproject.nakamura.api.lite.StorageClientUtils.java
public static String insecureHash(byte[] b) { try {/*from w w w . j ava 2s. c o m*/ MessageDigest md; try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e1) { try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e2) { LOGGER.error( "You have no Message Digest Algorightms intalled in this JVM, secure Hashes are not availalbe, encoding bytes :" + e2.getMessage()); return encode(StringUtils.leftPad((new String(b, "UTF-8")), 10, '_').getBytes(UTF8)); } } byte[] bytes = md.digest(b); return encode(bytes); } catch (UnsupportedEncodingException e3) { LOGGER.error("no UTF-8 Envoding, get a real JVM, nothing will work here. NPE to come"); return null; } }
From source file:org.sakaiproject.nakamura.api.lite.StorageClientUtils.java
/** * @param password/*from w w w. ja v a 2 s . c o m*/ * @return as secure hash of the supplied password, unsuitable for keys as * its too long. */ public static String secureHash(String password) { try { MessageDigest md; try { md = MessageDigest.getInstance(SECURE_HASH_DIGEST); } catch (NoSuchAlgorithmException e) { try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e1) { try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e2) { LOGGER.error( "You have no Message Digest Algorightms intalled in this JVM, secure Hashes are not availalbe, encoding bytes :" + e2.getMessage()); return encode(StringUtils.leftPad(password, 10, '_').getBytes(UTF8)); } } } byte[] bytes = md.digest(password.getBytes(UTF8)); return encode(bytes); } catch (UnsupportedEncodingException e3) { LOGGER.error("no UTF-8 Envoding, get a real JVM, nothing will work here. NPE to come"); return null; } }
From source file:org.opendatakit.briefcase.util.FileSystemUtils.java
public static final String getMd5Hash(File file) { try {/* w ww . jav a 2 s .c om*/ // 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) { logger.error("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) { logger.error("MD5 calculation failed: " + e.getMessage()); return null; } catch (FileNotFoundException e) { logger.error("No File: " + e.getMessage()); return null; } catch (IOException e) { logger.error("Problem reading from file: " + e.getMessage()); return null; } }
From source file:com.sat.vcse.automation.utils.http.HttpClient.java
/** * Get list of supported Cipher Suites/* w ww. jav a 2 s. c om*/ * @return Supported Cipher Suites */ public static List<String> getSupportedCipherSuites() { final String METHOD_NAME = "getSupportedCipherSuites(): "; try { final SSLContext sslContext = SSLContext.getDefault(); final SSLSocketFactory sslsf = sslContext.getSocketFactory(); return Arrays.asList(sslsf.getSupportedCipherSuites()); } catch (NoSuchAlgorithmException exp) { LogHandler.error(CLASS_NAME + METHOD_NAME + "Exception: " + exp.getMessage()); throw new CoreRuntimeException(exp, CLASS_NAME + METHOD_NAME + exp.getMessage()); } }
From source file:com.websqrd.catbot.setting.CatbotSettings.java
private static String encryptPasswd(String passwd) { String[] hexArray = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" }; StringBuffer sb = new StringBuffer(); try {//from www. j a v a2 s. c o m MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] encData = md5.digest(passwd.getBytes()); for (int i = 0; i < encData.length; i++) { sb.append(hexArray[0xff & encData[i]]); } } catch (NoSuchAlgorithmException e) { logger.error(e.getMessage(), e); } return sb.toString(); }
From source file:cn.ctyun.amazonaws.services.s3.internal.crypto.EncryptionUtils.java
/** * Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption. *///from ww w . java 2s.co m public static SecretKey generateOneTimeUseSymmetricKey() { KeyGenerator generator; try { generator = KeyGenerator.getInstance(JceEncryptionConstants.SYMMETRIC_KEY_ALGORITHM); generator.init(JceEncryptionConstants.SYMMETRIC_KEY_LENGTH, new SecureRandom()); return generator.generateKey(); } catch (NoSuchAlgorithmException e) { throw new AmazonClientException("Unable to generate envelope symmetric key:" + e.getMessage(), e); } }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
private static String encodeJwt(JSONObject jsonHeader, JSONObject jsonClaim, SignatureAlgorithm algorithm, String sharedKey, RSAPrivateKey rsaPrivateKey, ECDSAPrivateKey ecdsaPrivateKey) { String signature = ""; String header = jsonHeader.toString(); String claim = jsonClaim.toString(); try {/*from w w w. j a v a 2 s . c o m*/ header = base64urlencode(header.getBytes(Util.UTF8_STRING_ENCODING)); claim = base64urlencode(claim.getBytes(Util.UTF8_STRING_ENCODING)); String signingInput = header + "." + claim; byte[] sign = null; switch (algorithm) { case NONE: break; case HS256: sign = getSignatureHS256(signingInput.getBytes(Util.UTF8_STRING_ENCODING), sharedKey.getBytes(Util.UTF8_STRING_ENCODING)); break; case HS384: sign = getSignatureHS384(signingInput.getBytes(Util.UTF8_STRING_ENCODING), sharedKey.getBytes(Util.UTF8_STRING_ENCODING)); break; case HS512: sign = getSignatureHS512(signingInput.getBytes(Util.UTF8_STRING_ENCODING), sharedKey.getBytes(Util.UTF8_STRING_ENCODING)); break; case RS256: sign = getSignatureRS256(signingInput.getBytes(Util.UTF8_STRING_ENCODING), rsaPrivateKey); break; case RS384: sign = getSignatureRS384(signingInput.getBytes(Util.UTF8_STRING_ENCODING), rsaPrivateKey); break; case RS512: sign = getSignatureRS512(signingInput.getBytes(Util.UTF8_STRING_ENCODING), rsaPrivateKey); break; case ES256: sign = getSignatureES256(signingInput.getBytes(Util.UTF8_STRING_ENCODING), ecdsaPrivateKey); break; case ES384: sign = getSignatureES384(signingInput.getBytes(Util.UTF8_STRING_ENCODING), ecdsaPrivateKey); break; case ES512: sign = getSignatureES512(signingInput.getBytes(Util.UTF8_STRING_ENCODING), ecdsaPrivateKey); break; default: throw new UnsupportedOperationException("Algorithm not supported"); } if (sign != null) { signature = base64urlencode(sign); } } catch (NoSuchAlgorithmException e) { log.error(e.getMessage(), e); } catch (InvalidKeyException e) { log.error(e.getMessage(), e); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } catch (InvalidKeySpecException e) { log.error(e.getMessage(), e); } catch (NoSuchProviderException e) { log.error(e.getMessage(), e); } catch (SignatureException e) { log.error(e.getMessage(), e); } final StringBuilder builder = new StringBuilder(); builder.append(header).append('.').append(claim).append('.').append(signature); return builder.toString(); }
From source file:pt.lunacloud.services.storage.internal.crypto.EncryptionUtils.java
/** * Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption. *///from ww w. ja va 2 s.c o m public static SecretKey generateOneTimeUseSymmetricKey() { KeyGenerator generator; try { generator = KeyGenerator.getInstance(JceEncryptionConstants.SYMMETRIC_KEY_ALGORITHM); generator.init(JceEncryptionConstants.SYMMETRIC_KEY_LENGTH, new SecureRandom()); return generator.generateKey(); } catch (NoSuchAlgorithmException e) { throw new LunacloudClientException("Unable to generate envelope symmetric key:" + e.getMessage(), e); } }