Generating a universally unique identifier (UUID)

Introduction
This article will provide an example of how to generate a universally unique identifier (UUID). For more information on UUID, see the following hyperlink.

Universally unique identifier - Wikipedia

Example
The following is an example of how to build a UUID, using either 'hexadecimal' or 'base64' encoding.

USER>Set str="Hello, World!" USER>Write ##class(Utilities.UUID).MD5Hash(str, "hex", $ZTS) 775a126e-9bca-ecd8-0e39-3904f7167c1b USER>Write ##class(Utilities.UUID).MD5Hash(str, "b64", $ZTS) PTQiz7lrzyjOO2mKaEN80B89

Code
The code to generate a UUID string is as follows.

Class Utils.UUID Extends %RegisteredObject [ ProcedureBlock ] {

ClassMethod MD5Hash(input As %String, mode As %String, salt As %String, case As %String) As %String {       /*		   description: method to generate a universally unique identifier (UUID). (http://en.wikipedia.org/wiki/uuid) arguments: input  - input string mode   - output mode b64 = base 64 hex = hex salt   - salt string case   - output case return value: output - output hash variables: hash   - hash key pos    - position char   - character delim  - delimiter notes: always add a 'salt' to your input string, as a dictionary search can decrypt a MD5 hash (e.g. 'http://md5.noisette.ch/index.php'). for example, "5d41402abc4b2a76b9719d911017c592" = md5("hello"). */

// initialise local variables Set output=""

// classmethod MD5Encode(text As %String) returns %String // ** DEPRECATED *** (in Caché 5.1) // This method has been renamed MD5Hash.

// If $System.Version.GetNumber["5.0." {        If $ZVersion[" 5.0." {                Set hash=$System.Encryption.MD5Encode(input_$Get(salt)) }        Else { Set hash=$System.Encryption.MD5Hash(input_$Get(salt)) }

Set mode=$ZConvert($Get(mode,"hex"),"L") If mode="b64" { Set output=$System.Encryption.Base64Encode("="_hash_"=") }

If mode="hex" { For pos=1:1:$Length(hash) { Set delim=$Select(pos=4:"-",pos=6:"-",pos=8:"-",pos=10:"-",1:"") Set char=$ZHex($ASCII($Extract(hash,pos))) Set output=output_$Select($Length(char)=1:"0"_char,1:char)_delim }                Set case=$ZConvert($Get(case),"U") If case'="U" Set case="L" Set output=$ZConvert(output,case) }

// return output hash Quit output }

}