Solved

Creating and storing GUID Strings

Posted on 2011-09-11
10
475 Views
Last Modified: 2012-05-12
Hi All:

I want to create GUID or some string from some USER Input.

I want to keep this string in some Linux directory for future use.

I can only visualize only one way to store it as a unique filename.

Is there a native way for storing GUID in Linux OS.

I would be using C++ and g++ compiler on Ubuntu Linux.

Best Regards,

-Sunnybrad
0
Comment
Question by:sunnybrad
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:tampnic
ID: 36524087
http://www.codeproject.com/KB/mcpp/guid.aspx shows a method of creating UUIDs. His demo application uses C++ .NET but the actual code creating the ID is not .NET dependent.

http://sourceforge.net/projects/e2fsprogs/ maintains the source code for the uuidgen utility. This is a Linux utility to generate unique id's. You might find it useful to use as-is, or look at the source code to get ideas.

Have you considered using a hash function to convert the string data into a key? As long as the string data is different for each USER, then a 128-bit SHA1 hash value will be as unique as most GUID algorithms.

Cheers,
  Chris
0
 
LVL 7

Assisted Solution

by:JimBeveridge
JimBeveridge earned 100 total points
ID: 36526497
There's nothing particularly special about a GUID. It's just a 16 byte value.  If you want to store it as a filename, you can either convert the value to hex.

Now it may be that you don't necessarily need a GUID (a Globally Unique Identifier), maybe what you need is a number that represents the string. That's called a hash, and you can use a function like MD5 to create a 16-byte value for any arbitrary string. As tampnic noted, you can also use SHA1, but that generates a 32 byte value. Unless you are doing something highly secure, it doesn't really matter which of these two algorithms you use.

0
 
LVL 7

Accepted Solution

by:
tampnic earned 400 total points
ID: 36526699
A hash function is looking like the way to go - There is a C implementation of MD5 in the MD5 RFC:
  http://www.ietf.org/rfc/rfc1321.txt

I would still recommend SHA1 as it is over 1,000,000,000 times more collision resistant (guaranteed to be more unique) than MD5 for *no extra effort* on your part. There are better hash algorithms around e.g. SHA256 but I've been using SHA1 for a lot of years when I need unique IDs in various situations and haven't come across problems, so that's what I recommend.

Quoting from wikipedia http://en.wikipedia.org/wiki/MD5
"In 1996, collisions were found in the compression function of MD5 ... in the future MD5 should no longer be implemented...where a collision-resistant hash function is required."

Here is a link to a similar question on the Ubuntu forums http://ubuntuforums.org/archive/index.php/t-337664.html where tomchuk gives some example source code on the use of libgcrypt http://www.gnupg.org/download/index.en.html#libgcrypt to generate a SHA1 hash.

there is also the mhash library http://mhash.sourceforge.net/ which allows you to access a variety of hash functions to fit your need.

There isn't a native function built into Linux, but there are a number of established libraries which contain the functionality you want to perform. Choose one of them and install it to your Ubuntu system, you might need to tell gcc where to find the library when you link your code but that's about it.

Another way to skin the banana could be to write a shell script which uses the output of uuidgen to create a file with the unique name - pass appropriate parameters to the script when you call it from your code. This is a bit of a dirty solution and depends on how good you are with shell scripting but if you're a competent scripter it would be quicker to get up and running.

Cheers,
  Chris

0
 
LVL 2

Expert Comment

by:xeltek
ID: 36526775
Hi,
I will strongly recommend openssl library to generate uuid, if you want better result.
you can use the link provided by tampnic as reference.
http://www.openssl.org/
0
 
LVL 7

Expert Comment

by:tampnic
ID: 36526893
The openssl implementation of SHA1 is just another library - they are all pretty much the same as the hash algorithms are all in the public domain. If you look hard enough on the internet I will bet you can find literally hundreds of SHA1 implementations, none better than the other as they all follow the same algorithm. Getting the full openssl library is way over the top for your needs as it does a lot more than hashing.

Cheers,
  Chris
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:JimBeveridge
ID: 36527382
@tampnic,

Given that the sample space of MD5 is larger than the number of atoms in the universe, telling sunnybrad that he should be worried about collisions is pretty ridiculous. Certainly one should worry about collisions in any number of cryptographic scenarios, but he just wants to hash some strings to filenames. Even if he wants to hash a million filenames, the chance of a collision is zero to many, many decimal places.

You also conveniently ignored a subsequent sentence in the Wikipedia article you cited, which says "md5 and sha1 are both clearly broken (in terms of collision-resistance)."  So your function that's  "1,000,000,000 times more collision resistant" appears to be just as broken as MD5.

I'm not arguing against using SHA1 - but your reasons for doing so are specious.
0
 
LVL 7

Expert Comment

by:tampnic
ID: 36527602
Hi Jim,
You are correct they are both "broken". Just that one is more broken than the other. If I have a straight choice between two functions and the same effort is involved in using each one, I will choose the one less broken every time.

Let's follow your argument that the reasons for choosing one or the other are specious ... As I had already mentioned hashing to the questioner and given him an algorithm to use, your post introducing MD5 was specious.

Cheers,
 Chris
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 36530350
Um, no, that's not what specious means. Look it up.

I introduced MD5 because sunnybrad specifically requested a GUID, which is a 16 byte value. SHA1 is a 20 byte value. MD5 and GUID are interchangeable, size-wise. SHA1 and GUID are not. So it was not clear whether your solution was applicable (you can strip 4 bytes from the SHA1, but then you are back to MD5 anyway, and the fact that you can safely strip bytes is certainly not obvious to someone who is not familiar with hashes.)

Therefore, your solution did not meet the parameters set out by sunnybrad, which is a 16-byte value.  It's entirely possible that sunnybrad didn't care about this - but you never addressed this issue in your discussion.
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 36530650
Chris,

I should also have said that your final answer was very helpful with the library references and the rest of the discussion. You deserved the points. Good job.
0
 
LVL 7

Expert Comment

by:tampnic
ID: 36531689
Hi Jim, the reason I didn't care about the length of the string/value comes from second-guessing the questioners proposal - "create GUID or some string ... store it as a unique filename"

*some string* isn't a specific length and filenames on any filesystem I've encountered aren't pegged to a specific length either. I was happy to relegate his mentioning of a GUID and interpreted the questioner as wanting "something unique" like a hash.

Your comments were all of merit too. We often have to second-guess the questioner a bit and that's where our discussion stems from. Thank you for the kind words - see you around fella :-)

P.S. I did look up the defintion of specious - thanks for the correction too :-)

Cheers,
  Chris
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now