List of usage examples for java.security MessageDigest update
public final void update(ByteBuffer input)
From source file:com.slimsmart.common.util.code.EncodeUtil.java
/** * MD5?/*from w ww . java2 s .c om*/ */ public static String md5Encode(String input) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(input.getBytes()); byte[] byteDigest = md5.digest(); return hexEncode(byteDigest); } catch (NoSuchAlgorithmException e) { logger.error("e:{}", e); return null; } }
From source file:Main.java
public static byte[] calculateMd5(byte[] binaryData) { MessageDigest messageDigest = null; try {/* www . j a va 2s . c o m*/ messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found."); } messageDigest.update(binaryData); return messageDigest.digest(); }
From source file:com.all.dht.util.Digest.java
public static byte[] getSha1(byte[] bytes) { byte[] sha1 = null; try {/*from ww w .ja va 2 s .com*/ MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(bytes); sha1 = md.digest(); md.reset(); } catch (Exception e) { LOG.fatal("Could not generate SHA1.", e); } return sha1; }
From source file:com.linkedin.databus.client.registration.RegistrationIdGenerator.java
/** * Generate a hash out of the String id// w ww . j av a 2s . com * * @param id * @return */ private static String generateByteHash(String id) { try { final MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(id.getBytes(Charset.forName("UTF8"))); final byte[] resultsByte = messageDigest.digest(); String hash = new String(Hex.encodeHex(resultsByte)); final int length = 8; if (hash.length() > length) hash = hash.substring(0, length); return hash; } catch (NoSuchAlgorithmException nse) { LOG.error("Unexpected error : Got NoSuchAlgorithm exception for MD5"); return ""; } }
From source file:org.shredzone.cilla.web.comment.CommentThreadServiceImpl.java
/** * Computes a mail hash that can be used at Gravatar. * * @param mail// w w w. j a v a 2s .c om * mail address * @return md5 hash */ private static String computeMailHash(String mail) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.reset(); md5.update(mail.trim().toLowerCase().getBytes("UTF-8")); StringBuilder digest = new StringBuilder(); for (byte b : md5.digest()) { digest.append(String.format("%02x", b & 0xFF)); } return digest.toString(); } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) { // we expect no exception, since MD5 and UTF-8 are standards throw new InternalError(ex.getMessage()); } }
From source file:com.goliathonline.android.kegbot.util.BitmapUtils.java
/** * Only call this method from the main (UI) thread. The {@link OnFetchCompleteListener} callback * be invoked on the UI thread, but image fetching will be done in an {@link AsyncTask}. * * @param cookie An arbitrary object that will be passed to the callback. *//*from w w w . j av a 2 s . c o m*/ public static void fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback) { new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... params) { final String url = params[0]; if (TextUtils.isEmpty(url)) { return null; } // First compute the cache key and cache file path for this URL File cacheFile = null; try { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); mDigest.update(url.getBytes()); final String cacheKey = bytesToHexString(mDigest.digest()); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android" + File.separator + "data" + File.separator + context.getPackageName() + File.separator + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp"); } } catch (NoSuchAlgorithmException e) { // Oh well, SHA-1 not available (weird), don't cache bitmaps. } if (cacheFile != null && cacheFile.exists()) { Bitmap cachedBitmap = BitmapFactory.decodeFile(cacheFile.toString(), decodeOptions); if (cachedBitmap != null) { return cachedBitmap; } } try { // TODO: check for HTTP caching headers final HttpClient httpClient = SyncService.getHttpClient(context.getApplicationContext()); final HttpResponse resp = httpClient.execute(new HttpGet(url)); final HttpEntity entity = resp.getEntity(); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK || entity == null) { return null; } final byte[] respBytes = EntityUtils.toByteArray(entity); // Write response bytes to cache. if (cacheFile != null) { try { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); fos.write(respBytes); fos.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } catch (IOException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } } // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, decodeOptions); } catch (Exception e) { Log.w(TAG, "Problem while loading image: " + e.toString(), e); } return null; } @Override protected void onPostExecute(Bitmap result) { callback.onFetchComplete(cookie, result); } }.execute(url); }
From source file:com.ncr.itss.core.utils.StringUtils.java
/** * SHA1//from w w w .j a v a2 s .c om * * @param str * @return ? */ public static String generateSHA1(String str) { if (str == null) { return null; } try { MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); messageDigest.update(str.getBytes()); return byte2String(messageDigest.digest()); } catch (Exception e) { return null; } }
From source file:com._37coins.CryptoUtils.java
/** * <p>// www. jav a2 s. c o m * Helper method that creates an RFC 2307-compliant salted, hashed password * with the SHA1 MessageDigest algorithm. After the password is digested, * the first 20 bytes of the digest will be the actual password hash; the * remaining bytes will be the salt. Thus, supplying a password * <code>testing123</code> and a random salt <code>foo</code> produces the * hash: * </p> * <blockquote><code>{SSHA}yfT8SRT/WoOuNuA6KbJeF10OznZmb28=</code> * </blockquote> * <p> * In layman's terms, the formula is * <code>digest( secret + salt ) + salt</code>. The resulting digest is * Base64-encoded. * </p> * * @param password * the password to be digested * @param salt * the random salt * @return the Base64-encoded password hash, prepended by * <code>{SSHA}</code>. * @throws NoSuchAlgorithmException * If your JVM is totally b0rked and does not have SHA1. */ public static String getSaltedPassword(byte[] password, byte[] salt) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA"); digest.update(password); byte[] hash = digest.digest(salt); // Create an array with the hash plus the salt byte[] all = new byte[hash.length + salt.length]; for (int i = 0; i < hash.length; i++) { all[i] = hash[i]; } for (int i = 0; i < salt.length; i++) { all[hash.length + i] = salt[i]; } byte[] base64 = Base64.encodeBase64(all); return SSHA + new String(base64); }
From source file:Main.java
public static String md5(String str) { MessageDigest algorithm = null; try {/*from w ww .j a va2s . c om*/ algorithm = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } if (algorithm != null) { algorithm.reset(); algorithm.update(str.getBytes()); byte[] bytes = algorithm.digest(); StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { hexString.append(Integer.toHexString(0xFF & b)); } return hexString.toString(); } return ""; }
From source file:tw.idv.gasolin.pycontw2012.util.BitmapUtils.java
/** * Only call this method from the main (UI) thread. The * {@link OnFetchCompleteListener} callback be invoked on the UI thread, but * image fetching will be done in an {@link AsyncTask}. * /*from ww w . j a v a 2s. co m*/ * @param cookie * An arbitrary object that will be passed to the callback. */ public static void fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback) { new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... params) { final String url = params[0]; if (TextUtils.isEmpty(url)) { return null; } // First compute the cache key and cache file path for this URL File cacheFile = null; try { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); mDigest.update(url.getBytes()); final String cacheKey = bytesToHexString(mDigest.digest()); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android" + File.separator + "data" + File.separator + context.getPackageName() + File.separator + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp"); } } catch (NoSuchAlgorithmException e) { // Oh well, SHA-1 not available (weird), don't cache // bitmaps. } if (cacheFile != null && cacheFile.exists()) { Bitmap cachedBitmap = BitmapFactory.decodeFile(cacheFile.toString(), decodeOptions); if (cachedBitmap != null) { return cachedBitmap; } } try { // TODO: check for HTTP caching headers final HttpClient httpClient = SyncService.getHttpClient(context.getApplicationContext()); final HttpResponse resp = httpClient.execute(new HttpGet(url)); final HttpEntity entity = resp.getEntity(); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK || entity == null) { return null; } final byte[] respBytes = EntityUtils.toByteArray(entity); // Write response bytes to cache. if (cacheFile != null) { try { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); fos.write(respBytes); fos.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } catch (IOException e) { Log.w(TAG, "Error writing to bitmap cache: " + cacheFile.toString(), e); } } // Decode the bytes and return the bitmap. return BitmapFactory.decodeByteArray(respBytes, 0, respBytes.length, decodeOptions); } catch (Exception e) { Log.w(TAG, "Problem while loading image: " + e.toString(), e); } return null; } @Override protected void onPostExecute(Bitmap result) { callback.onFetchComplete(cookie, result); } }.execute(url); }