List of usage examples for java.security MessageDigest update
public void update(byte[] input, int offset, int len)
From source file:com.easyway.java.rpc.secure.security.Encodes.java
public static String operationAlgorithm(String secureData) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(secureData.getBytes("utf-8"), 0, secureData.length()); byte[] sha1hash = md.digest(); return convertToHex(sha1hash); }
From source file:fm.audiobox.core.utils.MD5Checksum.java
/** * Calculate checksum of a File using MD5 algorithm *//*from ww w . j a v a 2 s.c o m*/ public static String checkSum(InputStream is) throws NoSuchAlgorithmException { String checksum = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); // Using MessageDigest update() method to provide input byte[] buffer = new byte[8192]; int numOfBytesRead; while ((numOfBytesRead = is.read(buffer)) > 0) { md.update(buffer, 0, numOfBytesRead); } byte[] hash = md.digest(); checksum = new String(Hex.encodeHex(hash)); } catch (IOException ex) { logger.error(ex.getMessage()); } return checksum; }
From source file:core.SHA256Hash.java
public static String CalculateCheckSum(String FileName, String url) throws Exception { MessageDigest md = MessageDigest.getInstance(getHashAlgType(url)); FileInputStream fis = new FileInputStream(FileName); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }// w w w . j a v a 2 s . co m ; byte[] mdbytes = md.digest(); // Store byte array as string and return it String value = new String(mdbytes); System.out.println("Encoded " + getHashAlgType(url) + " hash: " + value); return value; }
From source file:net.sf.keystore_explorer.crypto.digest.DigestUtil.java
/** * Get a digest of the input stream./*w w w . j av a 2s.c om*/ * * @param istream * Input stream to digest * @param digestType * The message digest algorithm * @return The message digest * @throws CryptoException * If message digester could not be created */ public static byte[] getMessageDigest(InputStream istream, DigestType digestType) throws CryptoException { MessageDigest messageDigester = getMessageDigester(digestType); try { byte[] buffer = new byte[2048]; int read = 0; while ((read = istream.read(buffer)) != -1) { messageDigester.update(buffer, 0, read); } byte[] messageDigest = messageDigester.digest(); return messageDigest; } catch (IOException ex) { throw new CryptoException(res.getString("NoCreateDigest.exception.message"), ex); } finally { IOUtils.closeQuietly(istream); } }
From source file:core.SHA256Hash.java
public static String CalculateCheckSumBase64(String FileName, String url) throws Exception { MessageDigest md = MessageDigest.getInstance(SHA256); FileInputStream fileHandle = new FileInputStream(FileName); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fileHandle.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }/* ww w.ja v a 2 s.c om*/ ; byte[] mdbytes = md.digest(); byte[] bytes = null; if (SHA256 == getHashAlgType(url)) { // Do base64 encoding bytes = Base64.encodeBase64URLSafe(mdbytes); } else { if (SHA256_16 == getHashAlgType(url)) bytes = Base64.encodeBase64(new byte[] { mdbytes[0], mdbytes[1] }, false, true, 4); } // Store byte array as string and return it String value = new String(bytes); System.out.println("Base64 encoded " + getHashAlgType(url) + " hash: " + value); return value; }
From source file:edu.smc.mediacommons.modules.Md5Module.java
public static String getSHA1(final File file) { String sha1 = null;//from w w w.j ava 2 s .com try { final MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); InputStream is = new BufferedInputStream(new FileInputStream(file)); final byte[] buffer = new byte[1024]; for (int read = 0; (read = is.read(buffer)) != -1;) { messageDigest.update(buffer, 0, read); } // Convert the byte to hex format Formatter formatter = new Formatter(); for (final byte b : messageDigest.digest()) { formatter.format("%02x", b); } sha1 = formatter.toString(); formatter.close(); is.close(); } catch (IOException | NoSuchAlgorithmException e) { e.printStackTrace(); } finally { } return sha1; }
From source file:org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler.java
/** * /*from w ww .ja va 2 s. co m*/ * Generate a visitor id for this hit. If there is a visitor id in the * messageContext, use that. Otherwise use a random number. * */ private static String getVisitorId(String account, String userAgent, MessageContext msgCtx) throws NoSuchAlgorithmException, UnsupportedEncodingException { if (msgCtx.getProperty(COOKIE_NAME) != null) { return (String) msgCtx.getProperty(COOKIE_NAME); } String message; AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(msgCtx); if (authContext != null) { message = authContext.getApiKey(); } else { message = ANONYMOUS_USER_ID; } MessageDigest m = MessageDigest.getInstance("MD5"); m.update(message.getBytes("UTF-8"), 0, message.length()); byte[] sum = m.digest(); BigInteger messageAsNumber = new BigInteger(1, sum); String md5String = messageAsNumber.toString(16); /* Pad to make sure id is 32 characters long. */ while (md5String.length() < 32) { md5String = "0" + md5String; } return "0x" + md5String.substring(0, 16); }
From source file:com.exam.mserver.common.persistence.util.Digests.java
private static byte[] digest(InputStream input, String algorithm) throws IOException { try {/*from ww w . j a v a 2 s .co m*/ MessageDigest messageDigest = MessageDigest.getInstance(algorithm); int bufferLength = 8 * 1024; byte[] buffer = new byte[bufferLength]; int read = input.read(buffer, 0, bufferLength); while (read > -1) { messageDigest.update(buffer, 0, read); read = input.read(buffer, 0, bufferLength); } return messageDigest.digest(); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:com.idea.quickstart.common.security.Digests.java
private static byte[] digest(InputStream input, String algorithm) throws IOException { try {//from w w w . j a va 2 s. c o m MessageDigest messageDigest = MessageDigest.getInstance(algorithm); int bufferLength = 8 * 1024; byte[] buffer = new byte[bufferLength]; int read = input.read(buffer, 0, bufferLength); while (read > -1) { messageDigest.update(buffer, 0, read); read = input.read(buffer, 0, bufferLength); } return messageDigest.digest(); } catch (GeneralSecurityException e) { } return null; }
From source file:com.pfw.popsicle.common.Digests.java
private static byte[] digest(InputStream input, String algorithm) throws IOException { try {/*w ww . j a va 2s . c o m*/ MessageDigest messageDigest = MessageDigest.getInstance(algorithm); int bufferLength = 8 * 1024; byte[] buffer = new byte[bufferLength]; int read = input.read(buffer, 0, bufferLength); while (read > -1) { messageDigest.update(buffer, 0, read); read = input.read(buffer, 0, bufferLength); } return messageDigest.digest(); } catch (GeneralSecurityException e) { throw new IOException(e.getMessage()); } }