PhoneCheckUtil手机号正则验证工具类 和 SecureRandom 生成安全随机数工具类

 PhoneCheckUtil 

package com.tnar.utils;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年04月19日 16:14:36
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class PhoneCheckUtil {

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
       // "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        String regExp = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数,5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static void main(String[] args){
     System.out.println( PhoneCheckUtil.isChinaPhoneLegal(""));
    }

}
RandomPwdUtil
package com.tnar.utils;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author dzx
 * @ClassName:
 * @Description: 生成随机长度的密码工具类
 * @date 2019年04月20日 16:30:06
 */
public class RandomPwdUtil {


    enum PWD_FORMAT {
        LETTER_NUMBER("abcdefghijklmnopqrstuvwxyz1234567890"),
        NUMBER("1234567890");
        String format;

        PWD_FORMAT(String format) {
            this.format = format;
        }

        public String getFormat() {
            return format;
        }

        public void setFormat(String format) {
            this.format = format;
        }
    }

    /**
     * 产生密钥信息
     * 采用安全的生成随机数方法(SecureRandom)
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    /**
     * @param length 密码长度
     * @param format 指定格式
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String genRandomPwd(int length, PWD_FORMAT format) throws NoSuchAlgorithmException {
        try {
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < length; i++) {
                int number = secureRandom.nextInt(format.getFormat().length());
                stringBuffer.append(format.getFormat().charAt(number));
            }
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成x 位数的随机密码
     *
     * @param length
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String genRandomPwd(int length) {
        try {
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < length; i++) {
                stringBuffer.append(secureRandom.nextInt(10));
            }
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println(RandomPwdUtil.genRandomPwd(8));
        System.out.println(RandomPwdUtil.genRandomPwd(10, PWD_FORMAT.LETTER_NUMBER));
    }
}

 

 

你可能感兴趣的:(工具类)