/
main.go
43 lines (40 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"fmt"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/sha3"
)
// Take an input string to be hashed and the type of hash library to use
func getHash(input string, hashType string) string {
// Decide which type of hashing library to use
switch hashType {
case "MD5":
return fmt.Sprintf("%x", md5.Sum([]byte(input)))
case "SHA256":
return fmt.Sprintf("%x", sha256.Sum256([]byte(input)))
case "SHA512":
return fmt.Sprintf("%x", sha512.Sum512([]byte(input)))
case "SHA3_512":
return fmt.Sprintf("%x", sha3.Sum512([]byte(input)))
// from "golang.org/x/crypto/blake2s"
case "BLAKE2s_256":
return fmt.Sprintf("%x", blake2s.Sum256([]byte(input)))
// from "golang.org/x/crypto/blake2b"
case "BLAKE2b_512":
return fmt.Sprintf("%x", blake2b.Sum512([]byte(input)))
default:
return fmt.Sprintf("%x", sha256.Sum256([]byte(input)))
}
}
func main() {
fmt.Println("MD5:", getHash("Hello World!", "MD5"))
fmt.Println("SHA256:", getHash("Hello World!", "SHA256"))
fmt.Println("SHA512:", getHash("Hello World!", "SHA512"))
fmt.Println("SHA3_512:", getHash("Hello World!", "SHA3_512"))
fmt.Println("BLAKE2s_256:", getHash("Hello World!", "BLAKE2s_256"))
fmt.Println("BLAKE2b_512:", getHash("Hello World!", "BLAKE2b_512"))
}