sha256 vue前端加密 和 java代码sha256加密

一.vue sha256加密

1.npm安装sha256库

执行npm install js-sha256 进行安装

2.安装成功后,去项目配置文件package.json中查看

3.在模块中引入sha256

const sha256 = require("js-sha256").sha256; //引入sha256库 和import引入方式一样

 

sha256 vue前端加密 和 java代码sha256加密_第1张图片

 

二.java sha256加密

public class SHA256Util {

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

    public static String getSHA256StrJava(String str) {

        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}

 

你可能感兴趣的:(java)