10个字符ID,全局和本地唯一

0 c c++ linux x86-64 sip

我需要生成一个10个字符的唯一ID(SIP/VOIP人员需要知道它是P-Charging-Vector头中的param icid值).每个字符应为26个ASCII字母之一(区分大小写),10个ASCII数字之一或连字符减号.

它必须是"全局唯一的(在生成id的机器之外)"并且足够"本地唯一(在生成id的机器内)",并且所有需要打包成10个字符,p!

这是我的看法.我是第一个编码'必须'编码全局唯一本地IP地址到base-63(它是一个无符号长整数,编码后将占用1-6个字符)然后尽可能多的当前时间戳(其一个time_t/long long int,编码后将占用9-4个字符,具体取决于编码的ip地址占用的空间大小.

我还在时间戳中添加了循环计数'i',以便在一秒钟内多次调用该函数时保留唯一性.

这是否足以在全球和本地独特,还是有另一种更好的方法?

拉夫

#include <stdio.h>
#include <string.h>
#include <sys/time.h>

//base-63 character set
static char set[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";

// b63() returns the next vacant location in char array x
int b63(long long longlong,char *x,int index){
    if(index > 9)
        return index+1;

    //printf("index=%d,longlong=%lld,longlong%63=%lld\n",index,longlong,longlong%63);
    if(longlong < 63){
        x[index] = set[longlong];
        return index+1;
    }  

    x[index] = set[longlong%63];
    return b63(longlong/63,x,index+1);
}

int main(){
    char x[11],y[11] = {0}; /* '\0' is taken care of here */

    //let's generate 10 million ids
    for(int i=0; i<10000000; i++){

        /*  add i to timestamp to take care of sub-second function calls,
            3770168404(is a sample ip address in n/w byte order) =                84.52.184.224 */
        b63((long long)time(NULL)+i,x,b63((long long)3770168404,x,0));

        // reverse the char array to get proper base-63 output
        for(int j=0,k=9; j<10; j++,k--)
            y[j] = x[k];

        printf("%s\n",y);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Dou*_* T. 5

它必须是"全局唯一的(在生成id的机器之外)"并且足够"本地唯一(在生成id的机器内)",并且所有需要打包成10个字符,p!

您是否掌控所有软件生成ID?你在玩ids吗?如果不...

我对SIP一无所知,但必须误解你对规范的看法(或规范必定是错误的).如果另一个开发人员尝试使用与您编写的算法不同的算法来构建id,那么您将与其ID进行冲突,这意味着他们将在该系统中更长时间地全局唯一.

我将回到SIP文档,看看是否有附录,其中包含生成这些ID的算法.或者可能是一个更聪明的SO用户,而不是我可以回答用于生成这些id的SIP算法.