📄 md5.java
字号:
/**
* Update buffer with a single integer (only & 0xff part is used,
* as a byte)
*
* @param i Integer value, which is then converted to byte as i & 0xff
**/
public void Update (int i) {
Update((byte) (i & 0xff));
}
private byte[] Encode (int input[], int len) {
int i, j;
byte out[];
out = new byte[len];
for (i = j = 0; j < len; i++, j += 4) {
out[j] = (byte) (input[i] & 0xff);
out[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
}
return out;
}
/**
* Returns array of bytes (16 bytes) representing hash as of the
* current state of this object. Note: getting a hash does not
* invalidate the hash object, it only creates a copy of the real
* state which is finalized.
*
* @return Array of 16 bytes, the hash of all updated bytes
**/
public synchronized byte[] Final () {
byte bits[];
int index, padlen;
MD5State fin;
if (finals == null) {
fin = new MD5State(state);
int[] count_ints = {(int) (fin.count << 3), (int) (fin.count >> 29)};
bits = Encode(count_ints, 8);
index = (int) (fin.count & 0x3f);
padlen = (index < 56) ? (56 - index) : (120 - index);
Update(fin, padding, 0, padlen);
Update(fin, bits, 0, 8);
/* Update() sets finals to null */
finals = fin;
}
return Encode(finals.state, 16);
}
private static final char[] HEX_CHARS = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f',};
/**
* Turns array of bytes into string representing each byte as
* unsigned hex number.
*
* @param hash Array of bytes to convert to hex-string
* @return Generated hex string
*/
public static String asHex (byte hash[]) {
char buf[] = new char[hash.length * 2];
for (int i = 0, x = 0; i < hash.length; i++) {
buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf];
buf[x++] = HEX_CHARS[hash[i] & 0xf];
}
return new String(buf);
}
/**
* Returns 32-character hex representation of this objects hash
*
* @return String of this object's hash
*/
public String asHex () {
return asHex(this.Final());
}
public static synchronized final void initNativeLibrary (boolean disallow_lib_loading) {
if (disallow_lib_loading) {
native_lib_init_pending = false;
} else {
_initNativeLibrary();
}
}
private static synchronized final void _initNativeLibrary () {
if (!native_lib_init_pending) return;
native_lib_loaded = _loadNativeLibrary();
native_lib_init_pending = false;
}
private static synchronized final boolean _loadNativeLibrary () {
try {
// don't try to load if the right property is set
String prop = System.getProperty("com.twmacinta.util.MD5.NO_NATIVE_LIB");
if (prop != null) {
prop = prop.trim();
if (prop.equalsIgnoreCase("true") || prop.equals("1")) return false;
}
// the library to load can be specified as a property
File f;
prop = System.getProperty("com.twmacinta.util.MD5.NATIVE_LIB_FILE");
if (prop != null) {
f = new File(prop);
if (f.canRead()) {
System.load(f.getAbsolutePath());
return true;
}
}
// determine the operating system and architecture
String os_name = System.getProperty("os.name");
String os_arch = System.getProperty("os.arch");
if (os_name == null || os_arch == null) return false;
os_name = os_name.toLowerCase();
os_arch = os_arch.toLowerCase();
// define settings which are OS arch architecture independent
File arch_lib_path = null;
String arch_libfile_suffix = null;
// fill in settings for Linux on x86
if (os_name.equals("linux") &&
(os_arch.equals("x86") ||
os_arch.equals("i386") ||
os_arch.equals("i486") ||
os_arch.equals("i586") ||
os_arch.equals("i686"))) {
arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_x86");
arch_libfile_suffix = ".so";
}
// fill in settings for Windows on x86
else if (os_name.startsWith("windows ") &&
(os_arch.equals("x86") ||
os_arch.equals("i386") ||
os_arch.equals("i486") ||
os_arch.equals("i586") ||
os_arch.equals("i686"))) {
arch_lib_path = new File(new File(new File("lib"), "arch"), "win32_x86");
arch_libfile_suffix = ".dll";
}
// fill in settings for Mac OS X on PPC
else if (os_name.startsWith("mac os x") &&
(os_arch.equals("ppc"))) {
arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_ppc");
arch_libfile_suffix = ".jnilib";
}
// fill in settings for Mac OS X on x86
else if (os_name.startsWith("mac os x") &&
(os_arch.equals("x86") ||
os_arch.equals("i386") ||
os_arch.equals("i486") ||
os_arch.equals("i586") ||
os_arch.equals("i686"))) {
arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86");
arch_libfile_suffix = ".jnilib";
}
// default to .so files with no architecture specific subdirectory
else {
arch_libfile_suffix = ".so";
}
// build the required filename
String fname = "MD5" + arch_libfile_suffix;
// try the architecture specific directory
if (arch_lib_path != null) {
f = new File(arch_lib_path, fname);
if (f.canRead()) {
System.load(f.getAbsolutePath());
return true;
}
}
// try the "lib" subdirectory
f = new File(new File("lib"), fname);
if (f.canRead()) {
System.load(f.getAbsolutePath());
return true;
}
// try the working directory
f = new File(fname);
if (f.canRead()) {
System.load(f.getAbsolutePath());
return true;
}
}
// discard SecurityExceptions
catch (SecurityException e) {}
// unable to load
return false;
}
/**
* Calculates and returns the hash of the contents of the given file.
**/
public static byte[] getHash (File f) throws IOException {
if (!f.exists()) throw new FileNotFoundException(f.toString());
InputStream close_me = null;
try {
long buf_size = f.length();
if (buf_size < 512) buf_size = 512;
if (buf_size > 65536) buf_size = 65536;
byte[] buf = new byte[(int) buf_size];
MD5InputStream in = new MD5InputStream(new FileInputStream(f));
close_me = in;
while (in.read(buf) != -1);
in.close();
return in.hash();
} catch (IOException e) {
if (close_me != null) try { close_me.close(); } catch (Exception e2) {}
throw e;
}
}
/**
* @return true iff the first 16 bytes of both hash1 and hash2 are
* equal; both hash1 and hash2 are null; or either hash
* array is less than 16 bytes in length and their lengths and
* all of their bytes are equal.
**/
public static boolean hashesEqual (byte[] hash1, byte[] hash2) {
if (hash1 == null) return hash2 == null;
if (hash2 == null) return false;
int targ = 16;
if (hash1.length < 16) {
if (hash2.length != hash1.length) return false;
targ = hash1.length;
} else if (hash2.length < 16) {
return false;
}
for (int i = 0; i < targ; i++) {
if (hash1[i] != hash2[i]) return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -