随机数产生器
//规则13: 包含10个或更多随机十六进制数,(随机数限制:0-9 ,A-F), 长度 10-16 , 默认是10
void pwd_rule_thirteen(char *fixed, char *head, char *value, int len)
{
char lengthstr[8] = "";
char length = 10;
unsigned char random_value[16] = "";
char randomstr[33] = "";
int i = 0;
int dev_random_fd = -2;
char* next_random_byte;
int bytes_to_read;
if ( !fixed || !head || !value ) {
debug("param fixed or head or value is NULL \n");
return ;
}
cfgd_read_value_by_config_name("SYS_WIFI_PWD_LENGTH", lengthstr, sizeof(lengthstr) );
if ( strlen(lengthstr) ) {
length = atoi(lengthstr);
if ( 16 < length ) {
length = 16;
}
}
if ( length < len ) {
debug("len is too short , length : %d but len : %d \n" , length , len );
}
if (dev_random_fd == -2)
{
dev_random_fd = open("/dev/random", O_RDONLY);
if(-1 == dev_random_fd) {
debug("read /dev/random failed \n");
dev_random_fd = open("/dev/urandom", O_RDONLY);
}
if(-1 == dev_random_fd) {
debug("read /dev/urandom failed \n");
return ;
}
}
next_random_byte = (char*) &random_value;
bytes_to_read = sizeof (random_value);
do {
int bytes_read;
bytes_read = read (dev_random_fd, next_random_byte, bytes_to_read);
bytes_to_read -= bytes_read;
next_random_byte += bytes_read;
} while (bytes_to_read > 0);
close(dev_random_fd);
for(i = 0; i < sizeof(random_value); i++)
{
sprintf(randomstr + i * 2, "%02X", random_value[i]);
}
randomstr[32] = '\0';
snprintf(value, length, "%s%c", randomstr , '\0' );
}