PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 13, 2022

[FIXED] How can I create a unique ID between 2 users?

 July 13, 2022     c#, encryption, messages, visual-studio-2013, winforms     No comments   

Issue

So I've been working on a C# encrypted messenger for fun, and it has the encryption/decryption sequence that needs to use a unique 45 character set.

I've created some just as demos, and the encryption works. But currently I'm adding a login system. When logged in, you get a list of people, and each person you talk to will have a unique conversation ID.

So basically, I want to find a way to create this conversation ID so that it is different for each use you have added, but the same for if another user adds you.

For example: I add Bob and the ID for the conversation is: R4ToGdKknnFKZNucj8xvpoP30vagfhtIdyrrLnQG

Currently, if Bob adds me, the ID will be different. But I'm trying to find a way to make it the same between 2 users, but different between 2 other users.

If anyone can help, that'd be great. I'm using the encryption code provided by CodeProject but edited for my needs. If needed, I can post the code here. I'm using a C# Windows Form Application.


Solution

Assuming that the usernames are consistent and unique, you could convert some string combination of them into a format more appropriate as an ID (such as the bigint this procedure creates):

string user1 = "daniel25";
string user2 = "kevin91";

var hashBuilder = new StringBuilder();
if (user1.CompareTo(user2) < 0)
{
   hashBuilder.Append(user1);
   hashBuilder.Append(user2);
}
else
{
   hashBuilder.Append(user2);
   hashBuilder.Append(user1);
}

var bytes = Encoding.Unicode.GetBytes(hashBuilder.ToString());
byte[] hashBytes;

using (var hasher = SHA1.Create())
{
    hashBytes = hasher.ComputeHash(bytes);
}

long value = BitConverter.ToInt64(hashBytes, 12);
var uniqueHash =  IPAddress.HostToNetworkOrder(value);

Store that ID as a bigint in your database and you have a primary key that references a conversation between two people, and is also easy to index and look up.

This method has some limitations (such as user "daniel25" an "kevin91" receiving the same hash as "daniel" and "25kevin91"), but I think you could work around them if you add a username 'separator' that is not allowable as a username character.

var hashBuilder = new StringBuilder();
hashBuilder.Append(user1);
hashBuilder.Append("@");
hashBuilder.Append(user2);

Also, no need to convert to an Int64 if you want to just use a true hash - you can take hashBytes right into an nvarchar field.



Answered By - Chris Schubert
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing