Sample key generator

Sample key generator for a shareware app.

OS version: Any

(*
WARNING! THIS IS SIMPLE SAMPLE CODE FOR A KEY GENERATOR

This is a sample key generator. Basically, it assigns a semi-unique
serial number to a copy of your app; so, there is an unique serial
number associated with such copy of your application.

You should include these two handlers within your application,
and show the resulting "appKey" in any place (eg, splash screen, about box, etc.)
When an user purchases your product, he must provide you this number,
then you return him the correspondent SN, using the "snFromKey" handler.
The user introduces this SN in your app, and it is validated using "snFromKey".

You must "randomize" this sample, adjusting it to fit your needs. Eg, you can
sum 7 to every digit, substract 14, work only with odd numbers and a large etc.

To generate the "appKey", you must use some algorithm which throws allways
the same value, so it doesn't change every time the user launches your app.
Eg, you CAN'T use "size in bytes of home folder", since this can change from
time to time. In this example, I use the MAC address and RAM memory; such
values shouldn't change so frequently, so they can be valid "static" values.

The code in "snFromKey" can be exhaustivelly customized and return
serial numbers of any length, include high-ascii chars or whatever you want.
The more complex, the more difficult to crack.

Also, you must know that AppleScript stores handler and variable names
as plain-ascii, so you better choose a strange name for your handlers
and variables in these routines, such as "|\||" or "a".
*)

--> code to show the key in a visible place of your app's interface:
set appKey to getAppKey()

--> code to generate a serial number for the key
set matchingSN to snFromKey(appKey)

--> code to validate the serial number "8211396029730" entered by an user
"8211396029730" is snFromKey(appKey)
--> false, should be "82113960297307"

to getAppKey()
	--> use any algorithm, complex-ize as much as you wish
	set hexchars to "0c1f2b38a497e5d6" --> eg, use any combination for further use
	set MACAdress to do shell script "ifconfig en0 | grep ether | cut -b '8-24'" --> get MAC address, static value
	set staticRandomNumber to (text -3 thru -1 of (((system attribute of "lram") / 1048000) as string)) as integer --> static  value
	
	set AppleScript's text item delimiters to ":"
	set macNumbers to text items of MACAdress
	set AppleScript's text item delimiters to {""}
	set appUnicNumber to 0
	repeat with i in macNumbers --> mix MAC address with "staticRandomNumber"
		if (i as string) = macNumbers's item -2 or (i as string) = macNumbers's item -1 then
			repeat with x in i
				set appUnicNumber to appUnicNumber + (offset of x in hexchars)
			end repeat
		else
			set appUnicNumber to appUnicNumber + ((i as integer) * staticRandomNumber)
		end if
	end repeat
	set appUnicNumber to (staticRandomNumber as string) & ((appUnicNumber + staticRandomNumber) as string) --> more labyrinth
end getAppKey
to snFromKey(appKey)
	local SN
	--> use any algorithm, complex-ize as much as you wish
	set SN to appKey / pi as text
	set decimalSeparator to item 2 of ("0,1" as string)
	set AppleScript's text item delimiters to decimalSeparator
	set SN to reverse of (SN's text items) --> eg, reverse
	set AppleScript's text item delimiters to {""}
	set SN to SN as string
	set AppleScript's text item delimiters to "E+"
	set SN to reverse of (SN's text items) --> eg, reverse
	set AppleScript's text item delimiters to {""}
	set SN to SN as string
end snFromKey