最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用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通道類型

    淺談golang通道類型

    本文主要介紹了淺談golang通道類型,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • golang中defer執(zhí)行時機的案例分析

    golang中defer執(zhí)行時機的案例分析

    這篇文章主要來通過一些案例和大家一起探討一下golang中defer的執(zhí)行時機,文中的示例代碼講解詳細,對我們深入了解golang有一定的幫助,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • Go語言zip文件的讀寫操作

    Go語言zip文件的讀寫操作

    本文主要介紹了Go語言zip文件的讀寫操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • golang時間/時間戳的獲取與轉(zhuǎn)換實例代碼

    golang時間/時間戳的獲取與轉(zhuǎn)換實例代碼

    說實話,golang的時間轉(zhuǎn)化還是很麻煩的,最起碼比php麻煩很多,下面這篇文章主要給大家介紹了關(guān)于golang時間/時間戳的獲取與轉(zhuǎn)換的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • go語言實戰(zhàn)之實現(xiàn)比特幣地址校驗步驟

    go語言實戰(zhàn)之實現(xiàn)比特幣地址校驗步驟

    這篇文章主要介紹了go語言實戰(zhàn)之實現(xiàn)比特幣地址校驗步驟,利用生產(chǎn)的隨機數(shù)采用橢圓加密算法生成公鑰,具體步驟實例代碼請參考下本文
    2021-05-05
  • golang中使用aes加密的操作方法

    golang中使用aes加密的操作方法

    AES是一種對稱加密算法,適用于加密敏感數(shù)據(jù),通過上述方法,可在 Golang中實現(xiàn)AES加密/解密功能,本文給大家介紹golang中使用aes加密的操作方法,感興趣的朋友跟隨小編一起看看吧
    2025-10-10
  • Golang實現(xiàn)拓撲排序(DFS算法版)

    Golang實現(xiàn)拓撲排序(DFS算法版)

    這篇文章主要介紹了Golang實現(xiàn)拓撲排序(DFS算法版),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • go語言if/else語句簡單用法示例

    go語言if/else語句簡單用法示例

    這篇文章主要介紹了go語言if/else語句用法,結(jié)合實例形式分析了go語言if else語句的判定與流程控制技巧,需要的朋友可以參考下
    2016-05-05
  • Golang加權(quán)輪詢負載均衡的實現(xiàn)

    Golang加權(quán)輪詢負載均衡的實現(xiàn)

    負載均衡器在向后端服務(wù)分發(fā)流量負載時可以使用幾種策略。本文主要介紹了Golang加權(quán)輪詢負載均衡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • golang gc的內(nèi)部優(yōu)化詳細介紹

    golang gc的內(nèi)部優(yōu)化詳細介紹

    Go編譯器在垃圾回收(GC)的掃描標(biāo)記階段,對存儲無指針鍵值對的map進行了優(yōu)化,即在GC掃描時不深入掃描map內(nèi)部數(shù)據(jù),只檢查map本身是否需要回收,這一優(yōu)化顯著提升了GC掃描的速度,從而減少了GC對程序性能的影響
    2024-10-10

最新評論

方正县| 察隅县| 章丘市| 南阳市| 昌吉市| 峨山| 安图县| 昌吉市| 德阳市| 黑水县| 武宁县| 秦安县| 都匀市| 石首市| 麻江县| 任丘市| 安塞县| 孟州市| 多伦县| 汉川市| 万宁市| 三穗县| 曲麻莱县| 美姑县| 连平县| 平谷区| 油尖旺区| 社旗县| 郯城县| 枞阳县| 凉城县| 和静县| 利辛县| 江城| 榆中县| 绥阳县| 句容市| 美姑县| 上虞市| 新丰县| 布尔津县|