Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++/CLI

GUID Generator based on SHA1

4.80/5 (2 votes)
27 Jul 2006CPOL1 min read 1   1.4K  
GUID Generator based on SHA1
Sample Image - screen.gif

Introduction

The GUID generator GuidGen.exe provided by VC can only generate random GUID. This tool can generate GUID base on a string such as class name. This means one class name maps to one GUID.

Background

RFC 4122 described the algorithm for creating a Name-Based UUID.

http://www.ietf.org/rfc/rfc4122.txt

The requirements for these types of UUIDs are as follows:

  • The UUIDs generated at different times from the same name in the same namespace MUST be equal.
  • The UUIDs generated from two different names in the same namespace should be different (with very high probability).
  • The UUIDs generated from the same name in two different namespaces should be different with (very high probability).
  • If two UUIDs that were generated from names are equal, then they were generated from the same name in the same namespace (with very high probability).

The algorithm for generating a UUID from a name and a namespace are as follows:

  • Allocate a UUID to use as a "name space ID" for all UUIDs generated from names in that namespace; see Appendix C for some pre-defined values.
  • Choose either MD5 [4] or SHA-1 [8] as the hash algorithm; If backward compatibility is not an issue, SHA-1 is preferred.

Using the Code

I used the sample implementation code from the RFC 4122 document and I fixed the code to let it use openssl implementation of SHA1.

C++
#include <openssl/crypto.h>
#include <openssl/md5.h>
#include <openssl/sha.h>

It uses SHA1 to generate UUID:

C++
uuid_create_sha1_from_name(&id, NameSpace_DNS, ss, length);

You can change it to use MD5 easily:

C++
uuid_create_md5_from_name(&id, NameSpace_DNS, ss, length);

To build the source code, you need to download openssl from http://openssl.org, and build it as its guide.

Or you can just use the demo tool, it requires .NET Framework runtime.

History

  • 27th July, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)