forked from tengattack/gluacrypto
/
hmac.go
52 lines (46 loc) · 958 Bytes
/
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
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
}