List of usage examples for java.security MessageDigest reset
public void reset()
From source file:org.dcache.xrootd.plugins.authn.gsi.CertUtil.java
/** * Computes the hash from the principal, using the passed-in digest * (usually MD5). After applying the digest on the DER-encoded * principal, the first 4 bytes of the computed hash are taken and * interpreted as a hexadecimal integer in Little Endian. This * corresponds to the openssl hash mechanism. * * Keep a cache of principals, as this method will often be called * with the same principal (to avoid costly rehashing). * * @param md the digest instance/*from ww w . j a v a 2 s . c o m*/ * @param principal the principal (subject or issuer) * @return the 8-digit hexadecimal hash */ public static String computeHash(MessageDigest md, X500Principal principal) { String principalHash; if (_hashCache.containsKey(principal)) { principalHash = _hashCache.get(principal); } else { md.reset(); md.update(principal.getEncoded()); byte[] md5hash = md.digest(); // take the first 4 bytes in little Endian int shortHash = (0xff & md5hash[3]) << 24 | (0xff & md5hash[2]) << 16 | (0xff & md5hash[1]) << 8 | (0xff & md5hash[0]); // convert to hex principalHash = Integer.toHexString(shortHash); _hashCache.put(principal, principalHash); } return principalHash; }
From source file:io.manasobi.utils.DigestUtils.java
/** * ? ? .<br>/*from w w w. j a v a2 s. co m*/ * - "MD5", "SHA-1", "SHA-256"<br> * ? ? ? ?? ? - MD5: 32?, SHA-1: 40?, SHA-256: 64? * * @param password ? * @param secure * @return ? ? ? ? */ public static String encodePassword(String password, Secure secure) { byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { md = MessageDigest.getInstance(secure.getAlgorithm()); } catch (Exception e) { throw new DigestUtilsException(e.getMessage()); } md.reset(); md.update(unencodedPassword); byte[] encodedPassword = md.digest(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encodedPassword.length; i++) { if (((int) encodedPassword[i] & HEX_FF) < HEX_10) { sb.append("0"); } sb.append(Long.toString((int) encodedPassword[i] & HEX_FF, HEX)); } return sb.toString(); }
From source file:com.cloud.test.utils.UtilsForTest.java
public static String createMD5String(String password) { MessageDigest md5; try {//from w w w . j ava 2s .c o m md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CloudRuntimeException("Error", e); } md5.reset(); BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes())); // make sure our MD5 hash value is 32 digits long... StringBuffer sb = new StringBuffer(); String pwStr = pwInt.toString(16); int padding = 32 - pwStr.length(); for (int i = 0; i < padding; i++) { sb.append('0'); } sb.append(pwStr); return sb.toString(); }
From source file:com.xidu.framework.common.util.TokenUtils.java
/** * Encode a string using algorithm and return the resulting encrypted * password. If exception, the plain credentials string is returned * /*from w ww.j ava 2 s .co m*/ * @param token * Token or other credentials to use in authenticating this * username * @param algorithm * Algorithm used to do the digest * @return encypted password based on the algorithm. */ private static String encryptToken(String token, String algorithm) { byte[] unencodedToken = token.getBytes(); MessageDigest md; try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); } catch (Exception e) { logger.error("Exception: " + e); return token; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, eg. stream) md.update(unencodedToken); // now calculate the hash byte[] encodedToken = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedToken.length; i++) { if (((int) encodedToken[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString((int) encodedToken[i] & 0xff, 16)); } return buf.toString(); }
From source file:io.cslinmiso.line.utils.Utility.java
public static String cryptWithMD5(String pass) { try {/*from w w w. j a v a2 s.c o m*/ MessageDigest md = MessageDigest.getInstance("MD5"); byte[] passBytes = pass.getBytes(); md.reset(); byte[] digested = md.digest(passBytes); StringBuffer sb = new StringBuffer(); for (int i = 0; i < digested.length; i++) { sb.append(Integer.toHexString(0xff & digested[i])); } // System.out.println(sb.toString()); return sb.toString(); } catch (NoSuchAlgorithmException ex) { // ? } return null; }
From source file:StringUtils.java
/** * Encode a string using algorithm specified in web.xml and return the * resulting encrypted password. If exception, the plain credentials * string is returned//from w w w . j a v a 2 s. co m * * @param password Password or other credentials to use in authenticating * this username * @param algorithm Algorithm used to do the digest * * @return encypted password based on the algorithm. */ public static String encodePassword(String password, String algorithm) { byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); } catch (Exception e) { System.out.println(e); return password; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, eg. stream) md.update(unencodedPassword); // now calculate the hash byte[] encodedPassword = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedPassword.length; i++) { if ((encodedPassword[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); } return buf.toString(); }
From source file:MainFrame.HttpCommunicator.java
public static String md5Custom(String st) { MessageDigest messageDigest = null; byte[] digest = new byte[0]; try {//from w w w. j a v a 2 s. c o m messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(st.getBytes()); digest = messageDigest.digest(); } catch (NoSuchAlgorithmException e) { System.err.printf("MD-5 error"); } BigInteger bigInt = new BigInteger(1, digest); String md5Hex = bigInt.toString(16); while (md5Hex.length() < 32) { md5Hex = "0" + md5Hex; } return md5Hex; }
From source file:octavio.server.db.managers.UserManager.java
/** * Retourne la chaine de caractres fournie en paramtre hashe avec * l'algorithme configur.// w ww . j ava 2 s. c o m * * @param password Mot de passe hasher * * @return Mot de passe hash ou chane vide si algorithme indisponible */ public static String hashPassword(String password) { try { MessageDigest digest = MessageDigest.getInstance(Configuration.get("hash_algorithm", "SHA-1")); digest.reset(); digest.update(password.getBytes()); return new String(Hex.encodeHex(digest.digest())); } catch (NoSuchAlgorithmException exception) { Logger.printError(exception); return password; } }
From source file:com.amazonaws.services.kinesis.log4j.helpers.AmazonKinesisPutRecordsHelper.java
public static int calculateShardBucket(String partitionKey, int totalNumOfShards) { MessageDigest m = null; int shardBucket = 1; try {/*from ww w . j a v a2 s . com*/ m = MessageDigest.getInstance("MD5"); m.reset(); m.update(partitionKey.getBytes()); byte[] digest = m.digest(); BigInteger bigInt = new BigInteger(1, digest); shardBucket = bigInt.mod(BigInteger.valueOf(totalNumOfShards)).intValue() + 1; } catch (NoSuchAlgorithmException e) { //ignore } return shardBucket; }
From source file:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java
/** * Gets the checksum from the given byte array with an instance of. * * @param bytes/* w ww.j av a 2 s .co m*/ * 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(byte[] bytes, String algorithm) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.reset(); messageDigest.update(bytes); byte digest[] = messageDigest.digest(); StringBuilder hexView = new StringBuilder(); for (byte element : digest) { String intAsHex = Integer.toHexString(0xFF & element); if (intAsHex.length() == 1) { hexView.append('0'); } hexView.append(intAsHex); } return hexView.toString(); }