package com.tzy.common.utils;

import java.util.Date;
import java.util.Random;

public class JsThirdSignUtil {

    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static String[] nonceStrings = new String[]
    {
        "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
        "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
        "0","1","2","3","4","5","6","7","8","9"
    };
    public static String byteToHex(byte... bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 随机串
     * @return
     */
    public static String create_nonce_str(){
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        Integer length = nonceStrings.length;
        //生成15位数的随机字符串，当然也可以通过控制对应字符串大小生成，但是至多不超过32位
        for (int i = 0; i < 15; i++)
        {
            sb.append(nonceStrings[random.nextInt(length - 1)]);//通过random获得的随机索引到，NonceStrings数组中获取对应数组值
        }
        return sb.toString();
    }

    /**
     * 时间戳
     * @return
     */
    public static String create_timestamp(){
        Long time = new Date().getTime() / 1000;
        return time.toString();
    }

}
