Challenge 50
🗝️
Keygen
Note: this post doesn't support piracy - in the same way 99% of the population wouldn't start filling their pockets with things from the supermarket without paying, people should also be willing to support software they like
Today and especially previously, lots of software would require a license key to be validated. Since this license key was sometimes only checked by the running software and not validated with a server, it became possibly for skilled individuals to 'crack' it by disassembling & reverse-engineering the executable
Once the algorithm used to generated keys is discovered, a keygen that can automatically generate valid keys can be created by the crackers
Windows 95 surprisingly used an incredibly simple algorithm for validating keys - a key was in the format:
nnn-nnnnnnn
Here, 'n' is a number/digit (0-9). The important things to note:
- The first 3 numbers and hyphen were required for the correct format, but only the 2nd group of 7 digits actually determined it was a valid Windows product key
- To work, the final 7 digits had to add to a number that is divisible by 7. For example "999-1234567" is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 - and 28 is divisble by 7, so they key works
Note: the above is a slightly simplified version - the real version is listed here - you can implement it exactly if you want
Your task is to create a program that offers the following functionality:
- Validate product key - takes product key as a parameter and returns true or false if it's a valid license key
- Get all valid product keys - from "000-0000000" to "999-9999999", it should output all the valid product keys
- Get next product key - takes a product key as a parameter and returns it if valid, else returns the next valid product key
- Exit
Examples:
validate("000-0000000") would return true
validate("000-0000001") would return red
outputAllValidKeys() would output "000-0000000", "000-0000007", "000-0000016", ...
getNextValidKey("111-1111111") would return "111-1111111"
getNextValidKey("111-1111112") would return "111-1111118"
Extension:
Implement the key validating rules from the Wikipedia link exactly