Go從入門到精通系列視頻之go編程語言密碼學哈希算法(二)

2.1哈希算法的代碼實現

2.1.1核心代碼

下面列舉一些基礎工具函數,如例13-1所示。

例1-1?基礎工具函數

1?package main

2?import (

3? "encoding/hex"

4? "fmt"

5?)

6?func main(){

7? arr := []byte{'1', '0', '0', '0','p', 'h', 'o' ,'n', 'e'}

8? fmt.Println(string(arr))

9? str :=BytesToHexString(arr)

10? fmt.Println(str)

11? str = ReverseHexString(str)

12? arr,_ = HexStringToBytes(str)

13? fmt.Printf("%x\n", arr)

14? ReverseBytes(arr)

15? fmt.Println(string(arr))

16?}

17?/**

18??*?將字節數組轉成16進制字符串:?[]byte -> string

19??*/

20?func BytesToHexString(arr []byte) string {

21????return hex.EncodeToString(arr)

22?}

23?/**

24??*?將16進制字符串轉成字節數組:?hex string -> ?[]byte

25??*/

26?func HexStringToBytes(s string) ([]byte, error) {

27????arr, err := hex.DecodeString(s)

28????return arr, err

29?}

30?/**

31??* 16進制字符串大端和小端顛倒

32??*/

33?func ReverseHexString(hexStr string) string {

34????arr, _ := hex.DecodeString(hexStr)

35????ReverseBytes(arr)

36????return hex.EncodeToString(arr)

37?}

38?/**

39??*?字節數組大端和小端顛倒

40??*/

41?func ReverseBytes(data []byte) {

42????for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {

43???????data[i], data[j] = data[j], data[i]

44????}

45?}

運行結果如圖所示。

圖1.1?運行結果

?

下面列舉一些哈希函數的使用,如例13-2所示。

例1-2?哈希函數

1?package main

2?import (

3? "crypto/md5"

4? "crypto/sha1"

5? "crypto/sha256"

6? "crypto/sha512"

7? "encoding/hex"

8? "fmt"

9? "hash"

10?)

11?func main(){

12? str := "1000phone"

13? fmt.Println(str)

14? str1 := HASH(str, "md5", false)

15? fmt.Println(str1)

16? str2 := HASH(str, "sha1", false)

17? fmt.Println(str2)

18? str3 := HASH(str, "sha256", false)

19? fmt.Println(str3)

20? arr := SHA256Double(str, false)

21? fmt.Println(string(arr))

22? str4 := SHA256DoubleString(str, false)

23? fmt.Println(str4)

24?}

25?func HASH(text string, hashType string, isHex bool) string {

26? var hashInstance hash.Hash

27? switch hashType {

28? case "md5":

29? hashInstance = md5.New()

30? case "sha1":

31? hashInstance = sha1.New()

32? case "sha256":

33? hashInstance = sha256.New()

34? case "sha512":

35? hashInstance = sha512.New()

36? }

37? if isHex {

38? arr , _ := hex.DecodeString(text)

39? hashInstance.Write(arr)

40? } else {

41? hashInstance.Write([]byte(text))

42? }

43? cipherBytes := hashInstance.Sum(nil)

44? return fmt.Sprintf("%x" , cipherBytes)

45?}

46?

47?func SHA256Double(text string, isHex bool) []byte {

48? hashInstance := sha256.New()

49? if isHex {

50? arr , _ := hex.DecodeString(text)

51? hashInstance.Write(arr)

52? } else {

53? hashInstance.Write([]byte(text))

54? }

55? cipherBytes := hashInstance.Sum(nil)

56? hashInstance.Reset()

57? hashInstance.Write(cipherBytes)

58? cipherBytes = hashInstance.Sum(nil)

59? return cipherBytes

60?}

61?

62?

63?func SHA256DoubleString(text string, isHex bool) string {

64? hashInstance := sha256.New()

65? if isHex {

66? arr , _ := hex.DecodeString(text)

67? hashInstance.Write(arr)

68? } else {

69? hashInstance.Write([]byte(text))

70? }

71? cipherBytes := hashInstance.Sum(nil)

72? hashInstance.Reset()

73? hashInstance.Write(cipherBytes)

74? cipherBytes = hashInstance.Sum(nil)

75? return fmt.Sprintf("%x" , cipherBytes)

76?}

運行結果如圖所示。

圖1.2?運行結果

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,504評論 0 13
  • 這里先簡單介紹單向散列函數、消息摘要和哈希碰撞的的概念 單向散列函數: 將任意長度的信息轉換為較短的固定長度的值,...
    坤_7a1e閱讀 3,543評論 0 0
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    開心的鑼鼓閱讀 3,345評論 0 9
  • 一、數據類型轉換 https://studygolang.com/articles/10838 package m...
    蓓蓓的萬能男友閱讀 1,106評論 0 1
  • 電影《七十七天》,我覺得可以給它打三顆星。 不得不說七十七天拍攝的景很美,看的時候就好像置身其中,而且大部分景是實...
    _阿黎__閱讀 615評論 0 0