Mercurial > projects > dcrypt
comparison 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 |
comparison
equal
deleted
inserted
replaced
13:7ea528b61802 | 14:5ce3012f1def |
---|---|
1 /** | |
2 * This file is part of the dcrypt project. | |
3 * | |
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. | |
5 * License: MIT | |
6 * Authors: Thomas Dixon | |
7 */ | |
8 | |
9 module dcrypt.misc.checksums.Adler32; | |
10 | |
11 import dcrypt.misc.Checksum; | |
12 | |
13 /** | |
14 * Implementation of Mark Adler's Adler32 checksum. | |
15 * | |
16 * Conforms: RFC 1950 | |
17 * References: http://tools.ietf.org/html/rfc1950#page-10 | |
18 */ | |
19 class Adler32 : Checksum { | |
20 private static const uint BASE = 65521; | |
21 | |
22 uint compute(void[] input_, uint start=1) { | |
23 ubyte[] input = cast(ubyte[])input_; | |
24 uint adler = start, | |
25 s1 = adler & 0xffff, | |
26 s2 = (adler >> 16) & 0xffff; | |
27 | |
28 foreach (ubyte i; input) { | |
29 s1 = (s1 + i) % BASE; | |
30 s2 = (s2 + s1) % BASE; | |
31 } | |
32 | |
33 return (s2 << 16) + s1; | |
34 } | |
35 | |
36 char[] name() { | |
37 return "Adler32"; | |
38 } | |
39 | |
40 version (UnitTest) { | |
41 unittest { | |
42 static char[][] test_inputs = [ | |
43 "", | |
44 "a", | |
45 "checksum", | |
46 "chexksum" | |
47 ]; | |
48 | |
49 static const uint[] test_results = [ | |
50 0x1u, | |
51 0x620062u, | |
52 0xea10354u, | |
53 0xf0a0369u | |
54 ]; | |
55 | |
56 Adler32 adler32 = new Adler32; | |
57 foreach (uint i, char[] j; test_inputs) | |
58 assert(adler32.compute(j) == test_results[i], adler32.name); | |
59 } | |
60 } | |
61 } |