/* * Version A of the C password example: unseeded rand(). * * Companion to "LLMs Can Write Code, but Cannot Read Your Mind" * https://hed.am/briefs/llms-can-write-code-but-cannot-read-your-mind/ * * cc -O2 -Wall -Wextra -o password-rand password-rand.c * ./password-rand * ./password-rand # same password, every time * * The C standard specifies that calling rand() without calling srand() first * behaves as though srand(1) had been called. The sequence is therefore the * same on every run, determined by the C library rather than by anything in * this file. Run the program twice and the same password appears twice. That * is the visible defect, and the easy one to catch. password-srand.c corrects * it and is still not secure. */ // Version A - looks fine, absolutely wrong for security #include #include #define PASSWORD_LENGTH 16 #define CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" int main(void) { const char charset[] = CHARSET; const int charset_size = sizeof(charset) - 1; // exclude null terminator for (int i = 0; i < PASSWORD_LENGTH; i++) { int index = rand() % charset_size; putchar(charset[index]); } putchar('\n'); return 0; }