• 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

    • tvOS 26 arrives with karaoke mode, updated profile switcher, Liquid Glass, and more by Aditya Tiwari WWDC 2025 is now live, and Apple has finally renamed all of its operating systems to version 26. This year, we have tvOS 26 for the Apple TV, featuring the freshly-baked Liquid Glass software design. Apple explained that Liquid Glass brings a fresh and vibrant look to Apple TV, "delivering a fresh and expressive design that beautifully reflects and refracts its surroundings using real-time rendering." Content is the primary focus of the experience when you fast-forward, rewind, or change a setting from the Control Center. The redesigned Apple TV app now features new poster art for movies and TV shows, with a touch of Liquid Glass. Users can now choose whether to display the profile switcher UI when their Apple TV wakes up, allowing them to quickly access their Watchlist and recommendations in the TV app, as well as playlists in Apple Music. Speaking of Apple Music, your iPhone can now double as a handheld microphone for Apple TV. The new karaoke mode allows multiple users to queue up songs from their iPhones and sing along, with support for lyrics translation and pronunciation. Apple has updated the FaceTime experience to make it consistent with iPhone. Contact Posters introduced with iOS 17 are now available on Apple TV, which display a contact's customized photo and name when starting a FaceTime call. Live Captions support on FaceTime now includes French, German, Japanese, Korean, Mandarin, and Spanish. FaceTime on Apple TV has been updated to display phone and voice call notifications for active profiles. Users can use their connected HomePod speakers or iPhone to answer them. Apart from that, tvOS 26 also brings new Aerial screen savers, featuring India's Goa and Kerala. Users can also personalize their screensaver experience and designate any AirPlay-enabled speaker as the permanent speaker for Apple TV. tvOS 26 has also made the sign-in experience a bit easier for users. Developers can link app logins to an Apple account, making it quicker for users to access their apps and content when setting up a new device. The new tvOS update is now available for testing through the Apple Developer Program, supported on Apple TV 4K (2nd generation and later). Meanwhile, the updated sing-along feature is available on iPhone 11 and later with Apple TV 4K (3rd generation).
    • Yup, a bit slow on some PCs at the time, but absolutely gorgeous.
    • That's essentially what I've been running on KDE
    • For some of us, we've never left! A lot of customization tools have allowed us to adopt a non-native glassy design.
    • iPadOS 26 is now official with improved windowing, menu bar, 'Liquid Glass' redesign, more by Taras Buria As expected, today, at WWDC 2025, Apple announced iOS 26 and other operating systems that now carry the 26 version number (for simplicity). iPadOS is also part of the big redesign, and it is now official with the new Liquid Glass design, improved window controls, a Mac-like menu bar, file management enhancements, and more. In addition to the general Liquid Glass redesign, which is now present across all of Apple's operating systems, iPadOS 26 received several iPad-exclusive upgrades. It now has a reworked windowing system that helps you organize and switch between apps. You can finally place and resize apps exactly how you want. Plus, iPadOS now has familiar window controls: three buttons for closing, minimizing, and resizing your app. Straight from macOS. Apps can now remember their previous size and position, and Expose can show you all open windows. Finally, iPadOS now has a menu bar, which is another thing that comes from macOS. It gives access to common actions, commands, and features. Developers can customize the menu bar in their apps and specify what features are available there. Apple is also upgrading the Files app with a reworked List view, which now shows more details about your files and the ability to expand or collapse folders. Plus, you can customize folders with colors and emojis, pin folders to the dock, and set default apps for file types. When working with long-running processes, background tasks show up as live activities, so that you can track Final Cut exports or the Files app moving stuff around. Other changes in iPadOS 26 include improved audio controls and the ability to select audio input source and record audio with voice isolation. The Journal app is now available on the iPad, Notes received markdown support, and Calculator now supports 3D graphing capabilities. You can learn more about iPadOS 26 in the official announcement post here.
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      478
    2. 2
      +FloatingFatMan
      278
    3. 3
      ATLien_0
      257
    4. 4
      Edouard
      205
    5. 5
      snowy owl
      200
  • Tell a friend

    Love Neowin? Tell a friend!