/
hmac.go
82 lines (75 loc) · 1.59 KB
/
hmac.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
package gluacrypto_crypto
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
lua "github.com/yuin/gopher-lua"
)
func hmacFn(L *lua.LState) int {
algorithm := lua.LVAsString(L.Get(1))
s := lua.LVAsString(L.Get(2))
key := lua.LVAsString(L.Get(3))
raw := lua.LVAsBool(L.Get(4))
var h hash.Hash
switch algorithm {
case "md5":
h = hmac.New(md5.New, []byte(key))
case "sha1":
h = hmac.New(sha1.New, []byte(key))
case "sha256":
h = hmac.New(sha256.New, []byte(key))
case "sha512":
h = hmac.New(sha512.New, []byte(key))
default:
L.Push(lua.LNil)
L.Push(lua.LString("unsupported algorithm"))
return 2
}
_, err := h.Write([]byte(s))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
var result string
if !raw {
result = hex.EncodeToString(h.Sum(nil))
} else {
result = string(h.Sum(nil))
}
L.Push(lua.LString(result))
return 1
}
func validHmacFn(L *lua.LState) int {
algorithm := L.CheckString(1)
message := L.CheckString(2)
key := L.CheckString(3)
mmac := L.CheckString(4)
var mac hash.Hash
switch algorithm {
case "md5":
mac = hmac.New(md5.New, []byte(key))
case "sha1":
mac = hmac.New(sha1.New, []byte(key))
case "sha256":
mac = hmac.New(sha256.New, []byte(key))
case "sha512":
mac = hmac.New(sha512.New, []byte(key))
default:
L.Push(lua.LNil)
L.Push(lua.LString("unsupported algorithm"))
return 2
}
mac.Write([]byte(message))
expectedMAC := mac.Sum(nil)
if hmac.Equal([]byte(mmac), expectedMAC) {
L.Push(lua.LTrue)
} else {
L.Push(lua.LFalse)
}
return 1
}