view dcrypt/misc/checksums/Adler32.d @ 28:ad687db713a4

Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
author Thomas Dixon <reikon@reikon.us>
date Sun, 10 May 2009 22:38:48 -0400
parents 176c933827a8
children 21847420b1ac
line wrap: on
line source

/**
 * 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;
    }
    
    string name() {
        return "Adler32";
    }
    
    debug (UnitTest) {
        unittest {
            static string[] test_inputs = [
                "",
                "a",
                "checksum",
                "chexksum"
            ];
            
            static const uint[] test_results = [
                0x1u,
                0x620062u,
                0xea10354u,
                0xf0a0369u
            ];
            
            Adler32 adler32 = new Adler32;
            foreach (uint i, string j; test_inputs)
                assert(adler32.compute(j) == test_results[i], adler32.name);
        }
    }
}