资讯动态

哈希算法总结

发布时间:2026/10/14 3:30:53 来源:尧图企业网站定制
1.哈希算法的特点一种加密算法也称为“消息摘要算法”可以将任意长度或类型的内容计算成一个固定长度的哈希值不能根据哈希值计算出原值。2.哈希算法的用途主要用于“校验完整性”密码存储等。3.常见的哈希算法MD5输出哈希值长度为16个字节SHA-1输出长度是20个字节SHA-256输出长度是32个字节SHA-512输出的长度是64个字节。4.代码实现如下package com.cy.Day10_12; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class Demo238 { public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException { String hash1 HashUtil.getInstance().md5Hex(但使龙城飞将在); String hash2 HashUtil.getInstance().sha1Hex(但使龙城飞将在); String hash3 HashUtil.getInstance().sha256Hex(但使龙城飞将在); String hash4 HashUtil.getInstance().sha512Hex(但使龙城飞将在); String[] hash HashUtil.getInstance().hmacMd5Hex(但使龙城飞将在); String hash6 HashUtil.getInstance().hmacMd5Hex(但使龙城飞将在,hash[0]); System.out.println(hash1 hash1); System.out.println(hash2 hash2); System.out.println(hash3 hash3); System.out.println(hash4 hash4); System.out.println(hash密钥 hash[0]); System.out.println(hash密文 hash[1]); System.out.println(hash6 hash6); } } /** * 哈希工具类 * 支持MD5 / SHA1 / SHA2 / SHA3 / HMAC等主流算法 */ class HashUtil { //单例模式 private HashUtil() { } private static final HashUtil instancenew HashUtil(); public static HashUtil getInstance(){ return instance; } /** * 按照MD5算法进行哈希加密 * param data 原文 * return 密文(16进制编码结果) */ public String md5Hex(String data) throws NoSuchAlgorithmException { return hash(MD5,data); } public String sha1Hex(String data) throws NoSuchAlgorithmException { return hash(SHA-1,data); } public String sha256Hex(String data) throws NoSuchAlgorithmException { return hash(SHA-256,data); } public String sha512Hex(String data){ return hash(SHA-512,data); } private String hash(String algorithm,String data){ try { MessageDigest mdMessageDigest.getInstance(algorithm); md.update(data.getBytes());//将内容传进去 byte[] hashBytesmd.digest();//计算hash值 StringBuilder sb new StringBuilder(); for (byte b : hashBytes) { sb.append(String.format(%02x, b)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * 进行HMac加密 * param data 原文 * return 密文秘钥 */ public String[] hmacMd5Hex(String data) throws NoSuchAlgorithmException, InvalidKeyException { KeyGenerator keyGeneratorKeyGenerator.getInstance(HmacMD5); SecretKey secretKeykeyGenerator.generateKey();//生成密钥内容 Mac macMac.getInstance(HmacMD5); mac.init(secretKey); byte[] resultBytesmac.doFinal(data.getBytes());//计算哈希值 String keyBase64.getEncoder().encodeToString(secretKey.getEncoded()); String retBase64.getEncoder().encodeToString(resultBytes); return new String[]{key,ret}; } /** * 按照秘钥进行HMac加密 * param data * param secretKey * return */ public String hmacMd5Hex(String data, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException { byte[] keyBytesBase64.getDecoder().decode(secretKey); SecretKey key new SecretKeySpec(keyBytes, HmacMD5); Mac mac Mac.getInstance(HmacMD5); mac.init(key); byte[] dataBytes mac.doFinal(data.getBytes()); //使用Base64编码 return Base64.getEncoder().encodeToString(dataBytes); } }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑