view dcrypt/crypto/padding/PKCS7.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 8b5eaf3c2979
children 6b2c35b84186
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.crypto.padding.PKCS7;

import dcrypt.crypto.BlockCipherPadding; 

/**
 * This class implements the padding scheme described in PKCS7
 * from RSA Security. Ex. [... 0x03, 0x03, 0x03]
 */
class PKCS7 : BlockCipherPadding
{
    string name()
    {
        return "PKCS7";   
    }
    
    ubyte[] pad(uint len)
    {
        ubyte[] output = new ubyte[len];
        
        output[0..output.length] = cast(byte)len;

        return output;
    }
    
    uint unpad(void[] input_)
    {
        ubyte[] input = cast(ubyte[]) input_;
        
        ubyte len = input[input.length-1];
         
        if (len > input.length || len == 0)
            throw new InvalidPaddingError(name()~": Incorrect padding.");
        
        uint limit = input.length;
        for (int i = 0; i < len; i++)
            if (input[--limit] != len)
                throw new InvalidPaddingError(name()~": Pad value does not match pad length.");
                        
        return len;
    }
}