Integrating Lua inside Nginx

Download Lua from http://luajit.org/download.html
 unzip folder
cd /<FolderPath>
make
make install

Ngx_Lua Module
<Put inside add module in ./configure>
https://github.com/openresty/lua-nginx-module/tags

Installation Process
http://wiki.nginx.org/HttpLuaModule#Installation

./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_secure_link_module --add-module=/opt/nginxdependencies/ngx_devel_kit-master --add-module=/opt/nginxdependencies/set-misc-nginx-module-0.23 --add-module=/opt/nginxdependencies/encrypted-session-nginx-module-master --add-module=/opt/nginxdependencies/nginx-let-module-master --add-module=/opt/nginxdependencies/replace-filter-nginx-module-master --add-module=/opt/nginxdependencies/lua-nginx-module-0.9.10 --with-ld-opt='-Wl,-rpath,/usr/local/lib'

configuring additional modules
adding module in /opt/nginxdependencies/ngx_devel_kit-master
 + ngx_devel_kit was configured
adding module in /opt/nginxdependencies/set-misc-nginx-module-0.23
found ngx_devel_kit for ngx_set_misc; looks good.
 + ngx_http_set_misc_module was configured
adding module in /opt/nginxdependencies/encrypted-session-nginx-module-master
 + ngx_http_encrypted_session_module was configured
adding module in /opt/nginxdependencies/nginx-let-module-master
 + ngx_http_let_module was configured
adding module in /opt/nginxdependencies/replace-filter-nginx-module-master
checking for agentzh's sregex library ... found
 + ngx_http_replace_filter_module was configured
adding module in /opt/nginxdependencies/lua-nginx-module-0.9.10
checking for Lua library ... not found
checking for Lua library in /usr/local/ ... not found
checking for Lua library in /usr/local/ ... not found
checking for Lua library in /usr/pkg/ ... not found
checking for Lua library in /opt/local/ ... not found
checking for Lua library in /usr/local/*/lua51/ ... not found
checking for Lua library in /usr/ ... not found
checking for LuaJIT library in /usr/local/ ... found
checking for export symbols by default (-E) ... found
checking for export symbols by default (--export-all-symbols) ... not found
checking for SO_PASSCRED ... found
 + ngx_http_lua_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Now question is what all we can do with this scripting language
https://github.com/openresty/lua-resty-redis
http://www.stavros.io/posts/writing-an-nginx-authentication-module-in-lua/
http://wiki.nginx.org/HttpLuaModule

Examples
1)
set $res 5;
set $res1 2;

set_by_lua $sum '
          return ngx.var.res +ngx.var.res1
        ';
add_header Lua_res $sum;
2)
set $tempo "aq=lk=/90+=/p/l+==";
set_by_lua $myStr '
 local res1 = ngx.var.tempo:gsub("=",";")
local res2 = res1:gsub("/","_")
local res3 = res2:gsub("+","-")
return res3
';
add_header Lua_resNewString $myStr;

3)
set $tempo1 $myStr;#"aq=lk=/90+=/p/l+==";
set_by_lua $myStr1 '
 local res1 = ngx.var.tempo1:gsub(";","=")
local res2 = res1:gsub("_","/")
local res3 = res2:gsub("-","+")
return res3
';
add_header Lua_resNewString1 $myStr1;
4)For encryption
set_by_lua $encyptLua '
 local aes = require "resty.aes"
local str = require "resty.string"
 local aes_128_cbc_with_iv = assert(aes:new("1234567890123456",
        nil, aes.cipher(128,"cbc"), {iv="1234567890123456"}))
        -- AES 128 CBC with IV and no SALT
    local encrypted = aes_128_cbc_with_iv:encrypt("Really secret message!")
          return encrypted
        ';
add_header EncrptedLua $encyptLua;

set_encode_base64 $encyptLua;
add_header enctptedEncodedLUA $encyptLua;

5)For decryption
set_by_lua $dncyptLua '
 local aes = require "resty.aes"
local str = require "resty.string"
 local aes_128_cbc_with_iv = assert(aes:new("1234567890123456",
        nil, aes.cipher(128,"cbc"), {iv="1234567890123456"}))
        -- AES 128 CBC with IV and no SALT
    local encrypted = aes_128_cbc_with_iv:encrypt("Really secret message!")
          return aes_128_cbc_with_iv:decrypt(encrypted)
        ';

add_header DcrptedLua $dncyptLua;
Above example can used to decrypt the string at nginx, to encypt it at url creation we can use attach file
++++++Plus+++++
Add entry in nginx.conf above server {}
lua_package_path "/opt/nginxdependencies/lua-resty-string-master/lib/?.lua;;";
Lua Resty String Module

----------------Java code for encryption----------------------
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    
    public class Encryptor {
     public static String encrypt(String key1, String key2, String value) {
      try {
       IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
    
       SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
         "AES");
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
       cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
       byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("Encypted Bytes :"+encrypted);       
System.out.println("Encrypted string:"
         + Base64.encodeBase64String(encrypted));
       return Base64.encodeBase64String(encrypted);
      } catch (Exception ex) {
       ex.printStackTrace();
      }
      return null;
     }
    
     public static String decrypt(String key1, String key2, String encrypted) {
      try {
       IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
    
       SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
         "AES");
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
       cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
       byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
    
       return new String(original);
      } catch (Exception ex) {
       ex.printStackTrace();
      }
      return null;
     }
    
     public static void main(String[] args) {
    
      //String key1 = "Bar12345Bar12345"; // 128 bit key
      //String key2 = "ThisIsASecretKet";
  String key1 = "1234567890123456"; // 128 bit key
      String key2 = "1234567890123456";
      System.out.println(decrypt(key1, key2,
        encrypt(key1, key2, "Really secret message!")));
     }
    }




----Java code end----------------

No comments:

Post a Comment