List of usage examples for java.security MessageDigest reset
public void reset()
From source file:com.cprassoc.solr.auth.security.Sha256AuthenticationProvider.java
public static String sha256(String password, String saltKey) { MessageDigest digest; try {/*from w w w . j a va2s. c om*/ digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { log.error(e.getMessage(), e); return null;//should not happen } if (saltKey != null) { digest.reset(); digest.update(Base64.decodeBase64(saltKey)); } byte[] btPass = digest.digest(password.getBytes(StandardCharsets.UTF_8)); digest.reset(); btPass = digest.digest(btPass); return Base64.encodeBase64String(btPass); }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides one-way (irreversible) encryption of a provided string. * * @param plainText - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param instance - The security instance to utilize * @param iterations - The number of times the value should be re-encrypted * @param encoding - The text encoding//from w w w .j ava 2s . com * @return The encrypted string * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String encryptText(final String plainText, final String salt, final String instance, final int iterations, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String plainText, final String salt, final String algorithm, final String instance, final int iterations, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", plainText); DEBUGGER.debug("Value: {}", salt); DEBUGGER.debug("Value: {}", instance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", encoding); } String response = null; try { MessageDigest md = MessageDigest.getInstance(instance); md.reset(); md.update(salt.getBytes(encoding)); byte[] input = md.digest(plainText.getBytes(encoding)); for (int x = 0; x < iterations; x++) { md.reset(); input = md.digest(input); } response = Base64.getEncoder().encodeToString(input); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } return response; }
From source file:com.demandware.vulnapp.util.Helpers.java
/** * SHA-1 with optional salt /*ww w . j a va 2s . com*/ * * @param input String to hash * @param salt bytes to use as salt for sha * @return hex encoded hash */ public static String sha(String input, byte[] salt) { String ret = ""; try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); if (salt != null) { digest.update(salt); } ret = Hex.encodeHexString(digest.digest(input.getBytes("UTF-8"))); } catch (Throwable t) { //yeah this is bad... but there's no way to get either of the exceptions throw new RuntimeException(t); } return ret; }
From source file:Manifest.java
/** * This convenience method is used by both create() and verify(). It reads * the contents of a named file and computes a message digest for it, using * the specified MessageDigest object./* w w w . j ava2s. c om*/ */ public static byte[] getFileDigest(String filename, MessageDigest md) throws IOException { // Make sure there is nothing left behind in the MessageDigest md.reset(); // Create a stream to read from the file and compute the digest DigestInputStream in = new DigestInputStream(new FileInputStream(filename), md); // Read to the end of the file, discarding everything we read. // The DigestInputStream automatically passes all the bytes read to // the update() method of the MessageDigest while (in.read(buffer) != -1) /* do nothing */; // Finally, compute and return the digest value. return md.digest(); }
From source file:com.googlecode.osde.internal.utils.OpenSocialUtil.java
public static ApplicationInformation createApplicationInformation(String url) throws CoreException, ParserException, IOException { try {// w w w . j ava2s.c om Parser<Module> parser = ParserFactory.gadgetSpecParser(); Module module = parser.parse(new URL(url).openStream()); MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] hash = digest.digest(url.getBytes("UTF-8")); String appId = Gadgets.toHexString(hash); String consumerKey = "osde:" + url; digest.reset(); hash = digest.digest(consumerKey.getBytes("UTF-8")); String consumerSecret = Gadgets.toHexString(hash); ApplicationInformation info = new ApplicationInformation(); info.setAppId(appId); info.setModule(module); info.setPath(url); info.setConsumerKey(consumerKey); info.setConsumerSecret(consumerSecret); return info; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } catch (MalformedURLException e) { throw new IllegalStateException(e); } }
From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java
/** * Gets the checksum from the given byte array with an instance of. * * @param bytes/*from w w w.j av a 2s . c om*/ * the byte 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. * @throws NoSuchAlgorithmException * Is thrown if the algorithm is not supported or does not exists. * {@link java.security.MessageDigest} object. */ public static String getChecksum(final byte[] bytes, final String algorithm) throws NoSuchAlgorithmException { final MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.reset(); messageDigest.update(bytes); final byte digest[] = messageDigest.digest(); final StringBuilder hexView = new StringBuilder(); for (final byte element : digest) { final String intAsHex = Integer.toHexString(0xFF & element); if (intAsHex.length() == 1) { hexView.append('0'); } hexView.append(intAsHex); } return hexView.toString(); }
From source file:com.googlecode.osde.internal.utils.OpenSocialUtil.java
public static ApplicationInformation createApplicationInformation(IFile file) throws CoreException, ParserException { try {/*from w ww .j a v a 2 s . co m*/ Parser<Module> parser = ParserFactory.gadgetSpecParser(); Module module = parser.parse(file.getContents()); String path = file.getFullPath().toPortableString(); MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] hash = digest.digest(path.getBytes("UTF-8")); String appId = Gadgets.toHexString(hash); String consumerKey = "osde:" + path; digest.reset(); hash = digest.digest(consumerKey.getBytes("UTF-8")); String consumerSecret = Gadgets.toHexString(hash); ApplicationInformation info = new ApplicationInformation(); info.setAppId(appId); info.setModule(module); info.setPath(file.getFullPath().toPortableString()); info.setConsumerKey(consumerKey); info.setConsumerSecret(consumerSecret); return info; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
From source file:eu.cassandra.sim.utilities.Utils.java
public static String hashcode(String message) { String hash = null;//from w ww. j a v a 2s . c o m try { MessageDigest cript = MessageDigest.getInstance("SHA-1"); cript.reset(); cript.update(message.getBytes("utf8")); hash = new BigInteger(1, cript.digest()).toString(16); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return hash; }
From source file:com.lightbox.android.bitmap.BitmapUtils.java
private static String getFileMD5(File file) { try {/* w ww . ja va 2 s . c o m*/ MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.reset(); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); byte[] buffer = new byte[8192]; int count; while ((count = fileInputStream.read(buffer)) != -1) { md5.update(buffer, 0, count); } return md5ToString(md5.digest()).toString(); } finally { IOUtils.closeQuietly(fileInputStream); } } catch (NoSuchAlgorithmException e) { Log.w(TAG, e); } catch (FileNotFoundException e) { Log.w(TAG, e); } catch (IOException e) { Log.w(TAG, e); } return ""; }
From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
protected static byte[][] deriveKeyAndIV(byte[] salt, byte[] master_key) throws NoSuchAlgorithmException, IOException { byte[] key = new byte[KEY_LEN]; byte[] IV = new byte[IV_LEN]; MessageDigest md5 = MessageDigest.getInstance("MD5"); ByteArrayOutputStream data = new ByteArrayOutputStream(); ByteArrayOutputStream toHash = new ByteArrayOutputStream(); byte[] d = null; while (data.size() < IV_LEN + KEY_LEN) { md5.reset(); toHash.reset();// ww w . ja va 2 s .c o m if (d != null) toHash.write(d); toHash.write(master_key); toHash.write(salt); d = md5.digest(toHash.toByteArray()); data.write(d); } byte[] output = data.toByteArray(); System.arraycopy(output, 0, key, 0, KEY_LEN); System.arraycopy(output, KEY_LEN, IV, 0, IV_LEN); return new byte[][] { key, IV }; }