lundi 15 février 2016

Is this a good approach to random byte array genaration in C?

I know this kind of question has been already discussed here: How to generate a random number in C?

But I wanted to share the way I implemented it just to know what people may think about it.

#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

long int getNanoSecs(){
    struct timespec unixtimespec;
    clock_gettime(CLOCK_REALTIME_COARSE, &unixtimespec);
    return unixtimespec.tv_nsec;
}

. . .

unsigned char personal_final_token[PERSONAL_FINAL_TOKEN_SIZE_SIZE];
                unsigned char pwd_ptr_ctr;
                srandom(getNanoSecs()*(getNanoSecs()&0xFF));
                for(pwd_ptr_ctr=0;pwd_ptr_ctr<PERSONAL_FINAL_TOKEN_SIZE_SIZE;pwd_ptr_ctr++){
                    memset(personal_final_token+pwd_ptr_ctr,(random()^getNanoSecs())&0xFF,1);
                }

just define PERSONAL_FINAL_TOKEN_SIZE_SIZE and you can generate a quite random token of the size you want.




Aucun commentaire:

Enregistrer un commentaire