Java 使用MD5、SHA-1、SHA-2 加密資料演算法的懶人包,請用DigestUtils

four bots

Java 使用MD5、SHA-1、SHA-2 加密資料演算法的懶人包,請用DigestUtils

四筒老師最近應用在Java 網路程式設計中,發現Apache Commons Codec 這個懶人包,內建DigestUtils 的類別,給80年代後的年輕人輕鬆處理密碼學的演算法,真的省去了解指導該如演算的架構,直接的拿來引用相當簡單方便,我是過來人要知道加密的演算法還自學密碼學,都把統計學拿出來計算,才把概念弄懂。有多簡單就以下說明,現在使用NET 開發者還是要自己寫演算法喔!所以說目前只有Java 這個程式語言的優勢。

使用MD5、SHA-1、SHA-2(SHA-256、SHA-384、SHA-512) 加密資料

對於MD5及SHA的加密方式,假如資深的開發者應該都不陌生,這兩種加密方式都是不可逆的,簡單來說:只能加密不能解密。
目前主要用途有二種:

  1. 敏感資料:舉例來說資料庫裡使用者的的密碼是非常敏感的資料,如果當資料庫被駭客竊取或者管理者監守自盜,很容易讓使用者的密碼就外流,因此如果透不可逆的加密方式,就算被竊取到加密過後的密碼,也無從的得知使用者的密碼是為何。
  2. 使用者密碼:因為如果利用加密的方式來比對密碼,即可以讓網站維護人員達到「比對密碼」卻「無法得知密碼得正確性」的效用。舉例來說當使用者註冊時時輸入密碼abcdefg此時我們將密碼hash過後變成esZsDxSN6VGbi9JkMSxNZA==並且存入DB(資料庫),爾後當使用者要進行登入網站,我們要比對密碼時,此時我們把使用者這次輸入的密碼進行hash,再比對看看DB裡之前hash過後的密碼是否一致即可。

使用者密碼增加補充說明:
用來比對使用者輸入密碼的。在使用者註冊或修改密碼時,將他的密碼經過Hash後存到DB,之後要比對登入時輸入的密碼,只需要把輸入值Hash後,再與DB讀出來的值比對,就能達到網站維護人員或資料庫管理人員無法得知使用者的密碼正確性,卻進行比對方式確認是否密碼正確。

但是隨著電腦運算能力力越來越強,MD5以及SHA-1目前都已經可以透過碰過攻擊的方式解密以及一些大量的清單可以做比對,詳細可參考MD5SHA 維基百科說明。

由 Matt_Crypto (talk) (Uploads) – original illustration for Wikipedia, created in Dia., 公有領域, https://commons.wikimedia.org/w/index.php?curid=214963
Figure 1. 一個MD5運算— 由類似的64次迴圈構成,分成4組16次。F 一個非線性函式;一個函式運算一次。Mi 表示一個 32-bits 的輸入資料,Ki 表示一個 32-bits 常數,用來完成每次不同的計算。
由 Matt_Crypto (talk) (Uploads) – original illustration for Wikipedia, created in Dia., 公有領域, https://commons.wikimedia.org/w/index.php?curid=214963

Download Apache Commons Codec 來源下載網址:https://commons.apache.org/proper/commons-codec/download_codec.cgi

NET 開發者可建議參考如下:
[C#] 使用MD5、SHA-1、SHA-2(SHA-256、SHA-384、SHA-512) 加密資料

MD5 及SHA 的加密方式Java 原始碼

package com.four.bots.tester;

import org.apache.commons.codec.digest.DigestUtils;

public class DigestUtilsTest {

	public static void main(String[] args) {
		String data = "four-bots";
		String key = null;
		String val = null;
		
		key = DigestUtils.md5Hex(data);
		val = "<MD5 value=\"" + key + "\"/>";
		System.out.println(val);
		
		key = DigestUtils.sha1Hex(data);
		val = "<SHA-1 value=\"" + key + "\"/>";
		System.out.println(val);
		
		key = DigestUtils.sha256Hex(data);
		val = "<SHA-2 value=\"" + key + "\"/>";
		System.out.println(val);
		
		key = DigestUtils.sha384Hex(data);
		val = "<SHA-3 value=\"" + key + "\"/>";
		System.out.println(val);
				
		key = DigestUtils.sha512Hex(data);
		val = "<SHA512 value=\"" + key + "\"/>";
		System.out.println(val);
	}
}

MD5 及SHA 的加密方式(顯示結果)

<MD5 value="25dbfc4f8a8dac6604972a5cea54b2c9"/>
<SHA-1 value="740e4f6bb2d5fd392d2fc7e8fed4e4fad15469f3"/>
<SHA-2 value="a4669dfa168b69b71eda629d6c47649770075a5c4b5d43d3f9ac26d6bb3c64d6"/>
<SHA-3 value="dd5c7814fb3293abe900779a1274bd29ea3919a3d7e3f7f4359a60ecf7dc3e8262c4368385e749699231a7c08691e2d8"/>
<SHA512 value="b8d4bdb625f83865ff620b5768ed45c285f836eb3d6e531f81c82a61cb0c48a69cebc1db2afb935be9dcc60d913fcd843d82dff8dfcdafe8697b2faf48e687b5"/>

API 項目說明

Modifier and TypeMethod and Description
byte[]digest(byte[] data)Reads through a byte array and returns the digest for the data.
byte[]digest(ByteBuffer data)Reads through a ByteBuffer and returns the digest for the data
byte[]digest(File data)Reads through a File and returns the digest for the data
byte[]digest(InputStream data)Reads through an InputStream and returns the digest for the data
static byte[]digest(MessageDigest messageDigest, byte[] data)Reads through a byte array and returns the digest for the data.
static byte[]digest(MessageDigest messageDigest, ByteBuffer data)Reads through a ByteBuffer and returns the digest for the data
static byte[]digest(MessageDigest messageDigest, File data)Reads through a File and returns the digest for the data
static byte[]digest(MessageDigest messageDigest, InputStream data)Reads through an InputStream and returns the digest for the data
static byte[]digest(MessageDigest messageDigest, Path data, OpenOption... options)Reads through a File and returns the digest for the data
static byte[]digest(MessageDigest messageDigest, RandomAccessFile data)Reads through a RandomAccessFile using non-blocking-io (NIO) and returns the digest for the data
byte[]digest(Path data, OpenOption... options)Reads through a File and returns the digest for the data
byte[]digest(String data)Reads through a byte array and returns the digest for the data.
StringdigestAsHex(byte[] data)Reads through a byte array and returns the digest for the data.
StringdigestAsHex(ByteBuffer data)Reads through a ByteBuffer and returns the digest for the data
StringdigestAsHex(File data)Reads through a File and returns the digest for the data
StringdigestAsHex(InputStream data)Reads through an InputStream and returns the digest for the data
StringdigestAsHex(Path data, OpenOption... options)Reads through a File and returns the digest for the data
StringdigestAsHex(String data)Reads through a byte array and returns the digest for the data.
static MessageDigestgetDigest(String algorithm)Returns a MessageDigest for the given algorithm.
static MessageDigestgetDigest(String algorithm, MessageDigest defaultMessageDigest)Returns a MessageDigest for the given algorithm or a default if there is a problem getting the algorithm.
static MessageDigestgetMd2Digest()Returns an MD2 MessageDigest.
static MessageDigestgetMd5Digest()Returns an MD5 MessageDigest.
MessageDigestgetMessageDigest()Returns the message digest instance.
static MessageDigestgetSha1Digest()Returns an SHA-1 digest.
static MessageDigestgetSha256Digest()Returns an SHA-256 digest.
static MessageDigestgetSha3_224Digest()Returns an SHA3-224 digest.
static MessageDigestgetSha3_256Digest()Returns an SHA3-256 digest.
static MessageDigestgetSha3_384Digest()Returns an SHA3-384 digest.
static MessageDigestgetSha3_512Digest()Returns an SHA3-512 digest.
static MessageDigestgetSha384Digest()Returns an SHA-384 digest.
static MessageDigestgetSha512_224Digest()Returns an SHA-512/224 digest.
static MessageDigestgetSha512_256Digest()Returns an SHA-512/256 digest.
static MessageDigestgetSha512Digest()Returns an SHA-512 digest.
static MessageDigestgetShaDigest()Deprecated. (1.11) Use getSha1Digest()
static booleanisAvailable(String messageDigestAlgorithm)Test whether the algorithm is supported.
static byte[]md2(byte[] data)Calculates the MD2 digest and returns the value as a 16 element byte[].
static byte[]md2(InputStream data)Calculates the MD2 digest and returns the value as a 16 element byte[].
static byte[]md2(String data)Calculates the MD2 digest and returns the value as a 16 element byte[].
static Stringmd2Hex(byte[] data)Calculates the MD2 digest and returns the value as a 32 character hex string.
static Stringmd2Hex(InputStream data)Calculates the MD2 digest and returns the value as a 32 character hex string.
static Stringmd2Hex(String data)Calculates the MD2 digest and returns the value as a 32 character hex string.
static byte[]md5(byte[] data)Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[]md5(InputStream data)Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[]md5(String data)Calculates the MD5 digest and returns the value as a 16 element byte[].
static Stringmd5Hex(byte[] data)Calculates the MD5 digest and returns the value as a 32 character hex string.
static Stringmd5Hex(InputStream data)Calculates the MD5 digest and returns the value as a 32 character hex string.
static Stringmd5Hex(String data)Calculates the MD5 digest and returns the value as a 32 character hex string.
static byte[]sha(byte[] data)Deprecated. (1.11) Use sha1(byte[])
static byte[]sha(InputStream data)Deprecated. (1.11) Use sha1(InputStream)
static byte[]sha(String data)Deprecated. (1.11) Use sha1(String)
static byte[]sha1(byte[] data)Calculates the SHA-1 digest and returns the value as a byte[].
static byte[]sha1(InputStream data)Calculates the SHA-1 digest and returns the value as a byte[].
static byte[]sha1(String data)Calculates the SHA-1 digest and returns the value as a byte[].
static Stringsha1Hex(byte[] data)Calculates the SHA-1 digest and returns the value as a hex string.
static Stringsha1Hex(InputStream data)Calculates the SHA-1 digest and returns the value as a hex string.
static Stringsha1Hex(String data)Calculates the SHA-1 digest and returns the value as a hex string.
static byte[]sha256(byte[] data)Calculates the SHA-256 digest and returns the value as a byte[].
static byte[]sha256(InputStream data)Calculates the SHA-256 digest and returns the value as a byte[].
static byte[]sha256(String data)Calculates the SHA-256 digest and returns the value as a byte[].
static Stringsha256Hex(byte[] data)Calculates the SHA-256 digest and returns the value as a hex string.
static Stringsha256Hex(InputStream data)Calculates the SHA-256 digest and returns the value as a hex string.
static Stringsha256Hex(String data)Calculates the SHA-256 digest and returns the value as a hex string.
static byte[]sha3_224(byte[] data)Calculates the SHA3-224 digest and returns the value as a byte[].
static byte[]sha3_224(InputStream data)Calculates the SHA3-224 digest and returns the value as a byte[].
static byte[]sha3_224(String data)Calculates the SHA3-224 digest and returns the value as a byte[].
static Stringsha3_224Hex(byte[] data)Calculates the SHA3-224 digest and returns the value as a hex string.
static Stringsha3_224Hex(InputStream data)Calculates the SHA3-224 digest and returns the value as a hex string.
static Stringsha3_224Hex(String data)Calculates the SHA3-224 digest and returns the value as a hex string.
static byte[]sha3_256(byte[] data)Calculates the SHA3-256 digest and returns the value as a byte[].
static byte[]sha3_256(InputStream data)Calculates the SHA3-256 digest and returns the value as a byte[].
static byte[]sha3_256(String data)Calculates the SHA3-256 digest and returns the value as a byte[].
static Stringsha3_256Hex(byte[] data)Calculates the SHA3-256 digest and returns the value as a hex string.
static Stringsha3_256Hex(InputStream data)Calculates the SHA3-256 digest and returns the value as a hex string.
static Stringsha3_256Hex(String data)Calculates the SHA3-256 digest and returns the value as a hex string.
static byte[]sha3_384(byte[] data)Calculates the SHA3-384 digest and returns the value as a byte[].
static byte[]sha3_384(InputStream data)Calculates the SHA3-384 digest and returns the value as a byte[].
static byte[]sha3_384(String data)Calculates the SHA3-384 digest and returns the value as a byte[].
static Stringsha3_384Hex(byte[] data)Calculates the SHA3-384 digest and returns the value as a hex string.
static Stringsha3_384Hex(InputStream data)Calculates the SHA3-384 digest and returns the value as a hex string.
static Stringsha3_384Hex(String data)Calculates the SHA3-384 digest and returns the value as a hex string.
static byte[]sha3_512(byte[] data)Calculates the SHA3-512 digest and returns the value as a byte[].
static byte[]sha3_512(InputStream data)Calculates the SHA3-512 digest and returns the value as a byte[].
static byte[]sha3_512(String data)Calculates the SHA3-512 digest and returns the value as a byte[].
static Stringsha3_512Hex(byte[] data)Calculates the SHA3-512 digest and returns the value as a hex string.
static Stringsha3_512Hex(InputStream data)Calculates the SHA3-512 digest and returns the value as a hex string.
static Stringsha3_512Hex(String data)Calculates the SHA3-512 digest and returns the value as a hex string.
static byte[]sha384(byte[] data)Calculates the SHA-384 digest and returns the value as a byte[].
static byte[]sha384(InputStream data)Calculates the SHA-384 digest and returns the value as a byte[].
static byte[]sha384(String data)Calculates the SHA-384 digest and returns the value as a byte[].
static Stringsha384Hex(byte[] data)Calculates the SHA-384 digest and returns the value as a hex string.
static Stringsha384Hex(InputStream data)Calculates the SHA-384 digest and returns the value as a hex string.
static Stringsha384Hex(String data)Calculates the SHA-384 digest and returns the value as a hex string.
static byte[]sha512_224(byte[] data)Calculates the SHA-512/224 digest and returns the value as a byte[].
static byte[]sha512_224(InputStream data)Calculates the SHA-512/224 digest and returns the value as a byte[].
static byte[]sha512_224(String data)Calculates the SHA-512/224 digest and returns the value as a byte[].
static Stringsha512_224Hex(byte[] data)Calculates the SHA-512/224 digest and returns the value as a hex string.
static Stringsha512_224Hex(InputStream data)Calculates the SHA-512/224 digest and returns the value as a hex string.
static Stringsha512_224Hex(String data)Calculates the SHA-512/224 digest and returns the value as a hex string.
static byte[]sha512_256(byte[] data)Calculates the SHA-512/256 digest and returns the value as a byte[].
static byte[]sha512_256(InputStream data)Calculates the SHA-512/256 digest and returns the value as a byte[].
static byte[]sha512_256(String data)Calculates the SHA-512/256 digest and returns the value as a byte[].
static Stringsha512_256Hex(byte[] data)Calculates the SHA-512/256 digest and returns the value as a hex string.
static Stringsha512_256Hex(InputStream data)Calculates the SHA-512/256 digest and returns the value as a hex string.
static Stringsha512_256Hex(String data)Calculates the SHA-512/256 digest and returns the value as a hex string.
static byte[]sha512(byte[] data)Calculates the SHA-512 digest and returns the value as a byte[].
static byte[]sha512(InputStream data)Calculates the SHA-512 digest and returns the value as a byte[].
static byte[]sha512(String data)Calculates the SHA-512 digest and returns the value as a byte[].
static Stringsha512Hex(byte[] data)Calculates the SHA-512 digest and returns the value as a hex string.
static Stringsha512Hex(InputStream data)Calculates the SHA-512 digest and returns the value as a hex string.
static Stringsha512Hex(String data)Calculates the SHA-512 digest and returns the value as a hex string.
static StringshaHex(byte[] data)Deprecated. (1.11) Use sha1Hex(byte[])
static StringshaHex(InputStream data)Deprecated. (1.11) Use sha1Hex(InputStream)
static StringshaHex(String data)Deprecated. (1.11) Use sha1Hex(String)
static MessageDigestupdateDigest(MessageDigest messageDigest, byte[] valueToDigest)Updates the given MessageDigest.
static MessageDigestupdateDigest(MessageDigest messageDigest, ByteBuffer valueToDigest)Updates the given MessageDigest.
static MessageDigestupdateDigest(MessageDigest digest, File data)Reads through a File and updates the digest for the data
static MessageDigestupdateDigest(MessageDigest digest, InputStream inputStream)Reads through an InputStream and updates the digest for the data
static MessageDigestupdateDigest(MessageDigest digest, Path path, OpenOption... options)Reads through a Path and updates the digest for the data
static MessageDigestupdateDigest(MessageDigest digest, RandomAccessFile data)Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO)
static MessageDigestupdateDigest(MessageDigest messageDigest, String valueToDigest)Updates the given MessageDigest from a String (converted to bytes using UTF-8).