Commit 3f17936f authored by Joel Petersen's avatar Joel Petersen Committed by Andrei Mihu
Browse files

Added support for AES-256. (#241)

parent 0dab4bd3
Loading
Loading
Loading
Loading
+24 −6
Original line number Diff line number Diff line
@@ -132,6 +132,8 @@ func (n *RuntimeLuaNakamaModule) Loader(l *lua.LState) int {
		"base16_decode":               n.base16Decode,
		"aes128_encrypt":              n.aes128Encrypt,
		"aes128_decrypt":              n.aes128Decrypt,
		"aes256_encrypt":              n.aes256Encrypt,
		"aes256_decrypt":              n.aes256Decrypt,
		"md5_hash":                    n.md5Hash,
		"sha256_hash":                 n.sha256Hash,
		"hmac_sha256_hash":            n.hmacSHA256Hash,
@@ -799,15 +801,15 @@ func (n *RuntimeLuaNakamaModule) base16Decode(l *lua.LState) int {
	return 1
}

func (n *RuntimeLuaNakamaModule) aes128Encrypt(l *lua.LState) int {
func aesEncrypt(l *lua.LState, keySize int) int {
	input := l.CheckString(1)
	if input == "" {
		l.ArgError(1, "expects string")
		return 0
	}
	key := l.CheckString(2)
	if len(key) != 16 {
		l.ArgError(2, "expects key 16 bytes long")
	if len(key) != keySize {
		l.ArgError(2, fmt.Sprintf("expects key %v bytes long", keySize))
		return 0
	}

@@ -836,15 +838,15 @@ func (n *RuntimeLuaNakamaModule) aes128Encrypt(l *lua.LState) int {
	return 1
}

func (n *RuntimeLuaNakamaModule) aes128Decrypt(l *lua.LState) int {
func aesDecrypt(l *lua.LState, keySize int) int {
	input := l.CheckString(1)
	if input == "" {
		l.ArgError(1, "expects string")
		return 0
	}
	key := l.CheckString(2)
	if len(key) != 16 {
		l.ArgError(2, "expects key 16 bytes long")
	if len(key) != keySize {
		l.ArgError(2, fmt.Sprintf("expects key %v bytes long", keySize))
		return 0
	}

@@ -870,6 +872,22 @@ func (n *RuntimeLuaNakamaModule) aes128Decrypt(l *lua.LState) int {
	return 1
}

func (n *RuntimeLuaNakamaModule) aes128Encrypt(l *lua.LState) int {
	return aesEncrypt(l, 16)
}

func (n *RuntimeLuaNakamaModule) aes128Decrypt(l *lua.LState) int {
	return aesDecrypt(l, 16)
}

func (n *RuntimeLuaNakamaModule) aes256Encrypt(l *lua.LState) int {
	return aesEncrypt(l, 32)
}

func (n *RuntimeLuaNakamaModule) aes256Decrypt(l *lua.LState) int {
	return aesDecrypt(l, 32)
}

func (n *RuntimeLuaNakamaModule) md5Hash(l *lua.LState) int {
	input := l.CheckString(1)
	if input == "" {