选择Java密码算法第1部分-散列
抽象
這是涵蓋Java加密算法的三部分博客系列的第1部分。 本系列介紹如何實現以下目標:
這第一篇文章詳細介紹了如何實現SHA–512哈希。 讓我們開始吧。
免責聲明
這篇文章僅供參考。 在使用所提供的任何信息之前,請認真思考。 從中學到東西,但最終自己做出決定,風險自負。
要求
我使用以下主要技術完成了本文的所有工作。 您可能可以使用不同的技術或版本來做相同的事情,但不能保證。
- Java 1.8.0_152_x64
- NetBeans 8.2(內部版本201609300101)
- Maven 3.0.5(與NetBeans捆綁在一起)
下載
訪問我的GitHub頁面以查看我所有的開源項目。 這篇文章的代碼位于項目中: thoth-cryptography
散列
關于
散列是一種單向密碼算法,它接收任意長度的消息,并輸出該消息的可重復,固定長度和單向摘要(哈希)。 作為單向方式,應該無法從哈希值中重新生成原始消息。 相同的消息將始終生成相同的哈希。
哈希可以用于驗證原始消息。 哈希的一種常見用法是驗證密碼。 而不是存儲密碼本身,而是存儲密碼的哈希。 為了驗證密碼,在登錄過程中將存儲的哈希與輸入密碼的新哈希進行比較。
由于相同的消息會生成相同的哈希,因此將使用salt值使哈希更安全(Salt,2017,第1段)。 考慮多個用戶使用相同密碼的情況。 鹽值與原始密碼結合使用可實現唯一的哈希值。 這很重要,因為如果散列值曾經遭到破壞,則相同的哈希值會讓黑客知道那些密碼是相同的。
SHA–512
 截至今天進行的研究似乎表明,哈希算法的最佳和最安全算法是SHA–512,它使用64位字(Secure Hash Algorithms,2017,第2段)。 讓我們看一個例子。 
 
 注意請勿將MD5用作安全哈希。 它具有許多漏洞(MD5,2017年,第1段)。 將MD5的使用限制為校驗和和數據驗證。 
 
例
清單1是ShaTest.java單元測試,演示了如何哈希。 清單2是執行哈希的Sha.java類。
清單1 – ShaTest.java類
package org.thoth.security.hash;import java.util.Optional; import org.junit.Assert; import org.junit.Test;/*** @author Michael Remijan mjremijan@yahoo.com @mjremijan*/ public class ShaTest {@Testpublic void test_hash_with_optional_to_hex() throws Exception {// setupString username = "mjremijan";String password = "super!secret";Sha sha = new Sha();// testString asHex= sha.hashToHex(password, Optional.of(username));// assertAssert.assertEquals("F38CD5290D11B20159E36740843A8D93CFDFA395CF594F328613EF5C7BA42D9EAC00BF3EE47B7E8CE1587040B36365F05C8E15E9392C288A1D7C4CFB66097848", asHex);}@Testpublic void test_hash_without_optional_to_hex() throws Exception {// setupString password = "super!secret";Sha sha = new Sha();// testString asHex= sha.hashToHex(password, Optional.empty());// assertAssert.assertEquals("516A1FE9D87FE5B953D91B48B1A2FFA5AE5F670914C1B6FE0835D8877918DC4E8BC8FB8CCD520DBA940C21B4F294DFD1B4EFF2E06AB110C6A06E35068251C1DD", asHex);}@Testpublic void test_hash_with_optional_to_base64() throws Exception {// setupString username = "mjremijan";String password = "super!secret";Sha sha = new Sha();// testString asBase64= sha.hashToBase64(password, Optional.of(username));// assertAssert.assertEquals("84ZVKQ0RSGFZ42DAHDQNK8/FO5XPWU8YHHPVXHUKLZ6SAL8+5HT+JOFYCECZY2XWXI4V6TKSKIODFEZ7ZGL4SA==", asBase64);}@Testpublic void test_hash_without_optional_to_base64() throws Exception {// setupString password = "super!secret";Sha sha = new Sha();// testString asBase64= sha.hashToBase64(password, Optional.empty());// assertAssert.assertEquals("UWOF6DH/5BLT2RTISAL/PA5FZWKUWBB+CDXYH3KY3E6LYPUMZVINUPQMIBTYLN/RTO/Y4GQXEMAGBJUGGLHB3Q==", asBase64);} }清單2 – Sha.java類
package org.thoth.security.hash;import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Optional;/*** @author Michael Remijan mjremijan@yahoo.com @mjremijan*/ public class Sha {public String hashToHex(String hashMe, Optional<String> salt)throws NoSuchAlgorithmException, UnsupportedEncodingException {byte[] bytes= hash(hashMe, salt);StringBuilder sp= new StringBuilder();for (int i = 0; i < bytes.length; i++) {sp.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));}return sp.toString().toUpperCase();}public String hashToBase64(String hashMe, Optional<String> salt)throws NoSuchAlgorithmException, UnsupportedEncodingException {return Base64.getEncoder().encodeToString(hash(hashMe, salt)).toUpperCase();}public byte[] hash(String hashMe, Optional<String> salt)throws NoSuchAlgorithmException, UnsupportedEncodingException {MessageDigest md= MessageDigest.getInstance("SHA-512");md.update(hashMe.getBytes("UTF-8"));salt.ifPresent(s -> {try { md.update(s.getBytes("UTF-8")); } catch (Exception e) {throw new RuntimeException(e);}});return md.digest();} }摘要
哈希很容易。 選擇一種強大的哈希算法(例如SHA–512)來保護您的應用程序數據。 避免使用MD5來保護數據。 及時了解哪些算法強大且安全。 如果您使用的是較舊的算法存在漏洞或受到威脅,請更新您的應用程序。
參考文獻
鹽(加密)。 (2017年11月3日)。 維基百科。 取自https://en.wikipedia.org/wiki/Salt_(cryptography) 。
安全哈希算法。 (2017年11月25日)。 維基百科。 取自https://en.wikipedia.org/wiki/Secure_Hash_Algorithms 。
MD5。 (2017年11月22日)。 維基百科。 取自https://en.wikipedia.org/wiki/MD5 。
翻譯自: https://www.javacodegeeks.com/2017/12/choosing-java-cryptographic-algorithms-part-1-hashing.html
總結
以上是生活随笔為你收集整理的选择Java密码算法第1部分-散列的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 使用Apache Ignite优化Spa
- 下一篇: 化妆品出口备案需要哪些资料(化妆品出口备
