Custom implementation of the GUID generation

Name : GuidGen
Author : Perevoznyk Serhiy
Description : Custom implementation of the GUID generation
History :

Date By Description
---- -- -----------
5/13/05 Perevoznyk Serhiy Initial creation of the Unit.

unit GuidGen;


Windows, SysUtils;

ReservedNCS = $00;
Standard = $02;
ReservedMicrosoft = $06;
ReservedFuture = $07;
TimeBased = $01;
Reserved = $02;
NameBased = $03;
VRandom = $04;
VariantByte = 8;
VariantByteMask = $3f;
VariantByteShift = 6;
VersionByte = 7;
VersionByteMask = $0f;
VersionByteShift = 4;

The CryptAcquireContext function is used to acquire a handle to a particular
key container within a particular cryptographic service provider (CSP). This
returned handle is used in calls to CryptoAPI functions that use the selected CSP.
function CryptAcquireContextA(phProv : pointer; pszContainer : LPCTSTR;
pszProvider : LPCTSTR; dwprovType : DWORD; dwFlags : DWORD) : boolean; stdcall; external 'Advapi32.dll';

The CryptReleaseContext function releases the handle of a cryptographic service
provider (CSP) and a key container. At each call to this function, the reference
count on the CSP is reduced by one. When the reference count reaches zero, the
context is fully released and it can no longer be used by any function in
the application.
function CryptReleaseContext(hProv : pointer; dwFlags : DWORD) : boolean; stdcall; external 'Advapi32.dll';

The CryptGenRandom function fills a buffer with cryptographically random bytes.
function CryptGenRandom(hProv : pointer; dwLen : DWORD; pbBuffer : pointer) : boolean; stdcall; external 'Advapi32.dll';

Custom method to generate GUIDs
function NewGUID : TGUID;


function NewGUID : TGUID;
hCryptProv : pointer;
bits : array[0..15] of byte;
FillChar(Result, 16, 0);
hCryptProv := nil;
if not CryptAcquireContextA(@hCryptProv, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) then
raise Exception.Create('Failed to acquire cryptography handle');
if not CryptGenRandom(hCryptProv, 16, @bits) then
raise Exception.Create('Failed to generate cryptography random bytes');
bits[VariantByte] := bits[VariantByte] AND VariantByteMask;
bits[VariantByte] := bits[VariantByte] Or (Standard shl VariantByteShift);
bits[VersionByte] := bits[VersionByte] And VersionByteMask;
bits[VersionByte] := bits[VersionByte] Or (VRandom shl VersionByteShift);
Move(bits, Result, 16);
if hCryptProv nil then
CryptReleaseContext(hCryptProv, 0);



One thought on “Custom implementation of the GUID generation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s