3

I would like to create a function which would enable me to generate unique values always based on these two entry parameters:

public string ReturnUniqueValue(DateTime date, string ID)
{
 // Logic here
}

The date parameter is in the following format:

2017-08-14 11:55:32.00

While the ID is in the following format:

112452573848

I would like to generate a unique hash which is 40 characthers in length and that it never repeats.

Is this doable with datetime parameter + unique id string ?

I figured that datetime is never the same (it's nearly impossible), thus this should be able to always generate unique value?

8
  • 2
    It is possible that you get duplicates from combining these 2 (unless ID is always unique). If you want a somewhat unique string, look into Guid.NewGuid() Commented Aug 21, 2017 at 12:50
  • @Shyju yes the ID is always unique, what would be the best way to hash this ? :) Commented Aug 21, 2017 at 12:51
  • 1
    @adiga that's just combining two strings? :D Commented Aug 21, 2017 at 12:52
  • 2
    Use Guid instead, it was created exactly for this. With your method I could pass the same DateTime instance and ID field as many times as I wanted Commented Aug 21, 2017 at 12:53
  • 1
    @CamiloTerevinto I can't use GUID because the data comes from API source, and I need to somehow hash it in order to compare the values that I have in my DB and from API ^^ Commented Aug 21, 2017 at 12:53

1 Answer 1

6

You can

  • write all your parameters (whichever) into a stream
  • compute a hash using your favorite algorithm (e.g. SHA265)
  • and return its first 20 values (= 40 characters) as a final result

In code:

public string ReturnUniqueValue(DateTime date, string ID)
{
    var result = default(byte[]);

    using (var stream = new MemoryStream())
    {
        using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
        {
            writer.Write(date.Ticks);
            writer.Write(ID);
        }

        stream.Position = 0;

        using (var hash = SHA256.Create())
        {
            result = hash.ComputeHash(stream);
        }
    }

    var text = new string[20];

    for (var i = 0; i < text.Length; i++)
    {
        text[i] = result[i].ToString("x2");
    }

    return string.Concat(text);
}

Note: if you just want a single unique value for multiple parameters, a simple concatenation should already do it. Since you explicitly asked for a "hash" with 40 characters, this more elaborate solution may fit somehow better.

Attention: extending this to more parameters of the same type (e.g. two strings) should include the parameters position within the stream to avoid collisions ((a, b) != (b, a)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.