Search in sources :

Example 76 with MessageDigest

use of java.security.MessageDigest in project translationstudio8 by heartsome.

the class FileBasic method getMD5.

/**
	 * 计算文件的 MD5 码.
	 * @param file
	 *            	文件对象
	 * @return String
	 * 				文件的 MD5 编码
	 * @throws NoSuchAlgorithmException
	 *             the no such algorithm exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
public static String getMD5(File file) throws NoSuchAlgorithmException, IOException {
    FileInputStream fis = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        fis = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int length = -1;
        while ((length = fis.read(buffer)) != -1) {
            md.update(buffer, 0, length);
        }
        return bytesToString(md.digest());
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}
Also used : MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 77 with MessageDigest

use of java.security.MessageDigest in project xUtils3 by wyouflf.

the class MD5 method md5.

public static String md5(File file) throws IOException {
    MessageDigest messagedigest = null;
    FileInputStream in = null;
    FileChannel ch = null;
    byte[] encodeBytes = null;
    try {
        messagedigest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        messagedigest.update(byteBuffer);
        encodeBytes = messagedigest.digest();
    } catch (NoSuchAlgorithmException neverHappened) {
        throw new RuntimeException(neverHappened);
    } finally {
        IOUtil.closeQuietly(in);
        IOUtil.closeQuietly(ch);
    }
    return toHexString(encodeBytes);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 78 with MessageDigest

use of java.security.MessageDigest in project XobotOS by xamarin.

the class LockPatternUtils method patternToHash.

/*
     * Generate an SHA-1 hash for the pattern. Not the most secure, but it is
     * at least a second level of protection. First level is that the file
     * is in a location only readable by the system process.
     * @param pattern the gesture pattern.
     * @return the hash of the pattern in a byte array.
     */
private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
    if (pattern == null) {
        return null;
    }
    final int patternSize = pattern.size();
    byte[] res = new byte[patternSize];
    for (int i = 0; i < patternSize; i++) {
        LockPatternView.Cell cell = pattern.get(i);
        res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] hash = md.digest(res);
        return hash;
    } catch (NoSuchAlgorithmException nsa) {
        return res;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 79 with MessageDigest

use of java.security.MessageDigest in project hibernate-orm by hibernate.

the class Constraint method hashedName.

/**
	 * Hash a constraint name using MD5. Convert the MD5 digest to base 35
	 * (full alphanumeric), guaranteeing
	 * that the length of the name will always be smaller than the 30
	 * character identifier restriction enforced by a few dialects.
	 * 
	 * @param s
	 *            The name to be hashed.
	 * @return String The hased name.
	 */
public static String hashedName(String s) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(s.getBytes());
        byte[] digest = md.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        // character identifier restriction enforced by a few dialects.
        return bigInt.toString(35);
    } catch (NoSuchAlgorithmException e) {
        throw new HibernateException("Unable to generate a hashed Constraint name!", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 80 with MessageDigest

use of java.security.MessageDigest in project spring-boot by spring-projects.

the class ApplicationTemp method generateHash.

private byte[] generateHash(Class<?> sourceClass) {
    ApplicationHome home = new ApplicationHome(sourceClass);
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-1");
        update(digest, home.getSource());
        update(digest, home.getDir());
        update(digest, System.getProperty("user.dir"));
        update(digest, System.getProperty("java.home"));
        update(digest, System.getProperty("java.class.path"));
        update(digest, System.getProperty("sun.java.command"));
        update(digest, System.getProperty("sun.boot.class.path"));
        return digest.digest();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22