package com.qmth.sop.common.util;


import com.qmth.sop.common.contant.SystemConstant;

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

public class ShaUtils {

    public static byte[] sha1(String input) {
        try {
            return MessageDigest.getInstance("SHA1").digest(input.getBytes(SystemConstant.CHARSET));
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] sha256(String input) {
        try {
            return MessageDigest.getInstance("SHA256").digest(input.getBytes(SystemConstant.CHARSET));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 加密
     *
     * @param str
     * @return
     * @author
     */
    public static byte[] encode(String str) {
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        messageDigest.update(str.getBytes());
        return messageDigest.digest();
    }
}
