List of usage examples for java.security MessageDigest update
public final void update(ByteBuffer input)
From source file:com.redhat.rhn.common.util.MD5Crypt.java
/** * MD5 and Hexify an array of bytes. Take the input array, MD5 encodes it * and then turns it into Hex./*w ww . j av a 2 s. co m*/ * @param secretBytes you want md5hexed * @return md5hexed String. */ public static String md5Hex(byte[] secretBytes) { String retval = null; // add secret MessageDigest md; try { md = MessageDigest.getInstance("MD5"); //byte[] secretBytes = inputString.getBytes("UTF-8"); md.update(secretBytes); // generate the digest byte[] digest = md.digest(); // hexify this puppy retval = new String(Hex.encodeHex(digest)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException( "NoSuchAlgorithm: MD5. Something" + " weird with your JVM, you should be able to do this.", e); } return retval; }
From source file:com.redhat.rhn.common.util.SHA256Crypt.java
/** * crypt/*from w w w .j a v a2s.c o m*/ * Encodes a key using a salt (s) in the same manner as the perl crypt() function * @param key - The key to encode * @param s - The salt * @return Returns a string in the form of "$1$salt$encodedkey" * @throws SHA256CryptException */ public static String crypt(String key, String s) { final byte[] keyBytes = key.getBytes(); final int keyLen = keyBytes.length; String salt = CryptHelper.getSalt(s, CryptHelper.getSHA256Prefix(), saltLength); final byte[] saltBytes = salt.getBytes(); final int saltLen = saltBytes.length; final int blocksize = 32; MessageDigest ctx = getSHA256MD(); ctx.update(keyBytes); // add the key/salt to the first digest ctx.update(saltBytes); MessageDigest altCtx = getSHA256MD(); altCtx.update(keyBytes); // add the key/salt/key to the second digest altCtx.update(saltBytes); altCtx.update(keyBytes); byte[] altResult = altCtx.digest(); int cnt = keyBytes.length; while (cnt > blocksize) { ctx.update(altResult, 0, blocksize); cnt -= blocksize; } ctx.update(altResult, 0, cnt); cnt = keyBytes.length; while (cnt > 0) { if ((cnt & 1) != 0) { ctx.update(altResult, 0, blocksize); } else { ctx.update(keyBytes); } cnt >>= 1; } altResult = ctx.digest(); altCtx = getSHA256MD(); for (int i = 1; i <= keyLen; i++) { altCtx.update(keyBytes); } byte[] tempResult = altCtx.digest(); final byte[] pBytes = new byte[keyLen]; int cp = 0; while (cp < keyLen - blocksize) { System.arraycopy(tempResult, 0, pBytes, cp, blocksize); cp += blocksize; } System.arraycopy(tempResult, 0, pBytes, cp, keyLen - cp); altCtx = getSHA256MD(); for (int i = 1; i <= 16 + (altResult[0] & 0xff); i++) { altCtx.update(saltBytes); } tempResult = altCtx.digest(); final byte[] sBytes = new byte[saltLen]; cp = 0; while (cp < saltLen - blocksize) { System.arraycopy(tempResult, 0, sBytes, cp, blocksize); cp += blocksize; } System.arraycopy(tempResult, 0, sBytes, cp, saltLen - cp); for (int i = 0; i <= 5000 - 1; i++) { ctx = getSHA256MD(); if ((i & 1) != 0) { ctx.update(pBytes, 0, keyLen); } else { ctx.update(altResult, 0, blocksize); } if (i % 3 != 0) { ctx.update(sBytes, 0, saltLen); } if (i % 7 != 0) { ctx.update(pBytes, 0, keyLen); } if ((i & 1) != 0) { ctx.update(altResult, 0, blocksize); } else { ctx.update(pBytes, 0, keyLen); } altResult = ctx.digest(); } return generateEncodedKey(altResult, salt); }
From source file:UUIDGenerator.java
protected static String getInitialUUID() { try {/* w ww. jav a2 s .co m*/ if (System.class.getMethod("nanoTime", new Class[0]) != null) { useNano = true; } } catch (NoSuchMethodException ignore) { } if (myRand == null) { myRand = new Random(); } long rand = myRand.nextLong(); String sid; try { sid = InetAddress.getLocalHost().toString(); } catch (UnknownHostException e) { sid = Thread.currentThread().getName(); } StringBuffer sb = new StringBuffer(); sb.append(sid); sb.append(":"); sb.append(Long.toString(rand)); MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { //System.out.println("Error: " + e); //todo heve to be properly handle } md5.update(sb.toString().getBytes()); byte[] array = md5.digest(); StringBuffer sb2 = new StringBuffer(); for (int j = 0; j < array.length; ++j) { int b = array[j] & 0xFF; sb2.append(Integer.toHexString(b)); } int begin = myRand.nextInt(); if (begin < 0) begin = begin * -1; begin = begin % 8; return sb2.toString().substring(begin, begin + 18).toUpperCase(); }
From source file:Main.java
public static String digest(InputStream in) throws Exception { MessageDigest messageDigest = null; XMLInputFactory inputFactory = XMLInputFactory.newFactory(); messageDigest = MessageDigest.getInstance("MD5"); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { messageDigest.update(event.asStartElement().getName().toString().getBytes()); } else if (event.isEndElement()) { messageDigest.update(event.asEndElement().getName().toString().getBytes()); }/* w w w . jav a2s .c o m*/ } StringBuffer result = new StringBuffer(); byte[] digest = messageDigest.digest(); for (byte b : digest) { result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); } return result.toString(); }
From source file:net.firejack.platform.core.utils.SecurityHelper.java
/** * return hash/* w ww . ja v a2 s.c o m*/ * * @param plainText value to hash * @return hashed value */ public static String hashMD5(String plainText) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } try { md.update(plainText.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage()); } byte raw[] = md.digest(); return (new BASE64Encoder()).encode(raw); }
From source file:net.firejack.platform.core.utils.SecurityHelper.java
/** * return hash/*from www .j a v a 2 s. co m*/ * * @param plainText value to hash * @return hashed value */ public static String hashSHA(String plainText) { MessageDigest md; try { md = MessageDigest.getInstance("SHA"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } try { md.update(plainText.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage()); } byte raw[] = md.digest(); return (new BASE64Encoder()).encode(raw); }