forked from tcentric/cloud-lab
/
controller.go
55 lines (50 loc) · 1.11 KB
/
controller.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
package controller
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"net/http"
"os"
"github.com/gorilla/mux"
)
func GetHash(kind, text string) (string, error) {
var (
h hash.Hash
hashed string
)
switch kind {
case "Sha1":
h = sha1.New()
case "Sha256":
h = sha256.New()
case "Sha384":
h = sha512.New384()
case "Sha512":
h = sha512.New()
case "Md5":
h = md5.New()
default:
return "", errors.New("unsupported hashing algorithm")
}
h.Write([]byte(text))
hashed = hex.EncodeToString(h.Sum(nil))
return hashed, nil
}
func Sha256Handler(w http.ResponseWriter, r *http.Request) {
// to get the username, use the following:
username := mux.Vars(r)["username"]
// to calculate sha256 hash of a string, use internal/hashes package and function GetHash
hash, _ := GetHash("Sha256", username)
// to send the response, use the following:
fmt.Fprint(w, hash)
w.WriteHeader(http.StatusOK)
}
func GithubUsernameHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, os.Getenv("GITHUB_USERNAME"))
}