1、代码
package cn.jiami; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; public class SHAUtils { protected static MessageDigest messageDigest = null; public static String getSHAString(String s) { try { messageDigest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } byte[] bytes = messageDigest.digest(s.getBytes()); return Hex.encodeHexString(bytes); } }
2、测试
package cn.jiami; import org.junit.*; import java.io.File; import java.io.FileInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UtilsTest { @Test public void shaEncrypt(){ SHAUtils shaUtils = new SHAUtils(); String msg = "数据"; String enmsg = shaUtils.getSHAString(msg); System.out.println(enmsg); } }
3、执行结果
标签:messageDigest,java,String,public,SHA,org,import,JAVA,加密 From: https://www.cnblogs.com/phoenixy/p/17435109.html