List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.linkedin.databus2.schemas.utils.Utils.java
public static byte[] md5(byte[] bytes) { try {/*from w ww . jav a 2 s .c om*/ MessageDigest digest = MessageDigest.getInstance("md5"); return digest.digest(bytes); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("This can't happen.", e); } }
From source file:com.constellio.app.modules.es.connectors.http.utils.DigestUtil.java
public static String digest(final byte[] content) throws NoSuchAlgorithmException { MessageDigest shaDigester = MessageDigest.getInstance("SHA"); shaDigester.update(content);//from w w w.jav a 2 s . c om byte[] shaDigest = shaDigester.digest(); String digestString = new String(Base64.encodeBase64(shaDigest)); return digestString; }
From source file:com.intuit.tank.vm.common.PasswordEncoder.java
/** * Encodes the password using SHA-1 algorithm. * /*w ww .j av a 2 s . co m*/ * @param password * the password to encode * @return a base64 encoded has of the password. */ public static final String encodePassword(String password) { try { byte[] digest = MessageDigest.getInstance(DEFAULT_ALGORITHM).digest(password.getBytes()); return new String(Base64.encodeBase64(digest)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:com.eschava.forevernote.EvernoteUtil.java
public static Resource createResource(InputStream stream, String contentType) throws IOException { try {// w w w. j a v a2 s .co m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); FileCopyUtils.copy(stream, outputStream); byte[] bytes = outputStream.toByteArray(); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] hash = messageDigest.digest(bytes); Data data = new Data(); data.setBody(bytes); data.setBodyHash(hash); Resource resource = new Resource(); resource.setMime(contentType); resource.setData(data); return resource; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); // is not possible } }
From source file:Main.java
public static byte[] generateCramMD5HexClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception { String macAlgorithm = "HmacMD5"; byte[] digestedPasswordBytes = MessageDigest.getInstance("MD5").digest(userPassword.getBytes("UTF-8")); byte[] hexEncodedDigestedPasswordBytes = toHex(digestedPasswordBytes).getBytes("UTF-8"); Mac mac = Mac.getInstance(macAlgorithm); mac.init(new SecretKeySpec(hexEncodedDigestedPasswordBytes, macAlgorithm)); final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes); String responseAsString = userName + " " + toHex(messageAuthenticationCode); return responseAsString.getBytes(); }
From source file:com.likya.tlos.utils.PasswordService.java
public static synchronized String encrypt(String plaintext) throws Exception { MessageDigest md = null;//from ww w . jav a 2 s.c om try { md = MessageDigest.getInstance("SHA"); // step 2 //$NON-NLS-1$ } catch (NoSuchAlgorithmException e) { throw new Exception(e.getMessage()); } try { md.update(plaintext.getBytes("UTF-8")); // step 3 //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { throw new Exception(e.getMessage()); } byte raw[] = md.digest(); // step 4 /** * Sun uygulamasndan vazgeilip apache uygulamas kullanld * 16.03.2011 * */ // String hash = (new BASE64Encoder()).encode(raw); // step 5 String hash = Base64.encodeBase64String(raw); return hash; // step 6 }
From source file:EncryptUtil.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 2s . 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 encode(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) { //logger.error("Exception:{}", 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(); StringBuilder buf = new StringBuilder(); 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:cn.edu.bit.whitesail.utils.MD5Signature.java
public static String calculate(byte[] byteArray) { StringBuffer result = null;//from w ww . jav a 2s. c o m if (byteArray == null) return null; try { MessageDigest m = MessageDigest.getInstance("MD5"); m.update(byteArray); result = new StringBuffer(new BigInteger(1, m.digest()).toString(16)); for (int i = 0; i < 32 - result.length(); i++) result.insert(0, '0'); } catch (NoSuchAlgorithmException ex) { LOG.fatal("MD5 Hashing Failed,System is going down"); System.exit(1); } return result.toString(); }
From source file:Main.java
/** * Get current certificate fingerprint/*www . j a v a 2 s . c om*/ * * @param ctx context of application * @param packageName your package name * @return Base64 packed SHA fingerprint of your packet certificate */ public static String[] getCertificateFingerprint(Context ctx, String packageName) { try { if (ctx == null || ctx.getPackageManager() == null) return null; @SuppressLint("PackageManagerGetSignatures") PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES); assert info.signatures != null; String[] result = new String[info.signatures.length]; int i = 0; for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); result[i++] = toHex(md.digest()); } return result; } catch (Exception e) { return null; } }
From source file:Main.java
/** * Generate the SHA1 hash of a message /* w w w. ja va 2 s. co m*/ * @param message * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static byte[] SHA1(byte[] message) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md; md = MessageDigest.getInstance("SHA-1"); md.update(message); return md.digest(); }