]> pd.if.org Git - zpackage/blobdiff - lib/integ.c
switch to blake2
[zpackage] / lib / integ.c
index 016302e43f82dd0725192ea5b807d0d209fc42a7..b51d27e333892c5f9080bc7fb0b80ad881ad0216 100644 (file)
@@ -3,17 +3,17 @@
 
 #include "zpm.h"
 #include "sqlite3.h"
-#include "sha256.h"
+#include "lib/blake2/ref/blake2.h"
 
-static void hash_byte(struct sha256_state *h, int ch) {
+static void hash_byte(struct blake2b_state__ *h, int ch) {
        unsigned char buf[1];
 
        buf[0] = ch & 0xff;
-       sha256_process(h, buf, 1);
+       blake2b_update(h, buf, 1);
 }
 
 /* i will be positive, we are hashing column sizes */
-static void hash_int(struct sha256_state *h, int i) {
+static void hash_int(struct blake2b_state__ *h, int i) {
        int n;
        uint64_t z;
 
@@ -56,7 +56,7 @@ static void hash_int(struct sha256_state *h, int i) {
  * with no delimiters of any kind.
  */
 
-static void hash_query(struct zpm *zpm, const char *zSql, struct sha256_state *h) {
+static void hash_query(struct zpm *zpm, const char *zSql, struct blake2b_state__ *h) {
        sqlite3 *db;
        sqlite3_stmt *pStmt = 0;
        int nCol; /* Number of columns in the result set */
@@ -86,7 +86,7 @@ static void hash_query(struct zpm *zpm, const char *zSql, struct sha256_state *h
        nCol = sqlite3_column_count(pStmt);
 
        while (sqlite3_step(pStmt) == SQLITE_ROW) {
-               sha256_process(h, (const unsigned char *)"R", 1);
+               blake2b_update(h, "R", 1);
                for (i = 0; i < nCol; i++) {
                        switch (sqlite3_column_type(pStmt, i)) {
                                case SQLITE_NULL:
@@ -127,15 +127,19 @@ static void hash_query(struct zpm *zpm, const char *zSql, struct sha256_state *h
                                        hash_byte(h, 'B');
                                        hash_int(h, bytes);
                                        break;
+                               default:
+                                       hash_byte(h, 'U');
+                                       continue;
+                                       break;
                        }
-                       sha256_process(h, data, bytes);
+                       blake2b_update(h, data, bytes);
                }
        }
        sqlite3_finalize(pStmt);
 }
 
 int zpm_package_hash(struct zpm *zpm, char *pkgid, char *hash) {
-       struct sha256_state d;
+       struct blake2b_state__ d;
        char *sql;
        int i;
        unsigned char tmp[32];
@@ -144,9 +148,10 @@ int zpm_package_hash(struct zpm *zpm, char *pkgid, char *hash) {
                return 0;
        }
 
-       sha256_init(&d);
        /* find package */
 
+       blake2b_init(&d, 32);
+
        sql = sqlite3_mprintf("select package,version,release,description,architecture,url,licenses,packager,build_time from packages_pkgid where pkgid = %Q", pkgid);
 
        hash_query(zpm, sql, &d);
@@ -155,20 +160,27 @@ int zpm_package_hash(struct zpm *zpm, char *pkgid, char *hash) {
        /* hash package files */
 
        sql = sqlite3_mprintf("select path, mode, username, groupname, configuration, "
-               "filetype, target, devmajor, devminor, mtime, hash "
+               "filetype, target, device, mtime, hash "
                "from packagefiles_pkgid where pkgid = %Q order by path",
                pkgid);
        hash_query(zpm, sql, &d);
        sqlite3_free(sql);
 
-       sha256_done(&d, tmp);
+       /* package dependencies */
+       sql = sqlite3_mprintf("dselect requires from packagedeps"
+               " where printf('%%q-%%q-%%d',package,version,release) = %Q"
+              "order by requires",
+               pkgid);
+       hash_query(zpm, sql, &d);
+       sqlite3_free(sql);
+
+       blake2b_final(&d, tmp, sizeof tmp);
        for (i=0; i<32; i++) {
                sprintf(hash+i*2, "%02x", (unsigned)tmp[i]);
        }
        hash[64] = 0;
 
        return 1;
-
 }
 
 int zpm_package_sethash(struct zpm *zpm, char *pkgid, char *hash) {
@@ -187,5 +199,4 @@ int zpm_package_sethash(struct zpm *zpm, char *pkgid, char *hash) {
        sqlite3_free(sql);
 
        return 1;
-
 }