mirror of
https://github.com/correl/mage.git
synced 2025-04-09 01:01:06 -09:00
37 lines
1.1 KiB
Java
37 lines
1.1 KiB
Java
package com.magefree.update;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import java.security.MessageDigest;
|
|
|
|
public class ChechsumHelper {
|
|
public static byte[] createChecksum(String filename) throws
|
|
Exception
|
|
{
|
|
InputStream fis = new FileInputStream(filename);
|
|
|
|
byte[] buffer = new byte[1024];
|
|
MessageDigest complete = MessageDigest.getInstance("SHA1");
|
|
int numRead;
|
|
do {
|
|
numRead = fis.read(buffer);
|
|
if (numRead > 0) {
|
|
complete.update(buffer, 0, numRead);
|
|
}
|
|
} while (numRead != -1);
|
|
fis.close();
|
|
return complete.digest();
|
|
}
|
|
|
|
// see this How-to for a faster way to convert
|
|
// a byte array to a HEX string
|
|
public static String getSHA1Checksum(String filename) throws Exception {
|
|
byte[] b = createChecksum(filename);
|
|
String result = "";
|
|
for (int i=0; i < b.length; i++) {
|
|
result +=
|
|
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
|
|
}
|
|
return result;
|
|
}
|
|
}
|