读取 /proc/sys/kernel/random/uuid 可以得到一个guid

来源:互联网 发布:电动剃须刀推荐 知乎 编辑:程序博客网 时间:2024/06/05 11:44

读取 /proc/sys/kernel/random/uuid 可以得到一个guid

string CommFun::GenGuid(int extend){char uuid[39];int fd = open("/proc/sys/kernel/random/uuid", O_RDONLY);if(fd>=0){read(fd, uuid+1, 36);}uuid[37] = 0;if(extend==0){                close(fd);return uuid+1;}uuid[0] = '{';uuid[37] = '}';uuid[38] = 0;return uuid;}

void GenGuid(void){char dbcode[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};long long int uuid[2];static long long int seed=0;int rfd;if ((rfd = open("/dev/urandom", O_RDWR)) < 0 ){*(int32_t*)(uuid) = random();*(int32_t*)(uuid+1) = random();*(int32_t*)(uuid+2) = random();*(int32_t*)(uuid+3) = random();}else{if(!seed){seed=time(0)+getpid();write(rfd, &seed, sizeof(seed));}if(read(rfd, uuid, 16)!=16){*(int32_t*)(uuid) = random();*(int32_t*)(uuid+1) = random();*(int32_t*)(uuid+2) = random();*(int32_t*)(uuid+3) = random();}close(rfd);}static char uuidstring[40] = "1E176852-AB1F-FFFF-FFFF-15CAF932FE70";int indx=0;for(int i = 0; i < 2; ++i){for(int j=0; j<16; ++indx){if(indx==8 || indx==13 || indx==18 || indx==23){uuidstring[indx] = '-';continue;}uuidstring[indx] = dbcode[uuid[i] & 0xf];++j;uuid[i] = uuid[i]>>4;}}uuidstring[37]='\0';printf("%s\n", uuidstring);}


原创粉丝点击