]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libsec/port/nfastrand.c
libsec: generalize pbkdf2_hmac_sha1() to pbkdf2_x() passing the hmac as an argument
[plan9front.git] / sys / src / libsec / port / nfastrand.c
1 #include <u.h>
2 #include <libc.h>
3 #include <libsec.h>
4
5 #define Maxrand ((1UL<<31)-1)
6
7 ulong
8 nfastrand(ulong n)
9 {
10         ulong m, r;
11         
12         /*
13          * set m to the maximum multiple of n <= 2^31-1
14          * so we want a random number < m.
15          */
16         if(n > Maxrand)
17                 sysfatal("nfastrand: n too large");
18
19         m = Maxrand - Maxrand % n;
20         while((r = fastrand()) >= m)
21                 ;
22         return r%n;
23 }