This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TurboTuTone's profile picture

UUID API (Universally unique identifier)

Started by TurboTuTone, 04 July 2013 - 01:59 PM
TurboTuTone #1
Posted 04 July 2013 - 03:59 PM
UUID API for ComputerCraft

Hello,

Recently i found myself needing a unique identifier that for security reasons could not be based on simple easy to guess/generate id's such as the computerID.
Ive thought about generating a large random number, but that doesnt look so cool (reason enough to cancel that option :P/>) or to hash some variables, however then i remembered the UUID's.
http://en.wikipedia....ique_identifier

The UUID will practically never be the same plus they look pretty kickass, win win situation.
I have found a little UUID generating codesnippet that i slightly modified and included a validation and compare function into the final api.

You can get the API here:
UUID API

Usage
Usage is simple, save the contents of the pastebin into a file called "uuid" on your computer.
Include the api using:
os.loadAPI("<yourpathtofile>/uuid")

Once the api is loaded use

Function Generate:
local sMyUuid = uuid.Generate()
To generate a new UUID (returns a string)

Function Validate:
local bIsValid = uuid.Validate(sMyUuid)
Accepts a string, checks if the supplied string is a valid UUID (returns a bool, true if valid)

note: the validate function trims leading and trailing whitespaces by default, if you want to turn this off supply a extra parameter "false" like so:
local bIsValid = uuid.Validate(sMyUuid, false)

Function Compare:
local bMatch = uuid.Compare(uuid1, uuid2)
or
local bMatch = uuid.Compare(uuid1, uuid2, false)
Checks if 2 supplied strings are valid UUID's, then compares them for a match (returns true if match is found, false if no match or if one or both strings are not valid uuid's)
note: the optional third parameter passes on wether to trim leading/trailing whitespaces or not to the validate function.



Feel free to use this code and customize in whatever way you like.

Regards,
Turbo
Lyqyd #2
Posted 05 July 2013 - 07:55 PM
Split into new topic.