package com.ericfeminella.encryption
{
import com.adobe.crypto.MD5;
/**
*
* All static API which provides a Factory implementation for
* generating Genuine Unique Identifiers (GUID)
*
* @example the following example demonstrates the creation of
* a Genuine Unique Identifiers (GUID)
*
* <listing version="3.0">
*
* var guid:Object = GUIDFactory.createGUID();
*
* trace( guid );
* //20d9d6ae1c350d8037135cd225c513f1
*
* </lsiting>
*
*/
public final class GUIDFactory
{
/**
*
* Defines a Genuine Unique Identifiers (GUID) which is
* to be generated in numerical format
*
* @example the following example demonstrates a numerical
* GUID
*
* <listing version="3.0">
*
* var guid:Object = GUIDFactory.createGUID( GUIDFactory.NUMERICAL_GUID );
*
* trace( guid );
* //647477236177.8204
*
* </lsiting>
*
*/
public static const NUMERICAL_GUID:int = 0;
/**
*
* Defines a Genuine Unique Identifiers (GUID) which is
* to be generated in textual format
*
* @example the following example demonstrates a textual
* GUID
*
* <listing version="3.0">
*
* var guid:Object = GUIDFactory.createGUID( GUIDFactory.TEXTUAL_GUID );
*
* trace( guid );
* //240aa7cd1c16fcb20c98ae6eb824acd5.8204
*
* </lsiting>
*
*/
public static const TEXTUAL_GUID:int = 1;
/**
*
* Defines the unique integer which seeds each GUID to
* ensure uniqueness which generating a GUID
*
*/
private static var ID:int = 0;
/**
*
* Creates a Genuine Unique Identifier (GUID) of the
* specified type (i.e. NUMERICAL_GUID or TEXTUAL_GUID
*
* @param the specific type of GUID in which to create
* @return an object containing the guid
*
*/
public static function createGUID(type:int = 1) : Object
{
var guid:Object;
switch ( type )
{
case NUMERICAL_GUID :
guid = numericalGUID();
break;
case TEXTUAL_GUID :
default :
guid = textualGUID();
break;
}
return guid;
}
/**
*
* @private
*
* Generates a numerical Genuine Unique Identifiers (GUID)
*
*/
private static function textualGUID() : String
{
var timestamp:Number = new Date().time;
var random:Number = Math.random() * timestamp + ID++;
var uniqueId:String = MD5.hash(random.toString());
return uniqueId;
}
/**
*
* @private
*
* Generates a textual Genuine Unique Identifiers (GUID)
*
*/
private static function numericalGUID() : Number
{
var timestamp:Number = new Date().time;
var uniqueId:Number = Math.random() * timestamp + ID++;
return uniqueId;
}
}
}