# A Scala Adler-32 checksum algorithm

While fooling around recently with various computer programming algorithms, I ended up writing an implementation of the Adler-32 checksum algorithm in Scala. There isn't too much to say about it, other than I hope I got it right. My results for the simple test below matched the results shown on the Adler-32 Wikipedia page, so that's encouraging.

Here's the Scala source code for my Adler-32 checksum implementation:

```package tests

/**
* Calculating the Adler-32 checksum using Scala.
*/

def main(args : Array[String]) {
printf("checksum (int) = %d\n", sum)
printf("checksum (hex) = %s\n", sum.toHexString)
}

def adler32sum(s: String): Int = {
var a = 1
var b = 0
s.getBytes().foreach(char => {
a = (char + a) % MOD_ADLER
b = (b + a) % MOD_ADLER
})
// note: Int is 32 bits, which this requires
return b * 65536 + a     // or (b << 16) + a
}

}
```

As you can see from the code, the algorithm is in the adler32sum function. I'm not going to document the approach here, as it seems to be well-covered on the Wikipedia site, other than to say that it involves marching through each byte in the string you're evaluating. The use of the prime number in the algorithm (65521) is clever, and again, the implications of that are discussed on Wikipedia.

tags: