雜湊的模組載入器

lua-users home
wiki

以下是模組載入器的範例,在載入模組之前,會驗證模組的雜湊。它是根據 LuaCrypto [1] 所設計的。它並不真正安全,它只是一個範例,用來展示如何新增新的模組載入器。(JeromeVuarand

module(..., package.seeall)

local crypto = require 'crypto'

local dtypes = {"md5", "md4", "md2", "sha1", "sha", "sha256", "sha512"}

local function load(modulename)
  -- Find source
  local filename
  local file,hashfile,hashtype
  local errmsg = ""
  for path in string.gmatch(package.path..";", "([^;]*);") do
    filename = string.gsub(path, "%?", (string.gsub(modulename, "%.", "\\")))
    file = io.open(filename, "rb")
    -- If we found a module check if it has a hash file
    if file then
      for _,dtype in ipairs(dtypes) do
        hashfile = io.open(filename.."."..dtype, "rb")
        if hashfile then
          hashtype = dtype
          break
        end
      end
    end
    if hashfile then
      break
    end
    errmsg = errmsg.."\n\tno file '"..filename.."' (signed)"
  end
  if not file then
    return errmsg
  end
  -- Read source file
  local source = file:read("*a")
  -- Read saved hash
  local hash = hashfile:read("*a"):gsub("[^%x]", "")
  -- Check that the saved hash match the file hash
  assert(crypto.evp.digest(hashtype, source)==hash,
    "module "..modulename.." (from file '"..filename.."')"
    .." does not match its "..hashtype.." hash")
  -- Compile and return the module
  return assert(loadstring(source, filename))
end

-- Install the loader so that it's called just before the normal Lua loader
table.insert(package.loaders, 2, load)

可以在 [hashedmodules-200705100234.zip] 找到該程式,其中包含範例模組和測試腳本。


最近的變更 · 偏好設定
編輯 · 歷程記錄
最近於 2008 年 2 月 27 日下午 5:36 GMT 編輯 (比較)