12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package com.ruoyi.nccloud.utils;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- public class SHA256Util {
- public static void main(String[] args) throws Exception {
- String client_id = "xfxt";
- String client_secret = "e85b3ca8e7c840fab912";
- String pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtnA9R1DTbqpTpfpSsBe/cVIp0P+59p8x5bzzPzylAKwF6txAqeTCnVP2LtRquHVlu6W0A77V2LuUJWovDluBiz+jmnPmQolXbhbBJscGBlAfeuaeRw38GtYy7VIc7zetZKcJ+TUPRLB/4Ic/RAAgUte0/N0nYsehAncsBPSQLh30GHNtMHtLvrJaVxtH+e0G5gezUYLp0EqL6ieQCgyoWbt4u9obeTbaX9ppj8oqG63+ZaOXQG1qInC+Q49ALQsfAT6EbxMg0yIdgwl/4wWf/Aj5yxLxLrsPqBwAGM7FVnXfYbPYsQ7k+nndbfEBOr2eLWncIr7f85zDPLB8THurfwIDAQAB";
- String secret_entryption = Encryption.pubEncrypt(pubKey, client_secret);
- System.err.println("secret_entryption::" + secret_entryption);
- System.err.println(URLEncoder.encode(secret_entryption, "utf-8"));
- String sign = SHA256Util.getSHA256(client_id + client_secret + pubKey, pubKey);
- System.err.println(sign);
- }
- public static String getSHA256(String str, String key) {
- //加盐
- byte[] salt = new byte[16];
- try {
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
- random.setSeed(key.getBytes());
- random.nextBytes(salt);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- String salt_string = Base64Util.encryptBASE64(salt);
- // System.out.println("salt_String::" + salt_string);
- return getSHA256(str + salt_string.replaceAll("\r|\n", ""));
- }
- private static String getSHA256(String str) {
- MessageDigest messageDigest;
- String encodestr = "";
- try {
- messageDigest = MessageDigest.getInstance("SHA-256");
- messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
- encodestr = byte2Hex(messageDigest.digest());
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- 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();
- }
- }
|