LUA_LIBS += e2option.lua hash.lua tools.lua transport.lua cache.lua url.lua
LUA_LIBS += generic_git.lua eio.lua err.lua lock.lua errno.lua trace.lua
LUA_LIBS += assrt.lua
-SO_LIBS = lsha1.so lsha.so leio.so le2lib.so
+SO_LIBS = lsha.so leio.so le2lib.so
CLEAN_FILES = *~ *.o *.so
all: $(SO_LIBS)
-lsha1.so: lsha1.o sha1.o
-
lsha.so: lsha.o sha1.o sha2.o
sha2.o: sha2.c sha2.h
local e2lib = require("e2lib")
local eio = require("eio")
local err = require("err")
-local lsha1 = require("lsha1")
+local lsha = require("lsha")
local strict = require("strict")
local trace = require("trace")
--- Create a hash context. Throws error object on failure.
-- @return Hash context object.
function hash.hash_start()
- local errstring, hc
- hc = { _data = "" }
-
- hc._ctx, errstring = lsha1.init()
- if not hc._ctx then
- error(err.new("initializing SHA1 context failed: %s", errstring))
- end
-
+ local hc = { _data = "" }
+ hc._ctx = lsha.sha1_init()
return strict.lock(hc)
end
assert(type(hc) == "table")
assert(type(data) == "string")
assert(hc._data and hc._ctx)
- local rc, errstring
hc._data = hc._data .. data
-- Consume data and update hash whenever 64KB are available
- if #hc._data >= 64*1024 then
- rc, errstring = lsha1.update(hc._ctx, hc._data)
- if not rc then
- error(err.new("%s", errstring))
- end
+ if #hc._data >= 65536 then
+ lsha.sha1_update(hc._ctx, hc._data)
hc._data = ""
end
end
return false, re
end
- cs, re = hash.hash_finish(hc)
- if not cs then
- return false, re
- end
-
+ cs = hash.hash_finish(hc)
hcache_add(path, cs)
return cs
end
-- @param hc the hash context
-- @return SHA1 Checksum.
function hash.hash_finish(hc)
- local rc, errstring, cs
-
- rc, errstring = lsha1.update(hc._ctx, hc._data)
- if not rc then
- error(err.new("%s", errstring))
- end
+ local cs
- cs, errstring = lsha1.final(hc._ctx)
- if not cs then
- error(err.new("%s", errstring))
- end
+ lsha.sha1_update(hc._ctx, hc._data)
+ cs = lsha.sha1_final(hc._ctx)
-- Destroy the hash context to catch errors
- for k,_ in pairs(hc) do
- hc[k] = nil
- end
+ hc._data = nil
+ hc._ctx = nil
return cs
end
+++ /dev/null
-/*
- * Copyright (C) 2007-2016 emlix GmbH, see file AUTHORS
- *
- * This file is part of e2factory, the emlix embedded build system.
- * For more information see http://www.e2factory.org
- *
- * e2factory is a registered trademark of emlix GmbH.
- *
- * e2factory is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your
- * option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- */
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
-
-#include "sha1.h"
-
-static int
-init(lua_State *L)
-{
- SHA1_CTX *ctx = malloc(sizeof(SHA1_CTX));
- if (ctx == NULL) {
- lua_pushboolean(L, 0);
- lua_pushstring(L, strerror(errno));
- return 2;
- }
-
- SHA1Init(ctx);
- lua_pushlightuserdata(L, ctx);
- return 1;
-}
-
-static int
-update(lua_State *L)
-{
- const char *s;
- size_t len;
- SHA1_CTX *ctx;
-
- ctx = lua_touserdata(L, 1);
- if (ctx == NULL) {
- lua_pushboolean(L, 0);
- lua_pushstring(L, "lsha1.update: missing sha1 context");
- return 2;
- }
-
- s = lua_tolstring(L, 2, &len);
- if (s == NULL) {
- lua_pushboolean(L, 0);
- lua_pushstring(L, "lsha1.update: data missing or of wrong type");
- return 2;
- }
-
- SHA1Update(ctx, (unsigned char *)s, len);
- lua_pushboolean(L, 1);
-
- return 1;
-}
-
-static int
-final(lua_State *L)
-{
- SHA1_CTX *ctx;
- unsigned char digest[20];
- char s[41];
- int i;
-
- ctx = lua_touserdata(L, 1);
- if (ctx == NULL) {
- lua_pushboolean(L, 0);
- lua_pushstring(L, "lsha1.final: missing sha1 context");
- return 2;
- }
-
- SHA1Final(digest, ctx);
-
- memset(ctx, 0, sizeof(SHA1_CTX));
- free(ctx);
-
- for (i = 0; i < 20; i++) {
- snprintf(s + i*2, 2+1, "%02x", digest[i]);
- }
- lua_pushstring(L, s);
-
- return 1;
-}
-
-static luaL_reg lib[] = {
- { "init", init },
- { "update", update },
- { "final", final },
- { NULL, NULL }
-};
-
-int luaopen_lsha1(lua_State *L)
-{
- luaL_Reg *next;
-
- lua_newtable(L);
- for (next = lib; next->name != NULL; next++) {
- lua_pushcfunction(L, next->func);
- lua_setfield(L, -2, next->name);
- }
-
- return 1;
-}