-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
172 lines (137 loc) · 4.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright © 2025 Seednode <seednode@seedno.de>
*/
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/julienschmidt/httprouter"
)
var (
ErrInvalidHashAlgorithm = errors.New("invalid hash algorithm provided")
)
type HashAlgorithm string
const (
MD5 HashAlgorithm = "MD5"
SHA1 HashAlgorithm = "SHA-1"
SHA224 HashAlgorithm = "SHA-224"
SHA256 HashAlgorithm = "SHA-256"
SHA384 HashAlgorithm = "SHA-384"
SHA512 HashAlgorithm = "SHA-512"
SHA512_224 HashAlgorithm = "SHA-512/224"
SHA512_256 HashAlgorithm = "SHA-512/256"
)
func serveHash(algorithm HashAlgorithm, errorChannel chan<- Error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
securityHeaders(w)
value := ""
switch r.Method {
case http.MethodGet:
value = strings.TrimPrefix(p.ByName("string"), "/")
case http.MethodPost:
body, err := io.ReadAll(r.Body)
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("Failed to hash string\n"))
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
}
return
}
value = string(body)
}
var h hash.Hash
switch algorithm {
case MD5:
h = md5.New()
case SHA1:
h = sha1.New()
case SHA224:
h = sha256.New224()
case SHA256:
h = sha256.New()
case SHA384:
h = sha512.New384()
case SHA512:
h = sha512.New()
case SHA512_224:
h = sha512.New512_224()
case SHA512_256:
h = sha512.New512_256()
default:
errorChannel <- Error{ErrInvalidHashAlgorithm, realIP(r, true), r.URL.Path}
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Invalid hash algorithm requested\n"))
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
}
return
}
_, err := io.WriteString(h, value)
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
return
}
if verbose {
fmt.Printf("%s | %s => %s\n",
startTime.Format(timeFormats["RFC3339"]),
realIP(r, true),
r.RequestURI)
}
_, err = w.Write([]byte(fmt.Sprintf("%x\n", h.Sum(nil))))
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
return
}
}
}
func registerHash(mux *httprouter.Router, usage *sync.Map, errorChannel chan<- Error) {
const module = "hash"
mux.GET("/hash/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/md5/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/md5/:string", serveHash(MD5, errorChannel))
mux.POST("/hash/md5/", serveHash(MD5, errorChannel))
mux.GET("/hash/sha1/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha1/:string", serveHash(SHA1, errorChannel))
mux.POST("/hash/sha1/", serveHash(SHA1, errorChannel))
mux.GET("/hash/sha224/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha224/:string", serveHash(SHA224, errorChannel))
mux.POST("/hash/sha224/", serveHash(SHA224, errorChannel))
mux.GET("/hash/sha256/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha256/:string", serveHash(SHA256, errorChannel))
mux.POST("/hash/sha256/", serveHash(SHA256, errorChannel))
mux.GET("/hash/sha384/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha384/:string", serveHash(SHA384, errorChannel))
mux.POST("/hash/sha384/", serveHash(SHA384, errorChannel))
mux.GET("/hash/sha512/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha512/:string", serveHash(SHA512, errorChannel))
mux.POST("/hash/sha512/", serveHash(SHA512, errorChannel))
mux.GET("/hash/sha512-224/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha512-224/:string", serveHash(SHA512_224, errorChannel))
mux.POST("/hash/sha512-224/", serveHash(SHA512_224, errorChannel))
mux.GET("/hash/sha512-256/", serveUsage(module, usage, errorChannel))
mux.GET("/hash/sha512-256/:string", serveHash(SHA512_256, errorChannel))
mux.POST("/hash/sha512-256/", serveHash(SHA512_256, errorChannel))
usage.Store(module, []string{
"/hash/md5/foo",
"/hash/sha1/foo",
"/hash/sha224/foo",
"/hash/sha256/foo",
"/hash/sha384/foo",
"/hash/sha512/foo",
"/hash/sha512-224/foo",
"/hash/sha512-256/foo",
})
}