生成一个唯一的ID [英] Generate a unique id

查看:109
本文介绍了生成一个唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生,在大学,我们的任务是创建一个搜索引擎。我有困难生成一个唯一的ID,当加入到边疆分配给每个URL。我已经使用SHA-256散列算法,以及尝试的Guid。这里是code,我用来实现的GUID:

 公共字符串generateID(字符串url_add)
{
    长I = 1;

    的foreach(BYTE B在Guid.NewGuid()的toByteArray())
    {
        I * =((int)的B + 1);
    }

    串号=的String.Format({0:D9}(DateTime.Now.Ticks / 10)%10亿);

    返回数;
}
 

解决方案

为什么不直接使用的ToString?

 公共字符串generateID()
{
    返回Guid.NewGuid()的ToString(N)。
}
 

如果你想它是基于一个网址,你可以简单地做到以下几点:

 公共字符串generateID(字符串sourceUrl)
{
    返回的String.Format({0} _ {1:N},sourceUrl,Guid.NewGuid());
}
 

如果你想隐藏的网址,你可以使用某种形式的SHA1对sourceURL的,但我不知道,可能实现的。

I am a student at university and our task is to create a search engine. I am having difficulty generating a unique id to assign to each url when added into the frontier. I have attempted using the SHA-256 hashing algorithm as well as Guid. Here is the code that i used to implement the guid:

public string generateID(string url_add)
{
    long i = 1;

    foreach (byte b in Guid.NewGuid().ToByteArray())
    {
        i *= ((int)b + 1);
    }

    string number = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 1000000000);

    return number;
}

解决方案

Why not just use ToString?

public string generateID()
{
    return Guid.NewGuid().ToString("N");
}

If you would like it to be based on a URL, you could simply do the following:

public string generateID(string sourceUrl)
{
    return string.Format("{0}_{1:N}", sourceUrl, Guid.NewGuid());
}

If you want to hide the URL, you could use some form of SHA1 on the sourceURL, but I'm not sure what that might achieve.

这篇关于生成一个唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆