Mercurial > projects > dcrypt
diff dcrypt/crypto/padding/X923.d @ 0:0e08791a1418
Initial import.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sun, 10 Aug 2008 14:20:17 -0400 |
parents | |
children | cd376996cdb3 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dcrypt/crypto/padding/X923.d Sun Aug 10 14:20:17 2008 -0400 @@ -0,0 +1,46 @@ +/** + * 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.X923; + +import dcrypt.crypto.BlockCipherPadding; + +/** + * This class implements the Null/Zero byte padding described in ANSI X.923. + * Ex. [... 0x00, 0x00, 0x03] + */ +class X923 : BlockCipherPadding { + char[] name() { + return "X923"; + } + + /* Assumes input_ is a multiple of the underlying + * block cipher's block size. + */ + uint padBlock(void[] input_, uint inOff) { + ubyte[] input = cast(ubyte[]) input_; + + ubyte len = (input.length - inOff); + + input[inOff..input.length-1] = 0; + input[input.length-1] = len; + + return len; + } + + uint padLength(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."); + + return len; + } +}