Sample serial number validator

Sample serial number validator for a shareware app.

OS version: Any

(*
WARNING! THIS IS SIMPLE SAMPLE CODE FOR A S/N VALIDATOR

This is the most typical method to protect a shareware application.
The user download the full application, and it can be easily
registered after purchase: no need to download a "full retail" version,
etc, etc, etc.

Usually, you provide to the "validateSN" two values: user's mail and
serial number. If you sell on-line your app, you know your user's mail,
which is a "unique" string in the world. Then, you generate the matching
serial number and send him back.

Typical serial numbers are *numbers*, *strings* or a mix of them:
341972236
mohgvuicfk
APP103-6456-1737-2358

Please, customize this handler as much as you wish. Include a copy like
this in your app, and keep yours one simply to generate the SN,
given "key1" (your user's mail address).

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".
*)

validateSN("happybirthd@y.net", "APP-531758889") --> true

to validateSN(key1, key2)
	local finalCode
	set userMail to key1 --> assume this is a mail address
	set userCode to key2 --> this is the code you provided your user
	
	--> use any algorithm, complex-ize as much as you wish
	
	--> get 3 known variables to generate the code
	set var1 to offset of "@" in userMail
	set var2 to offset of "." in userMail
	set var3 to userMail's length
	
	set finalCode to var1 * var2 * var3
	
	--> do something with the input data
	set userMail to reverse of (userMail's text items)
	set userMailLength to userMail's length
	repeat with i from 1 to userMailLength
		set finalCode to finalCode + (ASCII number userMail's item i) * i
	end repeat
	set finalCode to round (finalCode * var1 * var2 * var3 * (pi ^ 2))
	
	--> uncomment the following for your own use (guess s/n)
	-- return "APP-" & finalCode
	
	if "A" & "P" & "P" & "-" & finalCode = userCode then
		return true
	else
		return false
	end if
end validateSN

Hi jj,

I tried do implement it in my application with XCode,
but it says it expected “end” but found “to”.
I want it to display a dialog if the entries are valid,
after the button “Unlock” is pressed.

This is how I got so far:

on clicked
validateSN(contents of text field "User Mail", contents of text field "User Code")
to validateSN(key1, key2)
	local Code
	set userMail to key1
	set userCode to key2
	set x to offset of "@" in key1
	set y to offset of "." in key1
	set Code to round (x * y * (pi ^ (12)))
	
	if "U" & "S" & "E" & "-" & Code = userCode then
		display dialog "YEEEPEEEEH !!!"
	else
		beep 5
		say "OH NOOOH!"
	end if
end validateSN
end clicked

Could you help me please? Thank you very very much !!!

hi jj,

I tried your code. There is a little problem with long eMail addresses. Because AppleScript switches to scientific number notation it can result in passwords looking like so:

“APP-1,3053159E+9” (“mistersmith@yahoo.com”, “fortestingonly”)

i suggest a modification like this:


set finalCode to (finalCode * var1 * var2 * var3 * (pi ^ 2))
repeat while finalCode > 536870911
	set finalCode to finalCode / 10
end repeat
set finalCode to round (finalCode)

D.

Thank you very much !
Everything runs smoothly now. Yuhu.