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();
}
}
}
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);
}
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;
}
}
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);
}
}
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);
}
}
Aggregations