package com.crunii.micro.service.sysmgr.utils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.stream.Collectors;

public class DigestUtil {

    public static String sha1(String str){

        try {
            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
            byte[] bytes = sha1.digest(str.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                result.append(String.format("%02x", b));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        String ticket = "O3SMpm8bG7kJnF36aXbe8_MrLc-CDvdpG_Lz91DZsvKSITLaFQ-mcKCpVgYtP1qgvamfy6twa0li5l-1oMbiiQ";
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonceStr = random.ints(12, 0, CHARACTERS.length())
                .mapToObj(CHARACTERS::charAt)
                .map(Object::toString)
                .collect(Collectors.joining());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("jsapi_ticket=").append(ticket)
                .append("&noncestr").append(nonceStr)
                .append("&timestamp=").append(timestamp)
                .append("&url=").append("https://qweqwe.com");
        System.out.println(sha1(stringBuilder.toString()));
    }
}
