Help us shape the future of Go! Take the 2026 Go Developer Survey

crypto

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: BSD-3-Clause Imports: 9 Imported by: 10

README

crypto GoDoc

Functions

  • md5(string) - return md5 checksum from string.
  • sha256(string) - return sha256 checksum from string.
  • aes_encrypt(string, string, string, string) - return AES encrypted binary ciphertext
  • aes_decrypt(string, string, string, string) - return AES decrypted binary text
  • aes_encrypt_hex(string, string, string, string) - return AES encrypted hex-encoded ciphertext
  • aes_decrypt_hex(string, string, string, string) - return AES decrypted hex-encoded plain text

AES support 3 modes: GCM, CBC, and CTR - first parameter is mode, second is hex-encoded key, third is hex-encoded initialization vector or nonce - depending on the mode, and forth is hex-encoded plain text or ciphertext.

Since lua strings are binary safe, you can use any binary data as input and output and, for your convenience, the library also provides hex-encoded versions of the encrypt and decrypt functions. The first argument (the mode string) can be one of the following: "GCM", "CBC", or "CTR" (case-insensitive) and is not hex-encoded for the hex variants.

Examples

local crypto = require 'crypto'

-- md5
if not(crypto.md5("1\n") == "b026324c6904b2a9cb4b88d6d61c81d1") then
    error("md5")
end

-- sha256
if not(crypto.sha256("1\n") == "4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865") then
    error("sha256")
end

--- aes encrypt in GCM mode with hex-encoded data
s, err = crypto.aes_encrypt_hex(crypto.GCM, "86e15cbc1cbf510d8f2e51d4b63a2144", "b6b86d581a991a652158bd10", "48656c6c6f20776f726c64")
assert(not err, err)
if not(s == "7ec4e38508a26abf7b46e8dc90a7299d5144bcf045e460c3ef6b3e") then
    error("encrypt AES")
end

--- aes decrypt in GCM mode with hex-encoded data
s, err = crypto.aes_decrypt_hex(crypto.GCM, "86e15cbc1cbf510d8f2e51d4b63a2144", "b6b86d581a991a652158bd10", "7ec4e38508a26abf7b46e8dc90a7299d5144bcf045e460c3ef6b3e")
assert(not err, err)
if not(s == "48656c6c6f20776f726c64") then
    error("decrypt AES")
end

--- Binary examples setup of binary strings equivalent to the hex-encoded strings above:
local hex = require 'hex'
local key, iv, plaintext, encrypted, err
key, err = hex.decode_string('86e15cbc1cbf510d8f2e51d4b63a2144')
assert(not err, err)
iv, err = hex.decode_string('b6b86d581a991a652158bd10')
assert(not err, err)
plaintext, err = hex.decode_string('48656c6c6f20776f726c64')
assert(not err, err)
s, err = crypto.aes_encrypt(crypto.GCM, key, iv, plaintext)
assert(not err, err)
encrypted, err = hex.decode_string("7ec4e38508a26abf7b46e8dc90a7299d5144bcf045e460c3ef6b3e")
assert(not err, err)

--- aes encrypt binary in GCM mode
s, err = crypto.aes_encrypt(crypto.GCM, key, iv, plaintext)
assert(not err, err)
if not(s == encrypted) then
    error("encrypt AES")
end

--- aes decrypt in GCM mode
s, err = crypto.aes_decrypt(crypto.GCM, key, iv, encrypted)
assert(not err, err)
if not(s == plaintext) then
    error("decrypt AES")
end

Documentation

Overview

Package crypto implements golang package crypto functionality for lua.

Index

Examples

Constants

View Source
const (
	GCM mode = iota + 1
	CBC
	CTR
)

Variables

This section is empty.

Functions

func AESDecrypt added in v0.7.0

func AESDecrypt(l *lua.LState) int

AESDecrypt implement AES decryption in Lua.

func AESDecryptHex added in v0.7.0

func AESDecryptHex(l *lua.LState) int

AESDecryptHex implement AES decryption in Lua.

func AESEncrypt added in v0.7.0

func AESEncrypt(l *lua.LState) int

AESEncrypt implements AES encryption in Lua.

func AESEncryptHex added in v0.7.0

func AESEncryptHex(l *lua.LState) int

AESEncryptHex implements AES encryption in Lua.

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func MD5

func MD5(L *lua.LState) int

MD5 lua crypto.md5(string) return string

Example

crypto.md5(string)

state := lua.NewState()
Preload(state)
source := `
    local crypto = require("crypto")
    print(crypto.md5("1\n"))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
b026324c6904b2a9cb4b88d6d61c81d1

func Preload

func Preload(L *lua.LState)

Preload adds crypto to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local crypto = require("crypto")

func SHA256

func SHA256(L *lua.LState) int

SHA256 lua crypto.sha256(string) return string

Example

crypto.sha256(string)

state := lua.NewState()
Preload(state)
source := `
    local crypto = require("crypto")
    print(crypto.sha256("1\n"))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:
4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL