This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to generate random keypass ?

I want to communicate Central and peripheral devices with KeyPass. Also I want to generate keypass in Peripheral device. But keypass should to include symbols, low/upper case and random character like "sP25Scw!HU?". I tried this code;

char *randstring(size_t length) { // length should be qualified as const if you follow a rigorous standard

static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";    
char *randomString;   // initializing to NULL isn't necessary as malloc() returns NULL if it couldn't allocate memory as requested

if (length) {
    randomString = malloc(length +1); // I removed your `sizeof(char) * (length +1)` as sizeof(char) == 1, cf. C99

    if (randomString) {        
        for (int n = 0;n < length;n++) {        
            int key = rand() % (int) (sizeof(charset) -1);
            randomString[n] = charset[key];
        }

        randomString[length] = '\0';
    }
}

return randomString;
}

And in main;

static char random_pass ;
random_pass = *randstring(10);

When I did debug I saw random_pass = 0x46 , randstring = 0x0001B11C

So, How do I generate keypass with the features mentioned above?

Related