Re: Random String
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 06 Jun 2007 15:15:34 +0000
Tor Rustad said:
Richard Heathfield wrote:
Andrea said:
Anyone could me suggest how to create a function that generates a
random string?
Here's one way you could do it:
That was rather a good example of why some application programmers,
should *NOT* implement security software.
I disagree. If I had been *trying* to implement security software, fine,
fair comment - but I wasn't. The OP asked how to generate a random
string. Others had *already pointed out* that the best he or she could
hope for in vanilla C was a *pseudo-random* string. I saw an
implementation elsethread that I considered a little clumsy, so I
figured I'd post something a bit better. I did not and do not claim
that it is cryptographically secure. If you want cryptographically
secure, don't use a PRNG to create passwords!
For the record, I think RH
already know, that we both can write a program which brute-force the
seed in "no" time.
For sufficiently large values of 0, sure.
I re-wrote your RandomString() function to this function (no malloc
and no '\"' and '\\' symbols):
$ cat rh_random.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
/* RH_Random: much FASTER version which DON'T malloc */
void RH_Random(unsigned char *str, size_t len)
Doesn't meet the interface spec.
{
/* do not include \" and \\ */
const char sym[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"%^&*()_-abcdefghijklmnopqrstuvwxyz"
"+=[]{}|,<.>/?";
size_t maxsym = sizeof sym / sizeof sym[0] - 1;
size_t i = 0;
assert(str);
Undefined behaviour in C90.
assert(len > 3);
while (i < len)
{
int r = (int) (maxsym * (rand() / (RAND_MAX + 1.0)));
Why the pointless cast?
<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: Random String
- From: Tor Rustad
- Re: Random String
- References:
- Random String
- From: Andrea
- Re: Random String
- From: Tor Rustad
- Random String
- Prev by Date: Re: Silly newbie question: Stop all processing on condition
- Next by Date: Re: #line
- Previous by thread: Re: Random String
- Next by thread: Re: Random String
- Index(es):
Relevant Pages
|