• 0

c# Short URL from GUID


Question

Hello gang,

 

I am working on a new project for a site and I would like to implement short urls.  Historically I have used GUIDs as table ids so that replication is not an issue.  So, now I'm looking at creating a short url for these values, the thing is a shortened guid is not that short (vs a shortened Int) Before getting too far down a path, I thought I'd ask if anyone had any thoughts.

 

Thanks

Link to comment
https://www.neowin.net/forum/topic/1203663-c-short-url-from-guid/
Share on other sites

16 answers to this question

Recommended Posts

  • 0

Just generate strings of whatever size you consider to be short, made up of numbers, uppercase letters and lowercase letters, and associated them with your ID field.

 

[short_url_assoc_table]

table_id : GUID

short_url : string

  • 0
  On 06/03/2014 at 20:00, firey said:

What is it you are trying to do?

Like you have something say:

guasd3 = www.google.ca

-3idjqis = www.neowin.net

or?  

I guess I don't understand what you are trying to do exactly.

 

 

I need to use GUIDs as the table ident fields for the issue of replication.  If I use numeric values for the identity, which is easy to convert to a short URL by using Base64 (quite a number of examples around the net for doing this)  however if I use numeric I'm going to have collisions when multiple machines are making new records.  When I have looked at making a short url from a guid, the value is shorter than the GUID, but still longer than the average short GUID.

 

  On 06/03/2014 at 20:32, virtorio said:

Just generate strings of whatever size you consider to be short, made up of numbers, uppercase letters and lowercase letters, and associated them with your ID field.

 

[short_url_assoc_table]

table_id : GUID

short_url : string

 

Interesting idea, but this could be an issue with replication.  I am also concerned about multiple identity fields (waste of time, space, etc)   Thanks though

  • 0
  On 06/03/2014 at 21:16, James Rose said:

 

 

Interesting idea, but this could be an issue with replication.  I am also concerned about multiple identity fields (waste of time, space, etc)   Thanks though

 

What exactly is being replicated?

 

 

  On 06/03/2014 at 21:16, James Rose said:

I need to use GUIDs as the table ident fields for the issue of replication.  If I use numeric values for the identity, which is easy to convert to a short URL by using Base64 (quite a number of examples around the net for doing this)  however if I use numeric I'm going to have collisions when multiple machines are making new records.  When I have looked at making a short url from a guid, the value is shorter than the GUID, but still longer than the average short GUID.

What am I missing here? This shouldn't be an issue with a relational database. You don't tell your database what the ID of a row is, you let the database decide when it inserts the row(s).

  • 0

Im not sure where replication is happening.  Also why do you have to use guids for the identity, why not just use an incrementing number, and hash it's value or something to get the url you want to use?  Also, the database should be able to handle the ID itself.. and there should never be an issue of too many inserts causing problems.

  • Like 1
  • 0

Okay gang,

 

Replication of tables between multiple servers cannot use numeric values as, for example in SQL Server the Identity value is incrimented by 1 (and yes, you can change this value, but it wouldn't help when the app needs to scale)  Imagine two servers, each one adding new values to a table; "Customers" One each server they would both get identity #1 for the first record, when the two servers attempt to merge (ever hour, every minute, whenever) there would be a collision since there would be two records with the same identity value.  Using GUIDs for the key field avoids this issue as it is, almost, impossible to have the same guid twice.

 

 

Thanks Asik, however the "guidAsString" variable is still too long to be a short url.

  • 0

Looks like I found the answer.

                //Guid guid = Guid.NewGuid();
                string sGUID = "a33d4a21-7d95-41f7-859e-bf02b2fda650"
                string hashCode = String.Format("{0:X}", sGUID.GetHashCode());
                Console.WriteLine(hashCode);

This makes a nice small url, can anyone think of why this should not be used?

  • 0

^you shouldn't use it for the reasons listed here (about uniqueness guarantees and differences between versions): http://stackoverflow.com/questions/7458139/net-is-type-gethashcode-guaranteed-to-be-unique

 

Hash the GUID using SHA1 and truncate it or something like that. That's probably the best you are going to do. (perhaps you will have to truncate it to much, forcing a too high of probability for collision -- you should check the probability).

 

EDIT: Oh also, if you encode the result of hashing in a higher base, you reduce the amount of information loss during truncation.

  • 0
  On 06/03/2014 at 22:13, snaphat (Myles Landwehr) said:

^you shouldn't use it for the reasons listed here (about uniqueness guarantees and differences between versions): http://stackoverflow.com/questions/7458139/net-is-type-gethashcode-guaranteed-to-be-unique

 

Hash the GUID using SHA1 and truncate it or something like that. That's probably the best you are going to do. (perhaps you will have to truncate it to much, forcing a too high of probability for collision -- you should check the probability).

 

EDIT: Oh also, if you encode the result of hashing in a higher base, you reduce the amount of information loss during truncation.

 

EDIT:  Maybe what the article is saying, and what you are trying to tell me is that two different GUIDs could return the same hex?

 

 

 

Pardon me if I appear dense; I just read that article and ran some test against the same guid value for 1 billion iterations and it always comes up with the same hex.  I understand that 1 billion isn't necessarily that large a number...  what I am asking is shouldn't the hex value for a specific string always return the same hex value. 

 

Quote: "does not guarantee unique return values for different objects."  Since the app will pull the guid from the db, and then issue a hex on demand wouldn't that value always be the same?

 

Thanks for your input

  • 0
  On 06/03/2014 at 22:25, James Rose said:

Pardon me if I appear dense; I just read that article and ran some test against the same guid value for 1 billion iterations and it always comes up with the same hex.  I understand that 1 billion isn't necessarily that large a number...  what I am asking is shouldn't the hex value for a specific string always return the same hex value. 

 

Quote: "does not guarantee unique return values for different objects."  Since the app will pull the guid from the db, and then issue a hex on demand wouldn't that value always be the same?

 

Thanks for your input

 

It's the same each time because you are using the same version of the .net runtime on the same object for each run so it's producing the same hash. What they are saying is really two things: (1) if you switch versions of the .net runtime (e.g. 3.5 to 4), the returned result can be different for the same object, and (2) and within the same version of the runtime (e.g. 4) there can be collisions in hashes between different objects. There are no uniqueness guarantees.

 

So for example GUID_A.getHashCode() can return different results if you switch .net runtimes. And GUID_B.getHashCode() and GUID_C.getHashCode() could return the same result in the same runtime.

  • 0
  On 06/03/2014 at 22:31, snaphat (Myles Landwehr) said:

So for example GUID_A.getHashCode() can return different results if you switch .net runtimes. And GUID_B.getHashCode() and GUID_C.getHashCode() could return the same result in the same runtime.

 

yea, this is the answer I finally got to (see my edit above).  I was having a very hard time getting to the idea that a 30+ char piece of data could reliably be set to a shorter value.

  • 0
  On 06/03/2014 at 22:39, James Rose said:

yea, this is the answer I finally got to (see my edit above).  I was having a very hard time getting to the idea that a 30+ char piece of data could reliably be set to a shorter value.

Well in any case, you should re-encode whatever you do use to a higher number base that is still valid as url characters. For example, as I was saying before if you do the following you can store more information of your hash in less characters. 

String result=re_encode_as_base_X(SHA1_hash(GUID), N) //base 16 --> base N

I think at the end of the day, you will have to truncate though regardless of what you do. 

  • 0

It turns out I may be suffering from "doing this too long" desease.  Someone was kind enough to send a private message to me that the issue of replication on numeric  idenities may no longer be the issue it used to be.

 

I'm reading this article now:  http://technet.microsoft.com/en-us/library/ms146907%28v=sql.105%29.aspx

  • Like 1
  • 0
  On 06/03/2014 at 22:01, James Rose said:

Thanks Asik, however the "guidAsString" variable is still too long to be a short url.

I was suggesting taking the BigInteger and passing it through whatever method you mentionned that converted numerical values into short URLs, not taking it as a string directly. Anyway, looks like you found your answer.

This topic is now closed to further replies.
  • Posts

    • You could just do it now yourself with all the tools out there or the right Powershell scripts. Nothing stops you from tinkering away at it like the Tiny11 dev(s) do. It's easier to remove and turn off stuff when you're targeting a specific device like a gaming handheld. All the work/office bits alone are probably 60-70% of the code anyway.
    • hah, not for long. Microsoft will add copilot, video recording, social media integration, Microsoft 365 outlook, cdrom support, printer drivers, xbox memory card manager for zune music players, dedicated film and tv app for several providers that don't actually support it, and windows media centre for backward compatibility for anyone still using a sling tv box.... then they'll decide that it also need the start menu so they can provide a nice place to show your recent blank space that takes up half the screen while showing you full screen ads asking you to setup your Microsoft account for xbox. Then the only good thing it actually did, they'll let the new intern show their coding skills and the ABXY buttons will be changed around to 1256 because its better for international support or something, but ... at the end of it all, this time next year because Microsoft loves supporting software and hardware, the Asus oem won't get any more updates for it's Ally and it'll be forgotten faster than the Xbox Samsung TVs
    • Grounded 2 out next month, bringing fans a new miniaturized survival adventure by Pulasthi Ariyasinghe Alongside Avowed and The Outer Worlds 2, Obsidian Entertainment will officially be releasing three games in 2025. The first-party Xbox studio surprise revealed Grounded 2 during the Xbox Games Showcase event today. The same four kids from the original survival game are returning for this new adventure, and this time, they are miniaturized in a park. Check out the trailer above. Grounded 2 is set two years after the events of the first game, and this time, it's Brookhollow Park that will become the playground for players. Obsidian is getting help from Eidos Montréal as well, saying that the collaboration is adding more depth, danger, and discovery to the experience. In addition to building on the first game's features, Grounded 2 is introducing Buggies as one of its biggest features. Essentially bug mounts, these will let you ride ally bugs and use them across various operations in the game, including combat and building. Mounts had been one of the biggest requests by fans in the first game, and Obsidian says it has delivered with plenty of deep integrations Just like the original, Grounded 2 will be an early access title at launch, aiming to build out the game, story, and features with the community. Here are the key features of the Early Access/Game Preview launch: Omni-Tool introduced: A major quality of life upgrade that combines the hammer, axe, shovel, and wrench into one all-purpose tool, saving precious backpack space and streamlining your survival experience.  Story: In Grounded 2, we won’t tell you the whole story at the launch of Game Preview, but there’ll be enough there for you to start uncovering mysteries, chasing clues, and sharing your wildest theories right away (yes, we’re watching).  Expanded world-building brands: Expect the return of in-world favorites like Punch-O and Minotaurs & Myrmidons, alongside new brands and scenery that don’t just look cool—they tell a story (if you know where to look)—all coming together to bring Brookhollow Park to life with that signature Grounded charm.  New and returning bugs: Face off against familiar foes and never-before-seen creepy crawlies like the graceful cockroach, which adds new challenges and combat dynamics, such as having the ability to block your attacks.  Larger world, richer biomes: More spaces to build, explore, and survive in— Brookhollow Park is nearly as big as the entire backyard from the first game, packed with new secrets around every corner, from snack bars and toppled ice cream carts to long-forgotten edges of the park.  Community driven evolution: We’re building with you, and every update will be more meaningful and shaped by player feedback, with a public roadmap to keep you in the loop that we will share when Game Preview launches on July 29.  Combat 2.0 – Whether you’re flying solo or in full squad mode, new combat mechanics like dodging and smarter enemy behavior make every fight more intense—and more satisfying to survive.  Grounded 2 is launching on July 29 across PC and Xbox Series X|S consoles in early access on July 29, 2025, with a $29.99 price tag. Xbox Game Pass subscribers will be gaining the title at launch for no extra cost as well.
    • More like, doing too much that you don't need for a handheld gaming device, which isn't a full desktop PC you'd also plan to do work on.
    • Linux Mint is finally getting native fingerprint login support by David Uzondu In the latest monthly news roundup from the Linux Mint team, the developers announced a feature that users have wanted for a long, long time. The famously user-friendly distribution is finally getting proper, integrated support for fingerprint login. For a distro that has built its reputation on providing a comfortable, "it just works" experience for people new to Linux, the absence of this convenient security feature has been a noticeable gap, especially on modern laptops. The new feature will arrive in Linux Mint 22.2, powered by Fingwit, a brand new XApp built by the Mint developers. It handles detecting your fingerprint reader and recording your prints. Once configured, you can use your fingerprint for the login screen, the screensaver, authenticating sudo commands, and any other administrative actions that pop up a password dialog (pkexec). What makes this particularly interesting is how it deals with situations where a fingerprint just will not work. Fingwit uses fprintd for the backend work, but the Mint devs say its custom authentication module is clever enough to detect tricky cases. For instance, if your home directory is encrypted, you absolutely need your password to decrypt it at login. Just using a fingerprint would lead to a crashed session. Fingwit sees this coming and dynamically prompts for your password instead. The Mint team says Fingwit will be able to run "in any desktop environment and on any Linux distribution." A significant driver for this development has been the team's ongoing work with Framework. Testing the company's hardware has pushed the Mint team to better support the features packed into modern laptops. This partnership is also the reason Mint 22.1 got power profiles and why Mint 22.2 will ship with a newer HWE (hardware enablement) kernel. The team also announced a slew of other changes for the upcoming release. As part of this work, core applications like gnome-calendar, simple-scan (the document scanner), and baobab (the disk usage analyzer) will be upgraded to their newer libAdwaita versions. To solve the long-standing frustration of libAdwaita apps ignoring system themes, the developers have patched the library. Taking it a step further, they have forked it entirely into a new project called libAdapta. On a final, critical note, the team also had a serious warning for users of older versions. The Linux Mint 20.x series, which includes versions 20, 20.1, 20.2, and 20.3, officially reached its End of Life in April 2024. Your system will continue to function, but it will no longer receive any security updates from the official repositories, leaving it vulnerable. The team laid out two options. The recommended path is a fresh installation of a newer release (22.1), which provides support until 2029 and is the cleanest way forward. Alternatively, you can attempt a long and complicated in-place upgrade, which is a multi-step process from 20.x to 21.3.
  • Recent Achievements

    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      472
    2. 2
      +FloatingFatMan
      265
    3. 3
      ATLien_0
      235
    4. 4
      snowy owl
      224
    5. 5
      Edouard
      174
  • Tell a friend

    Love Neowin? Tell a friend!