使用Go語言實現(xiàn)常見hash算法
更新時間:2024年01月14日 09:05:03 作者:242030
這篇文章主要為大家詳細介紹了使語言實現(xiàn)各種常見hash算法的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以參考下
Go語言實現(xiàn)各種hash算法
1、各種hash算法的實現(xiàn)
package main import ( "crypto" "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" "encoding/hex" "fmt" "hash" "golang.org/x/crypto/md4" "golang.org/x/crypto/ripemd160" ) func main() { str1 := HashByType("Hello World", "md4", false) fmt.Println(str1) str2 := HashByCrypto("Hello World", crypto.MD4, false) fmt.Println(str2) } // 根據(jù)不同哈希類型進行哈希: md4、md5、sha1、ripemd160、sha256、sha512 func HashByType(text string, hashType string, isHex bool) string { var hashInstance hash.Hash // 定義哈希實例 switch hashType { // 選擇哈希類型 case "md4": hashInstance = md4.New() // "golang.org/x/crypto/md4" case "md5": hashInstance = md5.New() case "sha1": hashInstance = sha1.New() case "ripemd160": hashInstance = ripemd160.New() // "golang.org/x/crypto/ripemd160" case "sha256": hashInstance = sha256.New() case "sha512": hashInstance = sha512.New() } if isHex { arr, _ := hex.DecodeString(text) // 十六進制字符串轉(zhuǎn)為十六進制字節(jié)數(shù)組 hashInstance.Write(arr) // 寫入哈希實例對象 } else { hashInstance.Write([]byte(text)) // 將字符串轉(zhuǎn)換為字節(jié)數(shù)組,寫入哈希對象 } bytes := hashInstance.Sum(nil) // 哈希值追加到參數(shù)后面,只獲取原始值,不用追加,用nil,返回哈希值字節(jié)數(shù)組 return fmt.Sprintf("%x", bytes) // 格式化輸出哈希值 } // 根據(jù)不同哈希類型進行哈希: md4、md5、sha1、ripemd160、sha256、sha512 func HashByCrypto(text string, myhash crypto.Hash, isHex bool) string { hashInstance := myhash.New() // 定義哈希實例 if isHex { arr, _ := hex.DecodeString(text) // 十六進制字符串轉(zhuǎn)為十六進制字節(jié)數(shù)組 hashInstance.Write(arr) // 寫入哈希實例對象 } else { hashInstance.Write([]byte(text)) // 將字符串轉(zhuǎn)換為字節(jié)數(shù)組,寫入哈希對象 } bytes := hashInstance.Sum(nil) // 哈希值追加到參數(shù)后面,只獲取原始值,不用追加,用nil,返回哈希值字節(jié)數(shù)組 return fmt.Sprintf("%x", bytes) // 格式化輸出哈希值 }
支持的Hash算法有:
const ( MD4 Hash = 1 + iota // import golang.org/x/crypto/md4 MD5 // import crypto/md5 SHA1 // import crypto/sha1 SHA224 // import crypto/sha256 SHA256 // import crypto/sha256 SHA384 // import crypto/sha512 SHA512 // import crypto/sha512 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA RIPEMD160 // import golang.org/x/crypto/ripemd160 SHA3_224 // import golang.org/x/crypto/sha3 SHA3_256 // import golang.org/x/crypto/sha3 SHA3_384 // import golang.org/x/crypto/sha3 SHA3_512 // import golang.org/x/crypto/sha3 SHA512_224 // import crypto/sha512 SHA512_256 // import crypto/sha512 BLAKE2s_256 // import golang.org/x/crypto/blake2s BLAKE2b_256 // import golang.org/x/crypto/blake2b BLAKE2b_384 // import golang.org/x/crypto/blake2b BLAKE2b_512 // import golang.org/x/crypto/blake2b maxHash )
2、兩次hash
- 有連續(xù)兩次哈希需求時,為了不重復(fù)創(chuàng)建哈希對象,造成內(nèi)存的浪費,可以單獨封裝函數(shù)。
- 哈希完成一次后,使用以下語句,清除哈希對象內(nèi)容,然后進行第二次哈希。
hashInstance.Reset() // 重置哈希實例
完整代碼:
package main import ( "crypto" "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" "encoding/hex" "fmt" "hash" "golang.org/x/crypto/md4" "golang.org/x/crypto/ripemd160" ) func main() { str1 := HashByType("Hello World", "md4", false) fmt.Println(str1) str2 := HashByCrypto("Hello World", crypto.MD4, false) fmt.Println(str2) str3 := DoubleHashByCrypto("Hello World", crypto.MD4, false) fmt.Println(str3) } // 根據(jù)不同哈希類型進行哈希: md4、md5、sha1、ripemd160、sha256、sha512 func HashByType(text string, hashType string, isHex bool) string { var hashInstance hash.Hash // 定義哈希實例 switch hashType { // 選擇哈希類型 case "md4": hashInstance = md4.New() // "golang.org/x/crypto/md4" case "md5": hashInstance = md5.New() case "sha1": hashInstance = sha1.New() case "ripemd160": hashInstance = ripemd160.New() // "golang.org/x/crypto/ripemd160" case "sha256": hashInstance = sha256.New() case "sha512": hashInstance = sha512.New() } if isHex { arr, _ := hex.DecodeString(text) // 十六進制字符串轉(zhuǎn)為十六進制字節(jié)數(shù)組 hashInstance.Write(arr) // 寫入哈希實例對象 } else { hashInstance.Write([]byte(text)) // 將字符串轉(zhuǎn)換為字節(jié)數(shù)組,寫入哈希對象 } bytes := hashInstance.Sum(nil) // 哈希值追加到參數(shù)后面,只獲取原始值,不用追加,用nil,返回哈希值字節(jié)數(shù)組 return fmt.Sprintf("%x", bytes) // 格式化輸出哈希值 } // 根據(jù)不同哈希類型進行哈希: md4、md5、sha1、ripemd160、sha256、sha512 func HashByCrypto(text string, myhash crypto.Hash, isHex bool) string { hashInstance := myhash.New() // 定義哈希實例 if isHex { arr, _ := hex.DecodeString(text) // 十六進制字符串轉(zhuǎn)為十六進制字節(jié)數(shù)組 hashInstance.Write(arr) // 寫入哈希實例對象 } else { hashInstance.Write([]byte(text)) // 將字符串轉(zhuǎn)換為字節(jié)數(shù)組,寫入哈希對象 } bytes := hashInstance.Sum(nil) // 哈希值追加到參數(shù)后面,只獲取原始值,不用追加,用nil,返回哈希值字節(jié)數(shù)組 return fmt.Sprintf("%x", bytes) // 格式化輸出哈希值 } // 根據(jù)不同哈希類型進行哈希: md4、md5、sha1、ripemd160、sha256、sha512 // 兩次哈希256后的字節(jié)數(shù)組,第二次是將第一次哈希后的16進制進行哈希 func DoubleHashByCrypto(text string, myhash crypto.Hash, isHex bool) string { hashInstance := myhash.New() // 定義哈希實例 if isHex { arr, _ := hex.DecodeString(text) // 十六進制字符串轉(zhuǎn)為十六進制字節(jié)數(shù)組 hashInstance.Write(arr) // 寫入哈希實例對象 } else { hashInstance.Write([]byte(text)) // 將字符串轉(zhuǎn)換為字節(jié)數(shù)組,寫入哈希對象 } bytes := hashInstance.Sum(nil) // 哈希值追加到參數(shù)后面,只獲取原始值,不用追加,用nil,返回哈希值字節(jié)數(shù)組 hashInstance.Reset() hashInstance.Write(bytes) bytes = hashInstance.Sum(nil) return fmt.Sprintf("%x", bytes) // 格式化輸出哈希值 }
輸出
77a781b995cf1cfaf39d9e2f5910c2cf
77a781b995cf1cfaf39d9e2f5910c2cf
487b5e8e42dcd5b4cc218c2e275561fb
到此這篇關(guān)于使用Go語言實現(xiàn)常見hash算法的文章就介紹到這了,更多相關(guān)Go hash算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
golang將切片或數(shù)組根據(jù)某個字段進行分組操作
這篇文章主要介紹了golang將切片或數(shù)組根據(jù)某個字段進行分組操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Go語言通過chan進行數(shù)據(jù)傳遞的方法詳解
這篇文章主要為大家詳細介紹了Go語言如何通過chan進行數(shù)據(jù)傳遞的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06goland遠程調(diào)試k8s上容器的實現(xiàn)
本文主要介紹了goland遠程調(diào)試k8s上容器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02