6 #include "BitTwiddle.hpp"
7 #include "RandomString.h"
9 static std::atomic
<uint32_t> s_rnd_next_seed
;
11 size_t RandomStringBuffer(unsigned char *out
, size_t min_len
, size_t max_len
, unsigned int flags
)
14 rng
.seed(getpid() ^ time(NULL
) ^ RevBytes(uint32_t(clock())) ^ uint32_t(s_rnd_next_seed
));
16 if (max_len
> min_len
) {
17 std::uniform_int_distribution
<std::mt19937::result_type
> len_dist(0, max_len
- min_len
);
21 std::uniform_int_distribution
<std::mt19937::result_type
> chr_dist(0, 0xff);
22 for (size_t i
= 0; i
< len
; ) {
23 unsigned char v
= chr_dist(rng
);
25 if ((flags
& RNDF_ZERO
) == 0) {
28 } else if (v
>= 'A' && v
<= 'Z') {
29 if ((flags
& RNDF_HICASE
) == 0) {
32 } else if (v
>= 'a' && v
<= 'z') {
33 if ((flags
& RNDF_LOCASE
) == 0) {
36 } else if (v
>= '0' && v
<= '9') {
37 if ((flags
& RNDF_DIGITS
) == 0) {
40 } else if (v
>= 128) {
41 if ((flags
& RNDF_X128
) == 0) {
44 } else if ((flags
& RNDF_ETC
) == 0) {
52 std::uniform_int_distribution
<std::mt19937::result_type
> seed_dist(0, 0xffffffff);
53 s_rnd_next_seed
= seed_dist(rng
);
58 void RandomStringAppend(std::string
&out
, size_t min_len
, size_t max_len
, unsigned int flags
)
60 const size_t orig_size
= out
.size();
61 out
.resize(orig_size
+ max_len
);
62 size_t len
= RandomStringBuffer(&out
[orig_size
], min_len
, max_len
, flags
);
63 out
.resize(orig_size
+ len
);