forked from glycerine/goq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
59 lines (49 loc) · 1.29 KB
/
signature.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
package main
import (
"crypto/hmac"
"crypto/sha1"
"fmt"
)
// check that message came from someone with
// who signed it with the same clusterid. The
// signature itself has to be blanked first.
func GetJobSignature(j *Job, cfg *Config) string {
jobcopy := *j
SignJob(&jobcopy, cfg)
return jobcopy.Signature
}
func JobSignatureOkay(j *Job, cfg *Config) bool {
sig := j.Signature
computed := GetJobSignature(j, cfg)
if computed == sig {
return true
}
return false
}
// sign by filling in the j.Signature field
//
// have to zero out both Signature and DesinationSocket before signing.
//
// SignJob() cannot modify Job, since it is used on the reciever for verification too.
//
func SignJob(j *Job, cfg *Config) {
j.Signature = ""
saveSock := j.destinationSock
j.destinationSock = nil
str := fmt.Sprintf("%+v\nclusterid:%s", *j, cfg.ClusterId)
//fmt.Printf("\n SignJob() signing this: '%s'\n", str)
//j.Signature = Sha1sum(str)
secretForHMAC := []byte(cfg.ClusterId)
j.Signature = string(Sha1HMAC([]byte(str), secretForHMAC))
j.destinationSock = saveSock
}
func Sha1sum(s string) string {
sum := sha1.Sum([]byte(s))
slsum := sum[:]
return fmt.Sprintf("%x", slsum)
}
func Sha1HMAC(message, key []byte) []byte {
mac := hmac.New(sha1.New, key)
mac.Write(message)
return mac.Sum(nil)
}