List of usage examples for java.security NoSuchAlgorithmException getMessage
public String getMessage()
From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java
/** * Gets the checksum quietly from the given byte array with an instance of. * * @param bytes/*from w w w . j a va 2 s .com*/ * the Byte object array. * @param algorithm * the algorithm to get the checksum. This could be for instance "MD4", "MD5", * "SHA-1", "SHA-256", "SHA-384" or "SHA-512". * @return The checksum from the file as a String object. */ public static String getChecksumQuietly(final Byte[] bytes, final String algorithm) { try { return getChecksum(bytes, algorithm); } catch (final NoSuchAlgorithmException e) { LOGGER.error("getChecksumQuietly failed...\n" + e.getMessage(), e); } return algorithm; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding/*w ww . j av a 2 s . c om*/ * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String decryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String decPass = null; try { String decoded = new String(Base64.getDecoder().decode(value)); String iv = decoded.split(":")[0]; String property = decoded.split(":")[1]; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(Base64.getDecoder().decode(iv))); decPass = new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), encoding); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidAlgorithmParameterException iapx) { throw new SecurityException(iapx.getMessage(), iapx); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } return decPass; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Base64 decodes a given string// w w w .j av a2 s .c om * * @param variance - The allowed differences in OTP values * @param algorithm - The algorithm to encrypt the data with * @param instance - The security instance to utilize * @param secret - The OTP secret * @param code - The OTP code * @return <code>true</code> if successful, <code>false</code> otherwise * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final boolean validateOtpValue(final int variance, final String algorithm, final String instance, final String secret, final int code) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#validateOtpValue(final int variance, final String algorithm, final String instance, final String secret, final int code) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", variance); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", instance); DEBUGGER.debug("Value: {}", secret); DEBUGGER.debug("Value: {}", code); } long truncatedHash = 0; byte[] data = new byte[8]; long timeIndex = System.currentTimeMillis() / 1000 / 30; final Base32 codec = new Base32(); final byte[] decoded = codec.decode(secret); SecretKeySpec signKey = new SecretKeySpec(decoded, algorithm); if (DEBUG) { DEBUGGER.debug("long: {}", timeIndex); } try { for (int i = 8; i-- > 0; timeIndex >>>= 8) { data[i] = (byte) timeIndex; } Mac mac = Mac.getInstance(instance); mac.init(signKey); byte[] hash = mac.doFinal(data); int offset = hash[20 - 1] & 0xF; for (int i = 0; i < 4; i++) { truncatedHash <<= 8; truncatedHash |= (hash[offset + i] & 0xFF); } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; if (DEBUG) { DEBUGGER.debug("truncatedHash: {}", truncatedHash); } return (truncatedHash == code); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } }
From source file:com.nloko.android.Utils.java
public static String getMd5Hash(byte[] input) { if (input == null) { throw new IllegalArgumentException("input"); }/*from w w w .ja v a 2 s . c o m*/ try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input); BigInteger number = new BigInteger(1, messageDigest); //String md5 = number.toString(16); StringBuffer md5 = new StringBuffer(); md5.append(number.toString(16)); while (md5.length() < 32) { //md5 = "0" + md5; md5.insert(0, "0"); } return md5.toString(); } catch (NoSuchAlgorithmException e) { Log.e("MD5", e.getMessage()); return null; } }
From source file:edu.umd.cs.eclipse.courseProjectManager.EclipseLaunchEventLog.java
private static String computeChecksumOfCVSControlledFiles(IProject project) { try {// www.j a v a 2s. com // Find all the files under CVS control Collection<IFile> fileList = TurninProjectAction.findFilesForSubmission(project); return md5sumAsHexString(fileList); } catch (NoSuchAlgorithmException e) { Debug.print("NoSuchAlgorithm " + e.getMessage(), e); } catch (IOException e) { Debug.print("IOException " + e.getMessage(), e); } catch (TeamException e) { Debug.print("TeamException " + e.getMessage(), e); } catch (CoreException e) { Debug.print("CoreException " + e.getMessage(), e); } return "cant_make_md5sum"; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding//w w w . j a v a 2 s .co m * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String encPass = null; try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.ENCRYPT_MODE, sks); AlgorithmParameters parameters = pbeCipher.getParameters(); IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class); byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding)); byte[] iv = ivParameterSpec.getIV(); String combined = Base64.getEncoder().encodeToString(iv) + ":" + Base64.getEncoder().encodeToString(cryptoText); encPass = Base64.getEncoder().encodeToString(combined.getBytes()); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } catch (InvalidParameterSpecException ipsx) { throw new SecurityException(ipsx.getMessage(), ipsx); } return encPass; }
From source file:password.pwm.util.secure.SecureEngine.java
public static byte[] computeHashToBytes(final InputStream is, final PwmHashAlgorithm algorithm) throws PwmUnrecoverableException { final InputStream bis = is instanceof BufferedInputStream ? is : new BufferedInputStream(is); final MessageDigest messageDigest; try {//from w w w . ja va 2 s . c o m messageDigest = MessageDigest.getInstance(algorithm.getAlgName()); } catch (NoSuchAlgorithmException e) { final String errorMsg = "missing hash algorithm: " + e.getMessage(); final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_CRYPT_ERROR, errorMsg); throw new PwmUnrecoverableException(errorInformation); } try { final byte[] buffer = new byte[HASH_BUFFER_SIZE]; int length; while (true) { length = bis.read(buffer, 0, buffer.length); if (length == -1) { break; } messageDigest.update(buffer, 0, length); } bis.close(); return messageDigest.digest(); } catch (IOException e) { final String errorMsg = "unexpected error during hash operation: " + e.getMessage(); final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_CRYPT_ERROR, errorMsg); throw new PwmUnrecoverableException(errorInformation); } }
From source file:org.sakaiproject.nakamura.util.PathUtils.java
/** * @param target/*w w w .ja va 2 s. c o m*/ * the target being formed into a structured path. * @param b * @return the structured path. */ private static String getStructuredHash(String target, int levels, boolean absPath) { try { // take the first element as the key for the target so that subtrees end up in the // same place. String[] elements = StringUtils.split(target, '/', 1); String pathInfo = removeFirstElement(target); target = elements[0]; target = String.valueOf(target); MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] userHash = md.digest(target.getBytes("UTF-8")); char[] chars = new char[(absPath ? 1 : 0) + levels * 3 + target.length() + pathInfo.length()]; int j = 0; if (absPath) { chars[j++] = '/'; } for (int i = 0; i < levels; i++) { byte current = userHash[i]; int hi = (current & 0xF0) >> 4; int lo = current & 0x0F; chars[j++] = (char) (hi < 10 ? ('0' + hi) : ('a' + hi - 10)); chars[j++] = (char) (lo < 10 ? ('0' + lo) : ('a' + lo - 10)); chars[j++] = '/'; } for (int i = 0; i < target.length(); i++) { char c = target.charAt(i); if (!Character.isLetterOrDigit(c)) { c = '_'; } chars[j++] = c; } for (int i = 0; i < pathInfo.length(); i++) { chars[j++] = pathInfo.charAt(i); } return new String(chars); } catch (NoSuchAlgorithmException e) { logger.error(e.getMessage(), e); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } return null; }
From source file:at.diamonddogs.util.Utils.java
/** * Computes a MD5 hash from an input string * * @param input the input string// ww w . j a va 2 s. com * @return the MD5 hash or <code>null</code> if an error occured */ public static String getMD5Hash(String input) { MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); return new String(Hex.encodeHex(md.digest(input.getBytes()))); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.getMessage(), e); } return null; }
From source file:org.iransoil.collect.android.utilities.FileUtils.java
@SuppressLint("LongLogTag") public static String getMd5Hash(File file) { try {//from www . java 2 s . 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; } }