package com.xt.hb.data.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.stereotype.Component;

/**
 * 系统静态工具方法类。
 */
@Component
public abstract class Utils {

	/**
	 * 比较密码是否一致
	 * 
	 * @param pwd
	 * @param md5Pwd
	 * @return
	 */
	public static boolean pwdEquals(String pwd, String md5Pwd) {
		if (pwd == null || md5Pwd == null)
			return false;
		return md5Pwd.equals(encrypt(pwd));
	}

	/**
	 * 返回MD5加密后的密码
	 * 
	 * @param pwd
	 * @return
	 */
	public static String encrypt(String password) {
		String str = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return str;
	}
}
