238 lines
5.1 KiB
Go
238 lines
5.1 KiB
Go
package crypto
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"crypto/sha1"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"math/big"
|
|
|
|
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func GetFunctionList() map[string]luajit.GoFunction {
|
|
return map[string]luajit.GoFunction{
|
|
"base64_encode": base64_encode,
|
|
"base64_decode": base64_decode,
|
|
"base64_url_encode": base64_url_encode,
|
|
"base64_url_decode": base64_url_decode,
|
|
"hex_encode": hex_encode,
|
|
"hex_decode": hex_decode,
|
|
"md5_hash": md5_hash,
|
|
"sha1_hash": sha1_hash,
|
|
"sha256_hash": sha256_hash,
|
|
"sha512_hash": sha512_hash,
|
|
"hmac_sha256": hmac_sha256,
|
|
"hmac_sha1": hmac_sha1,
|
|
"uuid_generate": uuid_generate,
|
|
"uuid_generate_v4": uuid_generate_v4,
|
|
"uuid_validate": uuid_validate,
|
|
"random_bytes": random_bytes,
|
|
"random_hex": random_hex,
|
|
"random_string": random_string,
|
|
"secure_compare": secure_compare,
|
|
}
|
|
}
|
|
|
|
func base64_encode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(str))
|
|
s.PushString(encoded)
|
|
return 1
|
|
}
|
|
|
|
func base64_decode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
decoded, err := base64.StdEncoding.DecodeString(str)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("invalid base64 data")
|
|
return 2
|
|
}
|
|
s.PushString(string(decoded))
|
|
return 1
|
|
}
|
|
|
|
func base64_url_encode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
encoded := base64.URLEncoding.EncodeToString([]byte(str))
|
|
s.PushString(encoded)
|
|
return 1
|
|
}
|
|
|
|
func base64_url_decode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
decoded, err := base64.URLEncoding.DecodeString(str)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("invalid base64url data")
|
|
return 2
|
|
}
|
|
s.PushString(string(decoded))
|
|
return 1
|
|
}
|
|
|
|
func hex_encode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
encoded := hex.EncodeToString([]byte(str))
|
|
s.PushString(encoded)
|
|
return 1
|
|
}
|
|
|
|
func hex_decode(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
decoded, err := hex.DecodeString(str)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("invalid hex data")
|
|
return 2
|
|
}
|
|
s.PushString(string(decoded))
|
|
return 1
|
|
}
|
|
|
|
func md5_hash(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
hash := md5.Sum([]byte(str))
|
|
s.PushString(hex.EncodeToString(hash[:]))
|
|
return 1
|
|
}
|
|
|
|
func sha1_hash(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
hash := sha1.Sum([]byte(str))
|
|
s.PushString(hex.EncodeToString(hash[:]))
|
|
return 1
|
|
}
|
|
|
|
func sha256_hash(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
hash := sha256.Sum256([]byte(str))
|
|
s.PushString(hex.EncodeToString(hash[:]))
|
|
return 1
|
|
}
|
|
|
|
func sha512_hash(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
hash := sha512.Sum512([]byte(str))
|
|
s.PushString(hex.EncodeToString(hash[:]))
|
|
return 1
|
|
}
|
|
|
|
func hmac_sha256(s *luajit.State) int {
|
|
message := s.ToString(1)
|
|
key := s.ToString(2)
|
|
h := hmac.New(sha256.New, []byte(key))
|
|
h.Write([]byte(message))
|
|
s.PushString(hex.EncodeToString(h.Sum(nil)))
|
|
return 1
|
|
}
|
|
|
|
func hmac_sha1(s *luajit.State) int {
|
|
message := s.ToString(1)
|
|
key := s.ToString(2)
|
|
h := hmac.New(sha1.New, []byte(key))
|
|
h.Write([]byte(message))
|
|
s.PushString(hex.EncodeToString(h.Sum(nil)))
|
|
return 1
|
|
}
|
|
|
|
func uuid_generate(s *luajit.State) int {
|
|
id := uuid.New()
|
|
s.PushString(id.String())
|
|
return 1
|
|
}
|
|
|
|
func uuid_generate_v4(s *luajit.State) int {
|
|
id := uuid.New()
|
|
s.PushString(id.String())
|
|
return 1
|
|
}
|
|
|
|
func uuid_validate(s *luajit.State) int {
|
|
str := s.ToString(1)
|
|
_, err := uuid.Parse(str)
|
|
s.PushBoolean(err == nil)
|
|
return 1
|
|
}
|
|
|
|
func random_bytes(s *luajit.State) int {
|
|
length := int(s.ToNumber(1))
|
|
if length < 0 || length > 65536 {
|
|
s.PushNil()
|
|
s.PushString("invalid length")
|
|
return 2
|
|
}
|
|
bytes := make([]byte, length)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
s.PushNil()
|
|
s.PushString("failed to generate random bytes")
|
|
return 2
|
|
}
|
|
s.PushString(string(bytes))
|
|
return 1
|
|
}
|
|
|
|
func random_hex(s *luajit.State) int {
|
|
length := int(s.ToNumber(1))
|
|
if length < 0 || length > 32768 {
|
|
s.PushNil()
|
|
s.PushString("invalid length")
|
|
return 2
|
|
}
|
|
bytes := make([]byte, length)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
s.PushNil()
|
|
s.PushString("failed to generate random bytes")
|
|
return 2
|
|
}
|
|
s.PushString(hex.EncodeToString(bytes))
|
|
return 1
|
|
}
|
|
|
|
func random_string(s *luajit.State) int {
|
|
length := int(s.ToNumber(1))
|
|
if length < 0 || length > 65536 {
|
|
s.PushNil()
|
|
s.PushString("invalid length")
|
|
return 2
|
|
}
|
|
|
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
if s.GetTop() >= 2 && !s.IsNil(2) {
|
|
charset = s.ToString(2)
|
|
}
|
|
|
|
if len(charset) == 0 {
|
|
s.PushNil()
|
|
s.PushString("empty charset")
|
|
return 2
|
|
}
|
|
|
|
result := make([]byte, length)
|
|
charsetLen := big.NewInt(int64(len(charset)))
|
|
for i := range result {
|
|
n, err := rand.Int(rand.Reader, charsetLen)
|
|
if err != nil {
|
|
s.PushNil()
|
|
s.PushString("failed to generate random number")
|
|
return 2
|
|
}
|
|
result[i] = charset[n.Int64()]
|
|
}
|
|
s.PushString(string(result))
|
|
return 1
|
|
}
|
|
|
|
func secure_compare(s *luajit.State) int {
|
|
a := s.ToString(1)
|
|
b := s.ToString(2)
|
|
s.PushBoolean(hmac.Equal([]byte(a), []byte(b)))
|
|
return 1
|
|
}
|