Sign in or Sign up

[C#] Create an identifier from input with an extension method
Started by deadeye


Rate this topic
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5


5 posts in this topic
deadeye Offline
Programmer
***


Registered
Posts: 247
Threads: 19
Joined: Sat Apr 2017
Reputation: 2

CZPoints: 28 CZP
HQMember
10-13-2017, 09:45 AM -
#1
This thread will show you how to create a GUID (Globally Unique Identifier) via a custom extension method for the Guid struct. It also shows you how to write and use extension methods.

I created following code for a console application. My recommendation is to extract the Return method in order to put it into a library project to use it anywhere.

Here is the code:

Code:
using System;
using System.Security.Cryptography;
using System.Text;

namespace Extensions
{
    public static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new Guid().Return("some input"));
            //this will create the Guid {75fd2bfa-7b50-0632-b3dc-ee37b6282dd4}
            Console.ReadKey();
        }

        static Guid Return(this Guid pBase, Object pInput)
        {
            return new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(pInput.ToString())));
        }
    }
}

I thinks it's pretty self-explanatory.


Maybe you have the question "For what can I use this?"
The answer is simple: for example, if you have a database with a table that uses GUIDs as identifiers and you want to retrieve a single object you have i.e. the name of, you can generate the id from this name and return the desired record from the database. The SQL query could look like this:
Code:
select * from your_table where ID='29fa8c2b-92d4-1937-dacf-71d8a0e5c726';


Thanks for reading, if you have a problem or an idea how to improve, please send me a pn or post a comment here. Also, if you liked this post, please post a comment. This motivates me to post more tutorials!
This post was last modified: 10-16-2017, 10:38 AM by deadeye.
[Image: cw2gsbi.png]

Discord: deadeye#7164

Disclaimer: I have no idea what I'm talking about.
Dox Offline
Cyber Security Student
***


Contributor
Posts: 414
Threads: 125
Joined: Mon Oct 2017
Reputation: 6

CZPoints: 136 CZP
OnFireContributor
10-13-2017, 04:16 PM -
#2
Pretty neet. Thanks for sharing this information with us friend.
Contact E-Mail: [email protected]
BTC Address: 1JmJrbmQn4Bg24r68bSyZ7TxNZGad3iPWM
[NS]DarkDude Offline
~# Nulled Systems #~
***


NulledSystems
Posts: 32
Threads: 5
Joined: Sun Oct 2017
Reputation: 3

CZPoints: 24 CZP
10-16-2017, 01:44 AM -
#3
Could it not be done using :

Code:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace ConsoleApplication5 {  
    class Program {  
        static int Main(string[] args) {  
            Guid obj = Guid.NewGuid();  
            Console.WriteLine("Generated GUI : " + obj.ToString());  
            Console.ReadLine();  
            return -1;  
        }  
    }  
or am I misunderstanding?
deadeye Offline
Programmer
***


Registered
Posts: 247
Threads: 19
Joined: Sat Apr 2017
Reputation: 2

CZPoints: 28 CZP
HQMember
10-16-2017, 03:43 AM -
#4
(10-16-2017, 01:44 AM)an1m4l Wrote: Could it not be done using :

Code:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace ConsoleApplication5 {  
    class Program {  
        static int Main(string[] args) {  
            Guid obj = Guid.NewGuid();  
            Console.WriteLine("Generated GUI : " + obj.ToString());  
            Console.ReadLine();  
            return -1;  
        }  
    }  
or am I misunderstanding?

Yeah, but my method uses an input. So, if you have an id like "section_1" and want a guid for it, you can use the string as argument. Your method generates a random guid.
[Image: cw2gsbi.png]

Discord: deadeye#7164

Disclaimer: I have no idea what I'm talking about.
[NS]DarkDude Offline
~# Nulled Systems #~
***


NulledSystems
Posts: 32
Threads: 5
Joined: Sun Oct 2017
Reputation: 3

CZPoints: 24 CZP
10-16-2017, 10:29 AM -
#5
(10-16-2017, 03:43 AM)deadeye Wrote:
(10-16-2017, 01:44 AM)an1m4l Wrote: Could it not be done using :

Code:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace ConsoleApplication5 {  
    class Program {  
        static int Main(string[] args) {  
            Guid obj = Guid.NewGuid();  
            Console.WriteLine("Generated GUI : " + obj.ToString());  
            Console.ReadLine();  
            return -1;  
        }  
    }  
or am I misunderstanding?

Yeah, but my method uses an input. So, if you have an id like "section_1" and want a guid for it, you can use the string as argument. Your method generates a random guid.

Oh okay, I thought so.
deadeye Offline
Programmer
***


Registered
Posts: 247
Threads: 19
Joined: Sat Apr 2017
Reputation: 2

CZPoints: 28 CZP
HQMember
10-16-2017, 10:39 AM -
#6
@an1m4l I have added more info to the post
[Image: cw2gsbi.png]

Discord: deadeye#7164

Disclaimer: I have no idea what I'm talking about.




Users browsing this thread: 1 Guest(s)