Question: I've made a cocoa mac application which needs to check if a serial number/license key is correct, i'm unsure what type of algorithms would be the best for this.

Lets say i have this serial number:
4829-1234-4321-1234-4326-5232 or any set of 4 numbers.
I was thinking about using an algorithm like if the first set of the license key is divisible by 5, second by 10, third by 24.... but i'm not sure if this is the best way to go about it.

I would also need to generate the keys in PHP.

Any help is appreciated

Answer: 
**Do not** attempt to build a serial number system from scratch without using public key cryptography. It will be child's play to crack it and create a keygen, no matter how convoluted you think your algorithm is.

I recommend the [Aquatic Prime](https://github.com/bdrister/AquaticPrime) library which uses public-key encryption. You should use the Core Foundation C-based version of the library rather than the Objective-C library as Obj-C is ridiculously easy to bypass. You should also statically link to the code rather than using a separate framework. Note that Aquatic Prime is not a complete solution and does not deal with obfuscating the license checks in your code.

It's very important that you don't create a single Objective-C method named something like `‑isRegistered` and returning a `BOOL`. This is trivial to crack.

Instead, use several different C functions (preferably macros) that are scattered throughout your code and that check different aspects of the registration code. For instance, you should check that an invalid code fails your registration test to ensure that your code has not been modified. You can also create a checksum of your app's binary and test that to ensure that it hasn't been patched.

There are many other methods that you can use to make your registration checking code more robust. I recommend searching the archives of the [MacSB Yahoo group](http://tech.groups.yahoo.com/group/macsb/) which are full of information about this topic.
The general idea I've heard is using [public-key cryptography](http://en.wikipedia.org/wiki/Public-key_cryptography). 

You generate a key pair, and put one of the keys into your application; the other is your private key. You collect a bit of information from a licensee, such as name and email address. You hash that, and encrypt the hash with your private key, then send the encrypted result to the licensee as the license code. 

The licensee is prompted by your application to enter the same information that you collected, along with the code. Now your application simply uses the key embedded in it (the "public" key) to decrypt the code, which retrieves the hash; performs a hash on the name and email; and compares the two results. If they match, the code is valid (or the person got awfully lucky).

Allan Odgaard did a swell writeup that should walk you through everything you need to do: [SIGPIPE 13: OpenSSL for License Keys](http://sigpipe.macromates.com/2004/09/05/using-openssl-for-license-keys/)

(Hope I'm not misunderstanding the question.)