]> Git repositories of Nishi - repoview.git/commitdiff
use urandom instead of rand
authorNishi <nishi@nishi.boats>
Wed, 21 Aug 2024 19:23:54 +0000 (19:23 +0000)
committerNishi <nishi@nishi.boats>
Wed, 21 Aug 2024 19:23:54 +0000 (19:23 +0000)
git-svn-id: file:///raid/svn-personal/repoview/trunk@29 7e8b2a19-8934-dd40-8cb3-db22cdd5a80f

CGI/avatar.c
CGI/rv_avatar.h
CGI/util.c

index 3d611891321ff0db73feb71615129f378f14a604..facfd4d386d87ba78198b157f9cf8854525535cd 100644 (file)
@@ -1,3 +1,31 @@
 /* $Id$ */
 
 #include "rv_avatar.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <png.h>
+
+void rv_avatar_generate(const char* name) {
+       FILE* f = fopen(name, "wb");
+
+       if(f == NULL) return;
+
+       png_structp pngp = NULL;
+       png_infop infop = NULL;
+       png_bytep row = NULL;
+
+       pngp = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+       infop = png_create_info_struct(pngp);
+
+       if(setjmp(png_jmpbuf(pngp))) {
+               goto closeall;
+       }
+
+closeall:
+       fclose(f);
+       if(infop != NULL) png_free_data(pngp, infop, PNG_FREE_ALL, -1);
+       if(pngp != NULL) png_destroy_write_struct(&pngp, (png_infopp)NULL);
+       if(row != NULL) free(row);
+}
index 1fda796b9bfc144d332e1a2b3ce13ca3d8263b1d..573aa4f065fe3eef2a2e8f71b6d33f2f91f391fc 100644 (file)
@@ -3,4 +3,6 @@
 #ifndef __RV_AVATAR_H__
 #define __RV_AVATAR_H__
 
+void rv_avatar_generate(const char* name);
+
 #endif
index b483cce3473841688d8d902559c1422ec561bd76..92fd6f083b4579f2351b812ed3c85947d8d6e5a7 100644 (file)
@@ -86,10 +86,14 @@ char* rv_new_token(const char* username) {
        char* token = malloc(17);
        token[16] = 0;
        int i;
+       unsigned char uc;
+       FILE* f = fopen("/dev/urandom", "rb");
 regenerate:
        for(i = 0; i < 16; i++) {
-               token[i] = tokenstr[rand() % strlen(tokenstr)];
+               fread(&uc, 1, 1, f);
+               token[i] = tokenstr[uc % strlen(tokenstr)];
        }
+       fclose(f);
        if(rv_has_token(token)) goto regenerate;
        rv_save_token(username, token);
        return token;