List of usage examples for java.security MessageDigest update
public final void update(ByteBuffer input)
From source file:com.continusec.client.ObjectHash.java
private static final byte[] hashDouble(double f) throws ContinusecException { MessageDigest d = DigestUtils.getSha256Digest(); d.update((byte) 'f'); if (f == 0.0) { // special case 0 d.update((byte) '+'); d.update((byte) '0'); d.update((byte) ':'); } else {/* ww w . jav a2 s . c o m*/ if (f < 0) { d.update((byte) '-'); f = -f; } else { d.update((byte) '+'); } int e = 0; while (f > 1) { f /= 2.0; e++; } while (f < 0.5) { f *= 2.0; e--; } d.update(Integer.toString(e).getBytes()); d.update((byte) ':'); if ((f > 1) || (f <= 0.5)) { throw new InvalidObjectException(); } for (int cnt = 0; (f != 0) && (cnt < 1000); cnt++) { if (f >= 1) { d.update((byte) '1'); f -= 1.0; } else { d.update((byte) '0'); } if (f >= 1) { throw new InvalidObjectException(); } f *= 2.0; } if (f != 0) { // we went too long throw new InvalidObjectException(); } } return d.digest(); }
From source file:me.j360.dubbo.modules.util.text.HashUtil.java
/** * , ?md5sha1./*from www . j ava 2 s .c om*/ */ private static byte[] digest(@NotNull byte[] input, MessageDigest digest, byte[] salt, int iterations) { // ? if (salt != null) { digest.update(salt); } // byte[] result = digest.digest(input); // >1 for (int i = 1; i < iterations; i++) { digest.reset(); result = digest.digest(result); } return result; }
From source file:com.linuxbox.enkive.docstore.mongo.FileDocStoreServiceTest.java
/** * Returns a MessageDigest that is primed with the data for the type of * document.// w w w. ja v a 2s . c om * * @param doc * @return * @throws Exception */ private static MessageDigest getPrimedMessageDigest(Document doc) throws Exception { MessageDigest messageDigest = MessageDigest.getInstance(AbstractDocStoreService.HASH_ALGORITHM); messageDigest.update(AbstractDocStoreService.getFileTypeEncodingDigestPrime(doc)); return messageDigest; }
From source file:mangotiger.net.EncodedDigester.java
/** * Utility Method for obtaining a BASE64 encoded MessageDigest via specified algorithm. * * Added a check for null here.// ww w . ja v a 2 s . c o m * @param toDigest - array of strings to digest * @param anAlgorithm - digesting algorithm to use to perform the digest * @return BASE64 encoded String of a MessageDigest digested by the specified algorithm * @throws java.security.NoSuchAlgorithmException * - when the algorithm is unknown * @throws UnsupportedEncodingException - when the string-to-byte encoding is not supported * @see MessageDigest */ public static String getEncodedDigest(String[] toDigest, String anAlgorithm, String stringEncoding) throws java.security.NoSuchAlgorithmException, java.io.UnsupportedEncodingException { String digest = null; if (toDigest != null && toDigest.length > 0) { try { MessageDigest messageDigest = MessageDigest.getInstance(anAlgorithm); for (int i = 0; i < toDigest.length && toDigest[i] != null; i++) { messageDigest.update(toDigest[i].getBytes(stringEncoding)); } digest = encode(messageDigest.digest()); } catch (java.security.NoSuchAlgorithmException e) { logger.error("SecurityManager.getLicenseKey(): can't get a digest instance", e); throw e; } catch (java.io.UnsupportedEncodingException e) { logger.error("SecurityManager.getLicenseKey(): can't get a digest instance", e); throw e; } } return digest; }
From source file:io.alicorn.server.http.LoginEndpoint.java
public static String hash(byte[] bytes) { try {//from w w w .j a va 2 s .c om //Create the message digest. MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(bytes); //Hash the bytes. byte[] hashed = digest.digest(); //Translate the bytes into a hexadecimal string. StringBuilder hexString = new StringBuilder(); for (byte aHashed : hashed) { String hex = Integer.toHexString(0xff & aHashed); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:PublisherUtil.java
/** * Utility for getting the MD5 hash from the provided key for sending the publishPassword. * // w w w . j a va 2 s . co m * @param passWord * The password to get an MD5 hash of * @return zero-padded MD5 hash of the password */ public static final String getPasswordKey(final String passWord) { try { MessageDigest md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$ md.reset(); // Reset the algorithm md.update(passWord.getBytes()); // Update the algorithm with the e-mail // Update the algorithm with a known "key" for keyed MD5 // It basically adds the new key to the end and computes byte[] digest = md.digest("P3ntah0Publ1shPa55w0rd".getBytes()); //$NON-NLS-1$ StringBuffer buf = new StringBuffer(); String s; for (byte element : digest) { s = Integer.toHexString(0xFF & element); buf.append((s.length() == 1) ? "0" : "").append(s); //$NON-NLS-1$ //$NON-NLS-2$ } return buf.toString(); // Return MD5 string } catch (NoSuchAlgorithmException ex) { PublisherUtil.logger.error(null, ex); return null; } }
From source file:net.dbjorge.jthumbor.ThumborUtils.java
/** * MD5 hashes the given input string and returns the hex digest in String form. * * Input may not be null or empty.//w w w.ja v a 2 s . c o m */ public static String md5String(String input) { String result = ""; MessageDigest algorithm; try { algorithm = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } algorithm.reset(); algorithm.update(input.getBytes()); byte[] md5 = algorithm.digest(); String tmp = ""; for (int i = 0; i < md5.length; i++) { tmp = (Integer.toHexString(0xFF & md5[i])); if (tmp.length() == 1) { result += "0" + tmp; } else { result += tmp; } } return result; }
From source file:codes.writeonce.maven.plugins.soy.CompileMojo.java
private static void updateDigest(Path root, MessageDigest sourceDigest, Path soyFilePath) throws IOException { sourceDigest.update(soyFilePath.toString().getBytes(TEXT_DIGEST_CHARSET)); final Path soyFile = root.resolve(soyFilePath); sourceDigest.update(Longs.toByteArray(Files.size(soyFile))); sourceDigest.update(Longs.toByteArray(Files.getLastModifiedTime(soyFile).toMillis())); }
From source file:com.zen.androidhtmleditor.util.TextUtil.java
public static String MD5(String str, String encoding) { MessageDigest messageDigest = null; try {//w w w .ja v a2 s . c om messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } messageDigest.reset(); try { messageDigest.update(str.getBytes(encoding)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.toString(); }
From source file:com.eucalyptus.auth.euare.EuareServerCertificateUtil.java
public static String getEncryptedKey(final String certArn, final String certPem) throws AuthException { final ServerCertificate targetCert = lookupServerCertificate(certArn); // generate symmetric key final MessageDigest digest = Digest.SHA256.get(); final byte[] salt = new byte[32]; Crypto.getSecureRandomSupplier().get().nextBytes(salt); digest.update(salt); final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES"); try {/* ww w .j a v a2s . c om*/ // encrypt the server pk using symm key Cipher cipher = Ciphers.AES_CBC.get(); final byte[] iv = new byte[16]; Crypto.getSecureRandomSupplier().get().nextBytes(iv); cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv), Crypto.getSecureRandomSupplier().get()); final byte[] cipherText = cipher.doFinal(Base64.encode(targetCert.getPrivateKey().getBytes())); final String encPrivKey = new String(Base64.encode(Arrays.concatenate(iv, cipherText))); // encrypt the symmetric key using the certPem X509Certificate x509Cert = PEMFiles.getCert(B64.standard.dec(certPem)); cipher = Ciphers.RSA_PKCS1.get(); cipher.init(Cipher.ENCRYPT_MODE, x509Cert.getPublicKey(), Crypto.getSecureRandomSupplier().get()); byte[] symmkey = cipher.doFinal(symmKey.getEncoded()); final String b64SymKey = new String(Base64.encode(symmkey)); return String.format("%s\n%s", b64SymKey, encPrivKey); } catch (final Exception ex) { throw Exceptions.toUndeclared(ex); } }