Jump to content
Tuts 4 You

purely functional Keygen written in Haskell - source code


glaufan

Recommended Posts

hi

This post is in literate Haskell which means the entire post can be posted into a .lhs file and compiled by GHC.

Haskell is a lazy pure functional programming language named after the logician Haskell Curry.

http://en.wikipedia.org/wiki/Haskell_(programming_language)

http://haskell.org

The following code generates a random cd key for an old game, Battlefield 1942. I derived the keygen method with OllyDBG on a random keygen I found on astalavista.

Thanks NaG-Scrim/Fighting For Fun - it had good music ;D but your generated code is weird, either msvc6 is not so smart at optimization or your src code is strange, never mind I still made sense of the larger picture.

> import Char
> import Random
> import Text.Printf
> import System.IO> genkey :: String -> String
> genkey inp = inp ++ (fmt first_number) ++ (fmt last_number)
> where last_number = compute last_sequences magic_1
> last_sequences = (map (\x -> read x :: Int) (sp (inp ++ "00000") 4 5 []))
> magic_1 = 0x43
> first_number = compute first_sequences magic_2
> first_sequences = (map (\x -> read x :: Int) (sp ("00000" ++ inp ++ "00000") 4 7 []))
> magic_2 = 0x25> compute :: [Int] -> Int -> Int
> compute = mod . sum
> fmt v = printf "%02u" (v::Int) :: Stringsplit a string into i parts n times> sp :: String -> Int -> Int -> [String] -> [String]
> sp xs 0 n acc = acc
> sp xs i n acc = sp (drop n xs) (i-1) n (acc ++ [(take n xs)])> main = do num <- randomRIO ((10^17 :: Int),(10^18-1 :: Int))
> putStrLn $ show $ genkey $ show num
Link to comment
  • 1 month later...
  • 3 weeks later...

I started in ...

BASIC > Pascal > Motorola Assembly > Z80 Assembly > x80/86 Assembly > C (Dos) > Visual Basic > Visual C++ > .Net > Currently learning C# and Revamping my Assembly knowledge under Windows

I kind of got out of Assembly when I got into structured languages. I did used to code some of my sub routines in assemby when I was taking C in college. My professor didn't know what to think but my programs ran faster than anyone else, lol.

I really got away from programming for a while and worked on mainly hardware hacking/mod'ing.

Never heard of Haskell but it seems interesting. I would like to do some ASIC programming.

-D

Edited by ZeR0Patches
Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...