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 运行结果

猜你喜欢

转载自blog.csdn.net/qfguankefeng/article/details/89000900