因为欣赏所以转载 原文地址 http://wuhaidong.iteye.com/blog/1668689
- package com.common.string;
-
- import java.util.ArrayList;
- import java.util.LinkedHashSet;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class StringUtil {
-
-
- @SuppressWarnings("unchecked")
- public static String[] split(String str, String splitsign) {
- int index;
- if (str == null || splitsign == null) {
- return null;
- }
- ArrayList al = new ArrayList();
- while ((index = str.indexOf(splitsign)) != -1) {
- al.add(str.substring(0, index));
- str = str.substring(index + splitsign.length());
- }
- al.add(str);
- return (String[]) al.toArray(new String[0]);
- }
-
-
- public static String replace(String from, String to, String source) {
- if (source == null || from == null || to == null)
- return null;
- StringBuffer str = new StringBuffer("");
- int index = -1;
- while ((index = source.indexOf(from)) != -1) {
- str.append(source.substring(0, index) + to);
- source = source.substring(index + from.length());
- index = source.indexOf(from);
- }
- str.append(source);
- return str.toString();
- }
-
-
- public static String htmlencode(String str) {
- if (str == null) {
- return null;
- }
- return replace("\"", """, replace("<", "<", str));
- }
-
-
- public static String htmldecode(String str) {
- if (str == null) {
- return null;
- }
-
- return replace(""", "\"", replace("<", "<", str));
- }
-
- private static final String _BR = "<br/>";
-
-
- public static String htmlshow(String str) {
- if (str == null) {
- return null;
- }
-
- str = replace("<", "<", str);
- str = replace(" ", " ", str);
- str = replace("\r\n", _BR, str);
- str = replace("\n", _BR, str);
- str = replace("\t", " ", str);
- return str;
- }
-
-
- public static String toLength(String str, int length) {
- if (str == null) {
- return null;
- }
- if (length <= 0) {
- return "";
- }
- try {
- if (str.getBytes("GBK").length <= length) {
- return str;
- }
- } catch (Exception e) {
- }
- StringBuffer buff = new StringBuffer();
-
- int index = 0;
- char c;
- length -= 3;
- while (length > 0) {
- c = str.charAt(index);
- if (c < 128) {
- length--;
- } else {
- length--;
- length--;
- }
- buff.append(c);
- index++;
- }
- buff.append("...");
- return buff.toString();
- }
-
-
- public static boolean isInteger(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
- return pattern.matcher(str).matches();
- }
-
-
- public static boolean isDouble(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
- return pattern.matcher(str).matches();
- }
-
-
- public static boolean isLetter(String str) {
- if (str == null || str.length() < 0) {
- return false;
- }
- Pattern pattern = Pattern.compile("[\\w\\.-_]*");
- return pattern.matcher(str).matches();
- }
-
-
- public static String parse(String content) {
- String email = null;
- if (content == null || content.length() < 1) {
- return email;
- }
-
- int beginPos;
- int i;
- String token = "@";
- String preHalf = "";
- String sufHalf = "";
-
- beginPos = content.indexOf(token);
- if (beginPos > -1) {
-
- String s = null;
- i = beginPos;
- while (i > 0) {
- s = content.substring(i - 1, i);
- if (isLetter(s))
- preHalf = s + preHalf;
- else
- break;
- i--;
- }
-
- i = beginPos + 1;
- while (i < content.length()) {
- s = content.substring(i, i + 1);
- if (isLetter(s))
- sufHalf = sufHalf + s;
- else
- break;
- i++;
- }
-
- email = preHalf + "@" + sufHalf;
- if (isEmail(email)) {
- return email;
- }
- }
- return null;
- }
-
-
- public static boolean isEmail(String email) {
- if (email == null || email.length() < 1 || email.length() > 256) {
- return false;
- }
- Pattern pattern = Pattern
- .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
- return pattern.matcher(email).matches();
- }
-
-
- public static boolean isChinese(String str) {
- Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
- return pattern.matcher(str).matches();
- }
-
-
- public static boolean isBlank(String str) {
- return str == null || str.trim().length() == 0;
- }
-
-
- public static boolean isPrime(int x) {
- if (x <= 7) {
- if (x == 2 || x == 3 || x == 5 || x == 7)
- return true;
- }
- int c = 7;
- if (x % 2 == 0)
- return false;
- if (x % 3 == 0)
- return false;
- if (x % 5 == 0)
- return false;
- int end = (int) Math.sqrt(x);
- while (c <= end) {
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 4;
- if (x % c == 0) {
- return false;
- }
- c += 6;
- if (x % c == 0) {
- return false;
- }
- c += 2;
- if (x % c == 0) {
- return false;
- }
- c += 6;
- }
- return true;
- }
-
-
- public static String hangeToBig(String str) {
- double value;
- try {
- value = Double.parseDouble(str.trim());
- } catch (Exception e) {
- return null;
- }
- char[] hunit = { ‘拾‘, ‘佰‘, ‘仟‘ };
- char[] vunit = { ‘万‘, ‘亿‘ };
- char[] digit = { ‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘ };
- long midVal = (long) (value * 100);
- String valStr = String.valueOf(midVal);
-
- String head = valStr.substring(0, valStr.length() - 2);
- String rail = valStr.substring(valStr.length() - 2);
-
- String prefix = "";
- String suffix = "";
-
- if (rail.equals("00")) {
- suffix = "整";
- } else {
- suffix = digit[rail.charAt(0) - ‘0‘] + "角"
- + digit[rail.charAt(1) - ‘0‘] + "分";
- }
-
- char[] chDig = head.toCharArray();
- char zero = ‘0‘;
- byte zeroSerNum = 0;
- for (int i = 0; i < chDig.length; i++) {
- int idx = (chDig.length - i - 1) % 4;
- int vidx = (chDig.length - i - 1) / 4;
- if (chDig[i] == ‘0‘) {
- zeroSerNum++;
- if (zero == ‘0‘) {
- zero = digit[0];
- } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
- prefix += vunit[vidx - 1];
- zero = ‘0‘;
- }
- continue;
- }
- zeroSerNum = 0;
- if (zero != ‘0‘) {
- prefix += zero;
- zero = ‘0‘;
- }
- prefix += digit[chDig[i] - ‘0‘];
- if (idx > 0)
- prefix += hunit[idx - 1];
- if (idx == 0 && vidx > 0) {
- prefix += vunit[vidx - 1];
- }
- }
-
- if (prefix.length() > 0)
- prefix += ‘圆‘;
- return prefix + suffix;
- }
-
-
- @SuppressWarnings("unused")
- private static String removeSameString(String str) {
- Set<String> mLinkedSet = new LinkedHashSet<String>();
- String[] strArray = str.split(" ");
- StringBuffer sb = new StringBuffer();
-
- for (int i = 0; i < strArray.length; i++) {
- if (!mLinkedSet.contains(strArray[i])) {
- mLinkedSet.add(strArray[i]);
- sb.append(strArray[i] + " ");
- }
- }
- System.out.println(mLinkedSet);
- return sb.toString();
- }
-
-
- public static String encoding(String src) {
- if (src == null)
- return "";
- StringBuilder result = new StringBuilder();
- if (src != null) {
- src = src.trim();
- for (int pos = 0; pos < src.length(); pos++) {
- switch (src.charAt(pos)) {
- case ‘\"‘:
- result.append(""");
- break;
- case ‘<‘:
- result.append("<");
- break;
- case ‘>‘:
- result.append(">");
- break;
- case ‘\‘‘:
- result.append("'");
- break;
- case ‘&‘:
- result.append("&");
- break;
- case ‘%‘:
- result.append("&pc;");
- break;
- case ‘_‘:
- result.append("&ul;");
- break;
- case ‘#‘:
- result.append("&shap;");
- break;
- case ‘?‘:
- result.append("&ques;");
- break;
- default:
- result.append(src.charAt(pos));
- break;
- }
- }
- }
- return result.toString();
- }
-
-
- public static boolean isHandset(String handset) {
- try {
- String regex = "^1[\\d]{10}$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(handset);
- return matcher.matches();
-
- } catch (RuntimeException e) {
- return false;
- }
- }
-
-
- public static String decoding(String src) {
- if (src == null)
- return "";
- String result = src;
- result = result.replace(""", "\"").replace("'", "\‘");
- result = result.replace("<", "<").replace(">", ">");
- result = result.replace("&", "&");
- result = result.replace("&pc;", "%").replace("&ul", "_");
- result = result.replace("&shap;", "#").replace("&ques", "?");
- return result;
- }
-
-
- public static void main(String[] args) {
- String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
- String from = "efg";
- String to = "房贺威";
- System.out.println("在字符串source中,用to替换from,替换结果为:"
- + replace(from, to, source));
- System.out.println("返回指定字节长度的字符串:"
- + toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));
- System.out.println("判断是否为整数:" + isInteger("+0"));
- System.out.println("判断是否为浮点数,包括double和float:" + isDouble("+0.36"));
- System.out.println("判断输入的字符串是否符合Email样式:" +
- isEmail("fhwbj@163.com"));
- System.out.println("判断输入的字符串是否为纯汉字:" + isChinese("你好!"));
- System.out.println("判断输入的数据是否是质数:" + isPrime(12));
- System.out.println("人民币转换成大写:" + hangeToBig("10019658"));
- System.out.println("去掉字符串中重复的子字符串:" + removeSameString("100 100 9658"));
- System.out.println("过滤特殊字符:" + encoding("100\"s<>fdsd100 9658"));
- System.out.println("判断是不是合法的手机号码:" + isHandset("15981807340"));
- System.out.println("从字符串中取值Email:" + parse("159818 fwhbj@163.com07340"));
- }
- }
- package com.common.time;
-
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- public class DateUtil {
-
- public static Date date = null;
-
- public static DateFormat dateFormat = null;
-
- public static Calendar calendar = null;
-
-
- public static Date parseDate(String dateStr, String format) {
- try {
- dateFormat = new SimpleDateFormat(format);
- String dt = dateStr.replaceAll("-", "/");
- if ((!dt.equals("")) && (dt.length() < format.length())) {
- dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]",
- "0");
- }
- date = (Date) dateFormat.parse(dt);
- } catch (Exception e) {
- }
- return date;
- }
-
-
- public static Date parseDate(String dateStr) {
- return parseDate(dateStr, "yyyy/MM/dd");
- }
-
-
- public static String format(Date date, String format) {
- String result = "";
- try {
- if (date != null) {
- dateFormat = new SimpleDateFormat(format);
- result = dateFormat.format(date);
- }
- } catch (Exception e) {
- }
- return result;
- }
-
-
- public static String format(Date date) {
- return format(date, "yyyy/MM/dd");
- }
-
-
- public static int getYear(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.YEAR);
- }
-
-
- public static int getMonth(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MONTH) + 1;
- }
-
-
- public static int getDay(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.DAY_OF_MONTH);
- }
-
-
- public static int getHour(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.HOUR_OF_DAY);
- }
-
-
- public static int getMinute(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MINUTE);
- }
-
-
- public static int getSecond(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.SECOND);
- }
-
-
- public static long getMillis(Date date) {
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.getTimeInMillis();
- }
-
-
- public static String getDate(Date date) {
- return format(date, "yyyy/MM/dd");
- }
-
-
- public static String getTime(Date date) {
- return format(date, "HH:mm:ss");
- }
-
-
- public static String getDateTime(Date date) {
- return format(date, "yyyy/MM/dd HH:mm:ss");
- }
-
-
- public static Date addDate(Date date, int day) {
- calendar = Calendar.getInstance();
- long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;
- calendar.setTimeInMillis(millis);
- return calendar.getTime();
- }
-
-
- public static int diffDate(Date date, Date date1) {
- return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
- }
-
-
- public static String getMonthBegin(String strdate) {
- date = parseDate(strdate);
- return format(date, "yyyy-MM") + "-01";
- }
-
-
- public static String getMonthEnd(String strdate) {
- date = parseDate(getMonthBegin(strdate));
- calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.MONTH, 1);
- calendar.add(Calendar.DAY_OF_YEAR, -1);
- return formatDate(calendar.getTime());
- }
-
-
- public static String formatDate(Date date) {
- return formatDateByFormat(date, "yyyy-MM-dd");
- }
-
-
- public static String formatDateByFormat(Date date, String format) {
- String result = "";
- if (date != null) {
- try {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- result = sdf.format(date);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- return result;
- }
-
- public static void main(String[] args) {
- Date d = new Date();
- System.out.println(d.toString());
- System.out.println(formatDate(d).toString());
- System.out.println(getMonthBegin(formatDate(d).toString()));
- System.out.println(getMonthBegin("2008/07/19"));
- System.out.println(getMonthEnd("2008/07/19"));
- System.out.println(addDate(d,15).toString());
- }
-
- }
-
- package com.founder.mnp.utl;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
-
-
- public class ZipUtils {
- static final int BUFFER = 2048;
-
- public static boolean zip( String[] filename ,String outname){
-
- boolean test = true;
- try {
- BufferedInputStream origin = null;
- FileOutputStream dest = new FileOutputStream(outname);
- ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
- dest));
- byte data[] = new byte[BUFFER];
-
-
-
- for (int i = 0; i < filename.length; i++) {
- File file = new File(filename[i]);
- FileInputStream fi = new FileInputStream(file);
- origin = new BufferedInputStream(fi, BUFFER);
- ZipEntry entry = new ZipEntry(file.getName());
- out.putNextEntry(entry);
- int count;
- while ((count = origin.read(data, 0, BUFFER)) != -1) {
- out.write(data, 0, count);
- }
- origin.close();
- }
- out.close();
- } catch (Exception e) {
- test = false;
- e.printStackTrace();
- }
- return test;
- }
-
-
-
- public static void main(String argv[]) {
-
- String[] filenames = new String[]{"F:\\12.jpg"};
- zip(filenames,"F:/12.zip");
- }
- }
-
- package com.test.test;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.zip.CRC32;
- import java.util.zip.CheckedInputStream;
-
- public class MD5 {
-
- private static final int S11 = 7;
- private static final int S12 = 12;
- private static final int S13 = 17;
- private static final int S14 = 22;
- private static final int S21 = 5;
- private static final int S22 = 9;
- private static final int S23 = 14;
- private static final int S24 = 20;
- private static final int S31 = 4;
- private static final int S32 = 11;
- private static final int S33 = 16;
- private static final int S34 = 23;
- private static final int S41 = 6;
- private static final int S42 = 10;
- private static final int S43 = 15;
- private static final int S44 = 21;
- private static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0 };
-
- private long[] state = new long[4];
- private long[] count = new long[2];
-
- private byte[] buffer = new byte[64];
-
- private String digestHexStr;
-
- private byte[] digest = new byte[16];
-
-
- public String getMD5ofStr(String inbuf) {
- md5Init();
- md5Update(inbuf.getBytes(), inbuf.length());
- md5Final();
- digestHexStr = "";
- for (int i = 0; i < 16; i++) {
- digestHexStr += byteHEX(digest[i]);
- }
- return digestHexStr;
- }
-
-
- public MD5() {
- md5Init();
- return;
- }
-
-
- private void md5Init() {
- count[0] = 0L;
- count[1] = 0L;
-
- state[0] = 0x67452301L;
- state[1] = 0xefcdab89L;
- state[2] = 0x98badcfeL;
- state[3] = 0x10325476L;
- return;
- }
-
- /*
- * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
- * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。
- */
- private long F(long x, long y, long z) {
- return (x & y) | ((~x) & z);
- }
-
- private long G(long x, long y, long z) {
- return (x & z) | (y & (~z));
- }
-
- private long H(long x, long y, long z) {
- return x ^ y ^ z;
- }
-
- private long I(long x, long y, long z) {
- return y ^ (x | (~z));
- }
-
-
- private long FF(long a, long b, long c, long d, long x, long s, long ac) {
- a += F(b, c, d) + x + ac;
- a = ((int) a << s) | ((int) a >>> (32 - s));
- a += b;
- return a;
- }
-
- private long GG(long a, long b, long c, long d, long x, long s, long ac) {
- a += G(b, c, d) + x + ac;
- a = ((int) a << s) | ((int) a >>> (32 - s));
- a += b;
- return a;
- }
-
- private long HH(long a, long b, long c, long d, long x, long s, long ac) {
- a += H(b, c, d) + x + ac;
- a = ((int) a << s) | ((int) a >>> (32 - s));
- a += b;
- return a;
- }
-
- private long II(long a, long b, long c, long d, long x, long s, long ac) {
- a += I(b, c, d) + x + ac;
- a = ((int) a << s) | ((int) a >>> (32 - s));
- a += b;
- return a;
- }
-
-
- private void md5Update(byte[] inbuf, int inputLen) {
- int i, index, partLen;
- byte[] block = new byte[64];
- index = (int) (count[0] >>> 3) & 0x3F;
-
- if ((count[0] += (inputLen << 3)) < (inputLen << 3))
- count[1]++;
- count[1] += (inputLen >>> 29);
- partLen = 64 - index;
-
- if (inputLen >= partLen) {
- md5Memcpy(buffer, inbuf, index, 0, partLen);
- md5Transform(buffer);
- for (i = partLen; i + 63 < inputLen; i += 64) {
- md5Memcpy(block, inbuf, 0, i, 64);
- md5Transform(block);
- }
- index = 0;
- } else
- i = 0;
-
- md5Memcpy(buffer, inbuf, index, i, inputLen - i);
- }
-
-
- private void md5Final() {
- byte[] bits = new byte[8];
- int index, padLen;
-
- Encode(bits, count, 8);
-
- index = (int) (count[0] >>> 3) & 0x3f;
- padLen = (index < 56) ? (56 - index) : (120 - index);
- md5Update(PADDING, padLen);
-
- md5Update(bits, 8);
-
- Encode(digest, state, 16);
- }
-
-
- private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
- int len) {
- int i;
- for (i = 0; i < len; i++)
- output[outpos + i] = input[inpos + i];
- }
-
-
- private void md5Transform(byte block[]) {
- long a = state[0], b = state[1], c = state[2], d = state[3];
- long[] x = new long[16];
- Decode(x, block, 64);
-
-
- a = FF(a, b, c, d, x[0], S11, 0xd76aa478L);
- d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L);
- c = FF(c, d, a, b, x[2], S13, 0x242070dbL);
- b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL);
- a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL);
- d = FF(d, a, b, c, x[5], S12, 0x4787c62aL);
- c = FF(c, d, a, b, x[6], S13, 0xa8304613L);
- b = FF(b, c, d, a, x[7], S14, 0xfd469501L);
- a = FF(a, b, c, d, x[8], S11, 0x698098d8L);
- d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL);
- c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L);
- b = FF(b, c, d, a, x[11], S14, 0x895cd7beL);
- a = FF(a, b, c, d, x[12], S11, 0x6b901122L);
- d = FF(d, a, b, c, x[13], S12, 0xfd987193L);
- c = FF(c, d, a, b, x[14], S13, 0xa679438eL);
- b = FF(b, c, d, a, x[15], S14, 0x49b40821L);
-
- a = GG(a, b, c, d, x[1], S21, 0xf61e2562L);
- d = GG(d, a, b, c, x[6], S22, 0xc040b340L);
- c = GG(c, d, a, b, x[11], S23, 0x265e5a51L);
- b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL);
- a = GG(a, b, c, d, x[5], S21, 0xd62f105dL);
- d = GG(d, a, b, c, x[10], S22, 0x2441453L);
- c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L);
- b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L);
- a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L);
- d = GG(d, a, b, c, x[14], S22, 0xc33707d6L);
- c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L);
- b = GG(b, c, d, a, x[8], S24, 0x455a14edL);
- a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L);
- d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L);
- c = GG(c, d, a, b, x[7], S23, 0x676f02d9L);
- b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL);
-
- a = HH(a, b, c, d, x[5], S31, 0xfffa3942L);
- d = HH(d, a, b, c, x[8], S32, 0x8771f681L);
- c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L);
- b = HH(b, c, d, a, x[14], S34, 0xfde5380cL);
- a = HH(a, b, c, d, x[1], S31, 0xa4beea44L);
- d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L);
- c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L);
- b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L);
- a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L);
- d = HH(d, a, b, c, x[0], S32, 0xeaa127faL);
- c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L);
- b = HH(b, c, d, a, x[6], S34, 0x4881d05L);
- a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L);
- d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L);
- c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L);
- b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L);
-
- a = II(a, b, c, d, x[0], S41, 0xf4292244L);
- d = II(d, a, b, c, x[7], S42, 0x432aff97L);
- c = II(c, d, a, b, x[14], S43, 0xab9423a7L);
- b = II(b, c, d, a, x[5], S44, 0xfc93a039L);
- a = II(a, b, c, d, x[12], S41, 0x655b59c3L);
- d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L);
- c = II(c, d, a, b, x[10], S43, 0xffeff47dL);
- b = II(b, c, d, a, x[1], S44, 0x85845dd1L);
- a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL);
- d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L);
- c = II(c, d, a, b, x[6], S43, 0xa3014314L);
- b = II(b, c, d, a, x[13], S44, 0x4e0811a1L);
- a = II(a, b, c, d, x[4], S41, 0xf7537e82L);
- d = II(d, a, b, c, x[11], S42, 0xbd3af235L);
- c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL);
- b = II(b, c, d, a, x[9], S44, 0xeb86d391L);
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
- }
-
-
- private void Encode(byte[] output, long[] input, int len) {
- int i, j;
- for (i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = (byte) (input[i] & 0xffL);
- output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
- output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
- output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
- }
- }
-
-
- private void Decode(long[] output, byte[] input, int len) {
- int i, j;
- for (i = 0, j = 0; j < len; i++, j += 4)
- output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
- | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
- return;
- }
-
-
- public static long b2iu(byte b) {
- return b < 0 ? b & 0x7F + 128 : b;
- }
-
-
- public static String byteHEX(byte ib) {
- char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘,
- ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
- char[] ob = new char[2];
- ob[0] = Digit[(ib >>> 4) & 0X0F];
- ob[1] = Digit[ib & 0X0F];
- String s = new String(ob);
- return s;
- }
-
-
-
-
-
- public static String getFileMD5(File file) {
- if (!file.isFile()) {
- return null;
- }
- MessageDigest digest = null;
- FileInputStream in = null;
- byte buffer[] = new byte[1024];
- int len;
- try {
- digest = MessageDigest.getInstance("MD5");
- in = new FileInputStream(file);
- while ((len = in.read(buffer, 0, 1024)) != -1) {
- digest.update(buffer, 0, len);
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- BigInteger bigInt = new BigInteger(1, digest.digest());
- return bigInt.toString(16).toUpperCase();
- }
-
-
- public static String getFileSHA1(File file) {
- if (!file.isFile()) {
- return null;
- }
- MessageDigest digest = null;
- FileInputStream in = null;
- byte buffer[] = new byte[1024];
- int len;
- try {
- digest = MessageDigest.getInstance("SHA1");
- in = new FileInputStream(file);
- while ((len = in.read(buffer, 0, 1024)) != -1) {
- digest.update(buffer, 0, len);
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- BigInteger bigInt = new BigInteger(1, digest.digest());
- return bigInt.toString(16).toUpperCase();
- }
-
-
- public static String getFileCRC32(File file) {
- FileInputStream fileinputstream;
- try {
- fileinputstream = new FileInputStream(file);
- CRC32 crc32 = new CRC32();
- for (CheckedInputStream checkedinputstream = new CheckedInputStream(
- fileinputstream, crc32); checkedinputstream.read() != -1;) {
- }
- return Long.toHexString(crc32.getValue());
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
-
-
- public static Map<String, String> getDirMD5(File file, boolean listChild) {
- if (!file.isDirectory()) {
- return null;
- }
-
- Map<String, String> map = new HashMap<String, String>();
- String md5;
- File files[] = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File f = files[i];
- if (f.isDirectory() && listChild) {
- map.putAll(getDirMD5(f, listChild));
- } else {
- md5 = getFileMD5(f);
- if (md5 != null) {
- map.put(f.getPath(), md5);
- }
- }
- }
- return map;
- }
-
-
- public static Map<String, String> getDirSHA1(File file, boolean listChild) {
- if (!file.isDirectory()) {
- return null;
- }
-
- Map<String, String> map = new HashMap<String, String>();
- String sha1;
- File files[] = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File f = files[i];
- if (f.isDirectory() && listChild) {
- map.putAll(getDirSHA1(f, listChild));
- } else {
- sha1 = getFileSHA1(f);
- if (sha1 != null) {
- map.put(f.getPath(), sha1);
- }
- }
- }
- return map;
- }
-
-
- public static Map<String, String> getDirCRC32(File file, boolean listChild) {
- if (!file.isDirectory()) {
- return null;
- }
-
- Map<String, String> map = new HashMap<String, String>();
- String crc32;
- File files[] = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File f = files[i];
- if (f.isDirectory() && listChild) {
- map.putAll(getDirCRC32(f, listChild));
- } else {
- crc32 = getFileCRC32(f);
- if (crc32 != null) {
- map.put(f.getPath(), crc32);
- }
- }
- }
- return map;
- }
- }
java经典代码
原文:https://www.cnblogs.com/yuyanhzao/p/12133881.html