欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用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ù)某個字段進行分組操作

    這篇文章主要介紹了golang將切片或數(shù)組根據(jù)某個字段進行分組操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • go語言實現(xiàn)sqrt的方法

    go語言實現(xiàn)sqrt的方法

    這篇文章主要介紹了go語言實現(xiàn)sqrt的方法,實例分析了Go語言實現(xiàn)計算平方根的技巧,需要的朋友可以參考下
    2015-03-03
  • Go語言通過chan進行數(shù)據(jù)傳遞的方法詳解

    Go語言通過chan進行數(shù)據(jù)傳遞的方法詳解

    這篇文章主要為大家詳細介紹了Go語言如何通過chan進行數(shù)據(jù)傳遞的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • goland遠程調(diào)試k8s上容器的實現(xiàn)

    goland遠程調(diào)試k8s上容器的實現(xiàn)

    本文主要介紹了goland遠程調(diào)試k8s上容器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Go GORM版本2.0新特性介紹

    Go GORM版本2.0新特性介紹

    這篇文章主要為大家介紹了Go GORM版本2.0新特性的使用示例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Go擴展原語之SingleFlight的用法詳解

    Go擴展原語之SingleFlight的用法詳解

    Go語言擴展包同步原語singleflight.Group能夠再一個服務(wù)中抑制對下游的多次重復(fù)請求,它能夠限制對同一個鍵值對的多次重復(fù)請求,減少對下游的瞬時流量,接下來小編就給大家講講Go SingleFlight的具體用法,需要的朋友可以參考下
    2023-07-07
  • 淺談go語言renderer包代碼分析

    淺談go語言renderer包代碼分析

    本篇文章主要介紹了淺談go語言renderer包代碼分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Golang泛型的使用方法詳解

    Golang泛型的使用方法詳解

    這篇文章主要介紹了Golang中泛型的使用,Go和Python語言不同,處理不同數(shù)據(jù)類型非常嚴格。如Python可以定義函數(shù)帶兩個數(shù)值類型并返回較大的數(shù)值,但可以不嚴格指定參數(shù)類型為float或integer
    2022-12-12
  • Go基礎(chǔ)教程系列之WaitGroup用法實例詳解

    Go基礎(chǔ)教程系列之WaitGroup用法實例詳解

    這篇文章主要介紹了Go基礎(chǔ)教程系列之WaitGroup用法實例詳解,需要的朋友可以參考下
    2022-04-04
  • 高效封禁:利用Go封裝功能,提升封禁操作效率

    高效封禁:利用Go封裝功能,提升封禁操作效率

    在網(wǎng)絡(luò)安全領(lǐng)域,封禁操作是一項重要的任務(wù),用于阻止惡意行為和保護系統(tǒng)安全,而利用Go語言封裝功能可以提升封禁操作的效率,Go語言具有高效的并發(fā)性能和簡潔的語法,使得開發(fā)者可以快速構(gòu)建高性能的封禁系統(tǒng),
    2023-10-10

最新評論