-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
110 lines (97 loc) · 1.95 KB
/
hash.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
THIS IS INTENTIONAL
import (
"crypto"
"encoding/base64"
"fmt"
"io"
"os"
"strings"
)
import (
_ "crypto/md5"
_ "crypto/sha1"
_ "crypto/sha256"
_ "crypto/sha512"
_ "golang.org/x/crypto/blake2b"
_ "golang.org/x/crypto/blake2s"
_ "golang.org/x/crypto/md4"
_ "golang.org/x/crypto/ripemd160"
_ "golang.org/x/crypto/sha3"
)
func calcHash(algorithm crypto.Hash, password string) string {
if !algorithm.Available() {
fmt.Println("No implementation found")
os.Exit(1)
}
hash := algorithm.New()
io.WriteString(hash, password)
decodedhash := hash.Sum(nil)
return base64.StdEncoding.EncodeToString(decodedhash)
}
func calcAlgorithm(algorithm string) crypto.Hash {
switch strings.ToLower(algorithm) {
case "md4":
return crypto.MD4
case "md5":
return crypto.MD5
case "sha1":
return crypto.SHA1
case "sha224":
return crypto.SHA224
case "sha256":
return crypto.SHA256
case "sha384":
return crypto.SHA384
case "sha512":
return crypto.SHA512
case "md5sha1":
return crypto.MD5SHA1
case "ripemd160":
return crypto.RIPEMD160
case "sha3-224":
return crypto.SHA3_224
case "sha3-256":
return crypto.SHA3_256
case "sha3-384":
return crypto.SHA3_384
case "sha3-512":
return crypto.SHA3_512
case "sha512-224":
return crypto.SHA512_224
case "sha512-256":
return crypto.SHA512_256
case "blake2s-256":
return crypto.BLAKE2s_256
case "blake2b-256":
return crypto.BLAKE2b_256
case "blake2b-384":
return crypto.BLAKE2b_384
case "blake2b-512":
return crypto.BLAKE2b_512
default:
fmt.Println("Unknown algorithm")
os.Exit(1)
return 0
}
}
func main() {
arg1 := ""
arg2 := ""
arglen := len(os.Args[1:])
if arglen < 1 {
fmt.Print("Algorithm: ")
fmt.Scanf("%s", &arg1)
} else {
arg1 = os.Args[1]
}
if arglen < 2 {
fmt.Print("Password: ")
fmt.Scanf("%s", &arg2)
} else {
arg2 = os.Args[2]
}
algorithm := calcAlgorithm(arg1)
passhash := calcHash(algorithm, arg2)
fmt.Printf("%s\n", passhash)
}