Mercurial > projects > dcrypt
diff dcrypt/misc/checksums/Adler32.d @ 14:5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Tue, 18 Nov 2008 18:03:40 -0500 |
parents | |
children | 176c933827a8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dcrypt/misc/checksums/Adler32.d Tue Nov 18 18:03:40 2008 -0500 @@ -0,0 +1,61 @@ +/** + * This file is part of the dcrypt project. + * + * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. + * License: MIT + * Authors: Thomas Dixon + */ + +module dcrypt.misc.checksums.Adler32; + +import dcrypt.misc.Checksum; + +/** + * Implementation of Mark Adler's Adler32 checksum. + * + * Conforms: RFC 1950 + * References: http://tools.ietf.org/html/rfc1950#page-10 + */ +class Adler32 : Checksum { + private static const uint BASE = 65521; + + uint compute(void[] input_, uint start=1) { + ubyte[] input = cast(ubyte[])input_; + uint adler = start, + s1 = adler & 0xffff, + s2 = (adler >> 16) & 0xffff; + + foreach (ubyte i; input) { + s1 = (s1 + i) % BASE; + s2 = (s2 + s1) % BASE; + } + + return (s2 << 16) + s1; + } + + char[] name() { + return "Adler32"; + } + + version (UnitTest) { + unittest { + static char[][] test_inputs = [ + "", + "a", + "checksum", + "chexksum" + ]; + + static const uint[] test_results = [ + 0x1u, + 0x620062u, + 0xea10354u, + 0xf0a0369u + ]; + + Adler32 adler32 = new Adler32; + foreach (uint i, char[] j; test_inputs) + assert(adler32.compute(j) == test_results[i], adler32.name); + } + } +} \ No newline at end of file