This article describes the method of implementing SHA algorithm in Java. Share it for your reference, as follows:
A brief introduction
Secure hashing algorithm
Fixed length summary information
Two SHA algorithm
SHA-1, SHA-2 (SHA-224, SHA-256, SHA384, SHA-512)
Three SHA algorithm implementation
package com.imooc.security.sha;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.Security;import org.apache.commons.codec.binary.Hex;import org.apache.commons.codec.digest.DigestUtils;import org.bouncycastle.crypto.Digest;import org.bouncycastle.crypto.digests.SHA1Digest;import org.bouncycastle.crypto.digests.SHA224Digest;import org.bouncycastle.jce.provider.BouncyCastleProvider;public class ImoocSHA { private static String src = "cakin24 security sha"; public static void main(String[] args) { jdkSHA1(); bcSHA1(); bcSHA224(); ccSHA1(); } public static void jdkSHA1() { try { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(src.getBytes()); System.out.println("jdk sha-1 : " + Hex.encodeHexString(md.digest())); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public static void bcSHA1() { Digest digest = new SHA1Digest(); digest.update(src.getBytes(), 0, src.getBytes().length); byte[] sha1Bytes = new byte[digest.getDigestSize()]; digest.doFinal(sha1Bytes, 0); System.out.println("bc sha-1 : " + org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes)); } public static void bcSHA224() { Digest digest = new SHA224Digest(); digest.update(src.getBytes(), 0, src.getBytes().length); byte[] sha224Bytes = new byte[digest.getDigestSize()]; digest.doFinal(sha224Bytes, 0); System.out.println("bc sha-224 : " + org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes)); } public static void ccSHA1() { System.out.println("cc sha1 - 1 :" + DigestUtils.sha1Hex(src.getBytes())); System.out.println("cc sha1 - 2 :" + DigestUtils.sha1Hex(src)); } //384, 256, 512. . . . . . }Four run results
jdk sha-1: ba28a0f5f08efc8afaee4706ffd496e1f88befdb
bc sha-1: ba28a0f5f08efc8afaee4706ffd496e1f88befdb
bc sha-224: ae3b58439cd53d28455781fe6dc20c83a45d63e1d9550330c7911f58
cc sha1 - 1 :ba28a0f5f08efc8afaee4706ffd496e1f88befdb
cc sha1 - 2 :ba28a0f5f08efc8afaee4706ffd496e1f88befdb
Five SHA algorithm application
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Online SHA1 encryption tool:
http://tools.VeVB.COM/password/sha1encode
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.VeVB.COM/password/txt_encode
Online hash/hash algorithm encryption tool:
http://tools.VeVB.COM/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools.VeVB.COM/password/sha_encode
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operating DOM Node Skills" and "Summary of Java Array Operation Skills"
I hope this article will be helpful to everyone's Java programming.