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?運行結果