List of usage examples for java.security MessageDigest reset
public void reset()
From source file:com.photon.phresco.framework.impl.ConfigProcessor.java
private static SecretKey getAes128Key(String s) { try {//from ww w . j a va2s . com MessageDigest digest = MessageDigest.getInstance(SHA_ALGO); digest.reset(); digest.update(s.getBytes(CI_UTF8)); return new SecretKeySpec(digest.digest(), 0, 128 / 8, AES_ALGO); } catch (NoSuchAlgorithmException e) { throw new Error(e); } catch (UnsupportedEncodingException e) { throw new Error(e); } }
From source file:org.nuclos.common2.StringUtils.java
public static String encrypt(String x) { MessageDigest digest = null; try {//from w w w .j a va2s . c o m digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(x.getBytes()); return new String(digest.digest()); } catch (NoSuchAlgorithmException e) { throw new CommonFatalException(e.getMessage(), e); } }
From source file:com.liveneo.plat.utils.StringUtil.java
/** * Encodes a string using algorithm specified in web.xml and return the * resulting encrypted password. If exception, the plain credentials string * is returned/*from w ww . ja v a 2 s . c o 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) { if (password == null) { return null; } Log log = LogFactory.getLog(StringUtil.class); byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); } catch (Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); if (log.isErrorEnabled()) { log.error(sw.toString()); } return password; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, e.g. 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:org.inwiss.platform.common.util.StringUtil.java
/** * Encodes 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.ja va2 s . com*/ * * @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) { if (password == null) { return null; } Log log = LogFactory.getLog(StringUtil.class); byte[] unencodedPassword = password.getBytes(); MessageDigest md; try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); } catch (Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); if (log.isErrorEnabled()) { log.error(sw.toString()); } return password; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, e.g. 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:net.jolm.util.UserPasswordHelper.java
/** * Calculates hash of clear text password to be stored in the userPassword * field./* w w w .j a v a2 s.com*/ * * @param clearpass * The password in plaintext that should be hashed. * @param alg * The algorithm to caculate the hash. * @param salt * The salt that is to be used together with the schemes {SMD5} * and {SSHA}. Should be between 8 and 16 Bytes. salt should be * null for any other scheme. * @return The base64-encoded hashed pwd with the following format: - * {MD5}base64(MD5-hash) for MD5 hashes - {SHA}base64(SHA-hash) for * SHA hashes - {SMD5}base64(MD5-hash+salt bytes) for SMD5 hashes - * {SSHA}base64(SHA-hash+salt bytes) for SSHA hashes Or null if t is * not one of 2, 3, 4, 5. */ public static byte[] clearPassToUserPassword(String clearpass, HashAlg alg, byte[] salt) { if (alg == null) { throw new IllegalArgumentException("Invalid hash argorithm."); } try { MessageDigest digester = null; StringBuilder resultInText = new StringBuilder(); switch (alg) { case MD5: resultInText.append("{MD5}"); digester = MessageDigest.getInstance("MD5"); break; case SMD5: resultInText.append("{SMD5}"); digester = MessageDigest.getInstance("MD5"); break; case SHA: resultInText.append("{SHA}"); digester = MessageDigest.getInstance("SHA"); break; case SSHA: resultInText.append("{SSHA}"); digester = MessageDigest.getInstance("SHA"); break; default: break; } digester.reset(); digester.update(clearpass.getBytes(DEFAULT_ENCODING)); byte[] hash = null; if (salt != null && (alg == HashAlg.SMD5 || alg == HashAlg.SSHA)) { digester.update(salt); hash = ArrayUtils.addAll(digester.digest(), salt); } else { hash = digester.digest(); } resultInText.append(new String(Base64.encodeBase64(hash), DEFAULT_ENCODING)); return resultInText.toString().getBytes(DEFAULT_ENCODING); } catch (UnsupportedEncodingException uee) { log.warn("Error occurred while hashing password ", uee); return new byte[0]; } catch (java.security.NoSuchAlgorithmException nse) { log.warn("Error occurred while hashing password ", nse); return new byte[0]; } }
From source file:com.puyuntech.flowerToHome.plugin.unionpayPayment.SecureUtil.java
/** * md5.// w w w. j a v a2 s . c om * * @param datas * ? * @return */ public static byte[] md5(byte[] datas) { MessageDigest md = null; try { md = MessageDigest.getInstance(ALGORITHM_MD5); md.reset(); md.update(datas); return md.digest(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.puyuntech.flowerToHome.plugin.unionpayPayment.SecureUtil.java
/** * sha1./*ww w .ja v a2 s .com*/ * * @param datas * ? * @return */ public static byte[] sha1(byte[] data) { MessageDigest md = null; try { md = MessageDigest.getInstance(ALGORITHM_SHA1); md.reset(); md.update(data); return md.digest(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:cn.usually.common.pay.union.sdk.SecureUtil.java
/** * md5.//w w w. ja va2s . c o m * * @param datas * ? * @return */ public static byte[] md5(byte[] datas) { MessageDigest md = null; try { md = MessageDigest.getInstance(ALGORITHM_MD5); md.reset(); md.update(datas); return md.digest(); } catch (Exception e) { e.printStackTrace(); LogUtil.writeErrorLog("MD5", e); return null; } }
From source file:cn.usually.common.pay.union.sdk.SecureUtil.java
/** * sha1.//from w w w . j a v a 2 s .c o m * * @param datas * ? * @return */ public static byte[] sha1(byte[] data) { MessageDigest md = null; try { md = MessageDigest.getInstance(ALGORITHM_SHA1); md.reset(); md.update(data); return md.digest(); } catch (Exception e) { e.printStackTrace(); LogUtil.writeErrorLog("SHA1", e); return null; } }
From source file:org.tigase.mobile.TrustCertDialog.java
public static Dialog createDIalogInstance(final Context context, final String account, final DataCertificateException cause, final Resources res, final Runnable actionAfterOK) { final X509Certificate[] chain = cause.getChain(); final StringBuilder chainInfo = new StringBuilder(); MessageDigest sha1 = null; try {/*from w w w. ja v a 2 s.co m*/ sha1 = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { } for (int i = 0; i < chain.length; i++) { chainInfo.append(res.getString(R.string.trustcert_certchain, i)).append("\n"); chainInfo .append(res.getString(R.string.trustcert_subject, chain[i].getSubjectX500Principal().getName(X500Principal.CANONICAL).toString())) .append("\n"); chainInfo .append(res.getString(R.string.trustcert_issuer, chain[i].getIssuerX500Principal().getName(X500Principal.CANONICAL).toString())) .append("\n"); if (sha1 != null) { sha1.reset(); try { char[] sha1sum = encodeHex(sha1.digest(chain[i].getEncoded())); chainInfo.append(res.getString(R.string.trustcert_fingerprint, new String(sha1sum))) .append("\n"); } catch (CertificateEncodingException e) { } } } Builder builder = new AlertDialog.Builder(context); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.trustcert_dialog_title); builder.setMessage(res.getString(R.string.trustcert_question, chainInfo)); builder.setCancelable(true); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { SecureTrustManagerFactory.add(chain); if (actionAfterOK != null) actionAfterOK.run(); } }); builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }); return builder.create(); }