package com.ruoyi.common.utils; import java.security.MessageDigest; public final class ShaUtil { public static String sha512(String plaintext) { return sha(plaintext, "SHA-512"); } public static String sha256(String plaintext) { return sha(plaintext, "SHA-256"); } private static String sha(String plaintext, String shaAlg) { if (plaintext == null || plaintext.length() == 0) { throw new IllegalArgumentException("Plain text must be not empty!!"); } try { MessageDigest md = MessageDigest.getInstance(shaAlg); md.update(plaintext.getBytes()); byte[] bytBuffer = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : bytBuffer) { String hex = Integer.toHexString(255 & b); if (hex.length() == 1) { sb.append("0"); } sb.append(hex); } return sb.toString(); } catch (Exception e) { throw new IllegalStateException("do sha failed", e); } } }