Files
bjarneo--cliamp/luaplugin/api_crypto.go
T
wehub-resource-sync ead81af521
CI / go (push) Waiting to run
CI / build (darwin, macos-14) (push) Waiting to run
CI / build (windows, windows-2025) (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Failing after 0s
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

41 lines
965 B
Go

package luaplugin
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
lua "github.com/yuin/gopher-lua"
)
// registerCryptoAPI adds cliamp.crypto.{md5,sha256,hmac_sha256} to the cliamp table.
func registerCryptoAPI(L *lua.LState, cliamp *lua.LTable) {
tbl := L.NewTable()
L.SetField(tbl, "md5", L.NewFunction(func(L *lua.LState) int {
s := L.CheckString(1)
h := md5.Sum([]byte(s))
L.Push(lua.LString(hex.EncodeToString(h[:])))
return 1
}))
L.SetField(tbl, "sha256", L.NewFunction(func(L *lua.LState) int {
s := L.CheckString(1)
h := sha256.Sum256([]byte(s))
L.Push(lua.LString(hex.EncodeToString(h[:])))
return 1
}))
L.SetField(tbl, "hmac_sha256", L.NewFunction(func(L *lua.LState) int {
key := L.CheckString(1)
msg := L.CheckString(2)
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(msg))
L.Push(lua.LString(hex.EncodeToString(mac.Sum(nil))))
return 1
}))
L.SetField(cliamp, "crypto", tbl)
}